php 获取手机的 GPS 位置
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15831203/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Get GPS Location of Mobile Phone
提问by cpdt
I have been looking around Google and SO for a way to get the GPS location of a user using a mobile phone with PHP. However, the only solutions I have found use Javascript, and I would rather not have to do some complicated stuff using Javascript when I can just add some PHP code to my already existing system.
我一直在 Google 和 SO 周围寻找一种方法来获取使用带有 PHP 的手机的用户的 GPS 位置。但是,我发现的唯一解决方案是使用 Javascript,当我可以将一些 PHP 代码添加到我现有的系统时,我宁愿不必使用 Javascript 做一些复杂的事情。
My question is simple: How can I get the GPS location of somebody browsing my site with PHP, instead of Javascript?
我的问题很简单:如何使用 PHP 而不是 Javascript 获取浏览我网站的人的 GPS 位置?
Additionally: If I have to use Javascript, how can I access the value from PHP? Also, is there any way to get a city/country from this GPS location, or possibly another method to get the city/country?
另外:如果我必须使用 Javascript,我如何从 PHP 访问该值?另外,有没有办法从这个 GPS 位置获取城市/国家,或者可能有另一种获取城市/国家的方法?
回答by nickhar
You can't get the GPS location using PHP.
您无法使用 PHP 获取 GPS 位置。
You could use GeoLocation to approximate it (using the IP address), but this is notoriously unreliable and best avoided.
您可以使用 GeoLocation 来近似它(使用 IP 地址),但众所周知,这是不可靠的,最好避免。
The other option is to use the location API built into HTML5. Upon visiting your site, those using a capable mobile device(or browser) will be asked if they wish to disclose their location for functional reasons, but you can't force users to divulge it:
另一种选择是使用 HTML5 中内置的位置 API。在访问您的网站时,系统会询问使用功能强大的移动设备(或浏览器)的用户是否出于功能原因希望透露他们的位置,但您不能强迫用户透露:
function show_map(position) {
var latitude = position.coords.latitude;
var longitude = position.coords.longitude;
// let's show a map or do something interesting!
}
An example guide/overview worth reading is here(which includes a live example of how location-aware browsing works).
值得一读的示例指南/概述在这里(其中包括位置感知浏览如何工作的现场示例)。
Location information is a very sensitive area with regard to privacy and thus handset manufacturers won't allow automatic disclosure of the information you're looking for.
位置信息是一个非常敏感的隐私领域,因此手机制造商不允许自动披露您正在寻找的信息。
Further reading resources:
进一步阅读资源:
- W3C geolocation API
- geoPosition.js, a geolocation API wrapper script
- Internet Explorer 9 Guide for Developers: Geolocation
- An SO Question: Web Geolocation API on the iPhone
- W3C 地理定位 API
- geoPosition.js,一个地理定位 API 包装脚本
- Internet Explorer 9 开发人员指南:地理位置
- 一个 SO 问题:iPhone 上的 Web 地理定位 API
回答by m4t1t0
There is no way to retrive this data via PHP, remember that PHP is executed in the server, not in the client, so you need a client execution language like javascript.
无法通过 PHP 检索这些数据,请记住,PHP 是在服务器中执行的,而不是在客户端中执行,因此您需要一种客户端执行语言,例如 javascript。