我想通过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;
?>
但问题是,这使用真实地址,我只想让用户登录,询问他是否有权跟踪他(允许位置)以及他是否接受获得所需的值(经度和纬度)。它可以通过 IP地址或其他方式来确定他的位置,但问题是我希望这种方法对于代理和 pratically 完美无缺。 fakies。有关如何做到这一点的任何想法?非常感谢你!
答案 0 :(得分:6)
我认为这就是你想要的。简单易用。感谢HTML5 see source here
<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>
答案 1 :(得分:4)
下载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.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 />" ;
答案 2 :(得分:0)
尝试IpInfoD http://ipinfodb.com/
这对你有帮助
答案 3 :(得分:0)
为什么不使用我们的地理位置API?只需转到https://smartip.io并注册即可获得一个免费的API密钥,该密钥每月可在Free层上接受250,000个请求。 要实现JQuery中所需的功能,只需使用以下代码段即可:
$.ajax ({
type: "GET",
url: "https://smartip.io/api/112.126.34.5/?api_key=[YOUR_API_KEY]",
success: function(response) { alert(response); }
});
JSON响应对象将包含您需要的地理位置信息以及许多其他信息:
{
"ip": "72.229.28.185",
"ip-type": "ipv4",
"hostname": "cpe-72-229-28-185.nyc.res.rr.com",
"country": {
"country-iso-code": "US",
"country-two-letter-iso-code": "US",
"country-name": "United States",
"capital": "Washington",
"is-in-europe": false,
"is-metric": false,
"country-geo-id": 6252001,
"continent-name": "North America",
"continent-code": "NA",
"continent-geo-id": 6255149,
"region-name": "New York",
"region-code": "NY",
"region-geo-id": 5128638
},
"location": {
"city": "New York",
"zip-code": "10009",
"latitude": "40.7263",
"longitude": "-73.9818",
"timezone": "America/New_York",
"metro-code": 501
}