如何通过IP地址或精确位置获取用户的经纬度 [英] How to obtain latitude and longitude of a user by IP address or pinpoint location

查看:566
本文介绍了如何通过IP地址或精确位置获取用户的经纬度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想通过php脚本获取计算机用户的地理位置(经度和纬度)。我正在使用这个

 <?php 

//获取lat和long by地址
$ 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地址或其他方式查明他的位置,但事实是我希望这种方法在代理方面完美无瑕, 完美无瑕。 fakies。任何想法如何我可以做到这一点?非常感谢!

解决方案

下载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 /库/ 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通过IP地址获取国家和城市详细信息< br />< br />;
回显IP:。 $ ip。 < br />< br />;

回显国家代码:。 $ record-> country_code。 < br /> 。
国家代码3:。 $ record-> country_code。 < br /> 。
国家名称:。 $ record-> country_name。 < br /> 。
地区代码:。 $ record->地区。 < br /> 。
地区名称:。 $ GEOIP_REGION_NAME [$ record-> country_code] [$ record->地区]。 < br /> 。
城市:。 $ record->城市。 < br /> 。
邮编:。 $ record->邮政编码。 < br /> 。
纬度:。 $ record->纬度。 <`在此处输入代码`br /> 。
经度:。 $ record->经度。 < br /> 。
Metro Code:。 $ record-> metro_code。 < br /> 。
区号:。 $ record-> area_code。 < br /> ;


I want to get the geolocation (latitude and longitude) of a computer user by a php script. I was using this one

<?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 adresses and 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 adress or another way to pinpoint his location but the thing is I wanted this method to be pratically flawless regarding proxies and fakies. Any idea on how I could do this? Thank you very much!

解决方案

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,

GeoLiteCity.dat - http://geolite.maxmind.com/download/geoip/database/GeoLiteCity.dat.gz please convert GeoLiteCity.dat.gz to GeoLiteCity.dat and put in geoip named folder

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

这篇关于如何通过IP地址或精确位置获取用户的经纬度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆