php PHP获取用户机器的经纬度
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16380544/
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 the longitude and latitude as user's machine in PHP
提问by Irfan Mir
How can I get the longitude and latitude using PHP of the user's machine?
如何使用用户机器的PHP获取经度和纬度?
I need the it to be so the longitude if north is a positive value and if south it is a negative value/
如果北是正值,如果南是负值,我需要它是经度/
And in terms of latitude, if east it is positive value and if west it is a negative value.
就纬度而言,如果东是正值,如果西是负值。
How can I do this?
我怎样才能做到这一点?
回答by adeneo
The only way to geolocate on the serverside would be to use a lookup table for the IP adress. There are services that provide this for you, so you could do this :
在服务器端进行地理定位的唯一方法是使用 IP 地址的查找表。有一些服务可以为您提供此服务,因此您可以这样做:
$ip = !empty($_SERVER['HTTP_X_FORWARDED_FOR']) ? $_SERVER['HTTP_X_FORWARDED_FOR'] : $_SERVER['REMOTE_ADDR'];
$url = "http://freegeoip.net/json/$ip";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
$data = curl_exec($ch);
curl_close($ch);
if ($data) {
$location = json_decode($data);
$lat = $location->latitude;
$lon = $location->longitude;
$sun_info = date_sun_info(time(), $lat, $lon);
print_r($sun_info);
}
It won't always be very accurate though. In javascript you would have access to the HTML5 Geolocation API, or Google Maps, but reverse geocoding with Google requires you to use a map, as per the TOS.
但它并不总是非常准确。在 javascript 中,您可以访问 HTML5 Geolocation API 或 Google Maps,但根据 TOS,使用 Google 进行反向地理编码要求您使用地图。
NOTE(2019): The service used in the example, FreeGeoIP has shut down, I'll leave the answer for posterity, as there are surely other services offering the same service.
注意(2019 年):示例中使用的服务 FreeGeoIP 已关闭,我会将答案留给后代,因为肯定还有其他服务提供相同的服务。
回答by Jordan.J.D
Please refer to the Google Maps API Geocoder: http://lab.abhinayrathore.com/ipmapper/
请参考 Google Maps API Geocoder:http: //lab.abhinayrathore.com/ipmapper/
That will answer all of your questions from A-Z. If you are having trouble putting it to use check out where this question has been asked and answered: Find Latitude & Longitude of a given IP Address using IPMapper
这将回答您所有来自 AZ 的问题。如果您在使用它时遇到问题,请查看询问和回答此问题的位置:使用 IPMapper 查找给定 IP 地址的纬度和经度
This will be done using Javascript and not PHP
这将使用 Javascript 而不是 PHP 来完成
回答by Mooseman
Because exact location requires user consent, it cannot be done with PHP. Try using JS to get the user's location and use AJAX to send it to the server.
因为确切的位置需要用户同意,所以不能用 PHP 来完成。尝试使用JS获取用户的位置并使用AJAX将其发送到服务器。
If you are willing to use an approximate location, you can map by IP address. A web search will bring up a few options for that, such as this one.
如果您愿意使用大致位置,则可以通过 IP 地址映射。网络搜索会为此提供一些选项,例如this。