簡體   English   中英

如何通過IP地址或精確位置獲取用戶的經度和緯度

[英]How to obtain latitude and longitude of a user by IP address or pinpoint location

我想通過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。 有關如何做到這一點的任何想法? 非常感謝你!

我認為這就是你想要的。簡單易用。感謝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>

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM