php 如何通过IP地址或精确定位获取用户的经纬度
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20769064/
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
How to obtain latitude and longitude of a user by IP address or pinpoint location
提问by user111671
I want to get the geolocation (latitude and longitude) of a computer user by a php script. I was using this one
我想通过 php 脚本获取计算机用户的地理位置(纬度和经度)。我正在使用这个
<?php
// Get lat and long by address
$address = $dlocation; // Google HQ
$prepAddr = str_replace(' ','+',$address);
$geocode=file_get_contents('http://maps.google.com/maps/api/geocode/json?address='.$prepAddr.'&sensor=false');
$output= json_decode($geocode);
$latitude = $output->results[0]->geometry->location->lat;
$longitude = $output->results[0]->geometry->location->lng;
?>
but the problem is that this uses real adressesand I just want the user to log in, ask him for the permission to track him (allow location) and if he accepts obtain the desired values (longitude and latitude). It could be by an IP adressor another way to pinpoint his location but the thing is I wanted this method to be praticallyflawless regarding proxies and fakies. Any idea on how I could do this? Thank you very much!
但问题是这使用了真实地址,我只希望用户登录,请求他允许跟踪他(允许位置),如果他接受,则获取所需的值(经度和纬度)。这可能是由一个IP地址或另一种方式来找出他的位置,但事情是,我想这个方法是pratically无瑕关于代理和fakies。关于我如何做到这一点的任何想法?非常感谢!
回答by R R
I think this is what you want.simple and easy to use.Thanks to HTML5see source here
我认为这就是您想要的。简单且易于使用。感谢 HTML5,请在此处查看源代码
<html>
<body>
<p id="demo">Click the button to get your coordinates:</p>
<button onclick="getLocation()">Try It</button>
<script>
var x=document.getElementById("demo");
function getLocation()
{
if (navigator.geolocation)
{
navigator.geolocation.getCurrentPosition(showPosition);
}
else{x.innerHTML="Geolocation is not supported by this browser.";}
}
function showPosition(position)
{
x.innerHTML="Latitude: " + position.coords.latitude +
"<br>Longitude: " + position.coords.longitude;
}
</script>
</body>
</html>
回答by Avish Shah
download geoip.inc - http://www.maxmind.com/download/geoip/api/php-20120410/geoip.inc, geoipcity.inc - http://www.maxmind.com/download/geoip/api/php-20120410/geoipcity.inc, geoipregionvars.php - http://www.maxmind.com/download/geoip/api/php-20120410/geoipregionvars.php,
下载 geoip.inc - http://www.maxmind.com/download/geoip/api/php-20120410/geoip.inc, geoipcity.inc - http://www.maxmind.com/download/geoip/api/php -20120410/geoipcity.inc, geoipregionvars.php - http://www.maxmind.com/download/geoip/api/php-20120410/geoipregionvars.php,
GeoLiteCity.dat - http://geolite.maxmind.com/download/geoip/database/GeoLiteCity.dat.gzplease convert GeoLiteCity.dat.gz to GeoLiteCity.dat and put in geoip named folder
GeoLiteCity.dat - http://geolite.maxmind.com/download/geoip/database/GeoLiteCity.dat.gz请将 GeoLiteCity.dat.gz 转换为 GeoLiteCity.dat 并放入 geoip 命名文件夹
include("application/libraries/geoip/geoipcity.inc");
include("application/libraries/geoip/geoipregionvars.php");
$giCity = geoip_open("application/libraries/geoip/GeoLiteCity.dat", GEOIP_STANDARD);
$ip =$_SERVER['REMOTE_ADDR'];
$record = geoip_record_by_addr($giCity, $ip);
echo "Getting Country and City detail by IP Address <br /><br />";
echo "IP: " . $ip . "<br /><br />";
echo "Country Code: " . $record->country_code . "<br />" .
"Country Code3: " . $record->country_code . "<br />" .
"Country Name: " . $record->country_name . "<br />" .
"Region Code: " . $record->region . "<br />" .
"Region Name: " . $GEOIP_REGION_NAME[$record->country_code][$record->region] . "<br />" .
"City: " . $record->city . "<br />" .
"Postal Code: " . $record->postal_code . "<br />" .
"Latitude: " . $record->latitude . "<`enter code here`br />" .
"Longitude: " . $record->longitude . "<br />" .
"Metro Code: " . $record->metro_code . "<br />" .
"Area Code: " . $record->area_code . "<br />" ;

