我想通过 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 地址 或其他方式来查明他的位置,但问题是我希望这种方法实际上是 在代理和伪造方面完美无瑕。关于我如何做到这一点的任何想法?非常感谢你!

最佳答案

下载 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 />" ;

关于php - 如何通过IP地址或精确定位获取用户的经纬度,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/20769064/

10-11 05:07