2015-06-12 28 views
0

我複製粘貼下面的代碼,我是一個noob在這。那麼你能指出錯誤嗎?我很抱歉提出這樣一個愚蠢的問題。使用PHP的地理位置

<HTML> 

<HEAD> 
</HEAD> 

<BODY> 
    <h3>Client side IP geolocation using <a href="http://ipinfo.io">ipinfo.io</a></h3> 
    <hr/> 
    <div id="ip"></div> 
    <div id="address"></div> 
    <hr/>Full response: <pre id="details"></pre> 
</BODY> 

</HTML> 
<script type="text/javascript" charset="utf-8"> 
$.get("http://ipinfo.io", function(response) { 
    $("#ip").html("IP: " + response.ip); 
    $("#address").html("Location: " + response.city + ", " + response.region); 
    $("#details").html(JSON.stringify(response, null, 4)); 
}, "jsonp"); 
</script> 
+0

你問轉換在PHP相當於該代碼? – Gabs

+0

IDK只是想運行這個並獲取基於IP地址的地理位置並獲取訪問者的地址。它會起作用嗎? –

+0

我將此文件保存爲location.html –

回答

3

使用獲得用戶的緯度,經度和獲取當前用戶地址HTML5。

示例代碼:

<!DOCTYPE html> 
<html> 
    <head> 
     <script type="text/javascript" src="http://code.jquery.com/jquery-latest.min.js"></script> 
     <script src="https://maps.googleapis.com/maps/api/js?v=3.exp&signed_in=true"></script> 
    </head> 
    <body> 
     <p>Address: <div id="address"></div></p> 
    </body> 
    <script type="text/javascript" charset="utf-8"> 
    $(document).ready(function() { 

     var currgeocoder; 

     //Set geo location lat and long 
     navigator.geolocation.getCurrentPosition(function(position, html5Error) { 
      geo_loc = processGeolocationResult(position); 
      currLatLong = geo_loc.split(","); 
      initializeCurrent(currLatLong[0], currLatLong[1]); 
     }); 

     //Get geo location result 
     function processGeolocationResult(position) { 
      html5Lat = position.coords.latitude; //Get latitude 
      html5Lon = position.coords.longitude; //Get longitude 
      html5TimeStamp = position.timestamp; //Get timestamp 
      html5Accuracy = position.coords.accuracy; //Get accuracy in meters 
      return (html5Lat).toFixed(8) + ", " + (html5Lon).toFixed(8); 
     } 

     //Check value is present or 
     function initializeCurrent(latcurr, longcurr) { 
      currgeocoder = new google.maps.Geocoder(); 

      console.log(latcurr + "-- ######## --" + longcurr); 

      if (latcurr != '' && longcurr != '') { 
       //call google api function 
       var myLatlng = new google.maps.LatLng(latcurr, longcurr); 
       return getCurrentAddress(myLatlng); 
      } 
     } 

     //Get current address 
     function getCurrentAddress(location) { 
      currgeocoder.geocode({ 
       'location': location 
      }, function(results, status) { 
       if (status == google.maps.GeocoderStatus.OK) { 
        console.log(results[0]); 
        $("#address").html(results[0].formatted_address); 
       } else { 
        alert('Geocode was not successful for the following reason: ' + status); 
       } 
      }); 
     } 
    }); 
    </script> 
</html> 

演示:http://jsfiddle.net/hmy7e0fs/

0
<HTML> 
<HEAD> 

</HEAD> 
<script src="http://code.jquery.com/jquery-2.1.4.min.js" type="text/javascript" ></script> 
<script src="http://js.maxmind.com/js/geoip.js" type="text/javascript" ></script> 
<script type="text/javascript" charset="utf-8"> 


$.get("http://ipinfo.io", function (response) { 
alert('hello'); 
    $("#ip").html("IP: " + response.ip); 
    $("#address").html("Location: " + response.city + ", " + response.region); 
    $("#details").html(JSON.stringify(response, null, 4)); 
}, "jsonp"); 
</script> 
<BODY> 
<h3>Client side IP geolocation using <a href="http://ipinfo.io">ipinfo.io</a></h3> 

<hr/> 
<div id="ip"></div> 
<div id="address"></div> 
<hr/>Full response: <pre id="details"></pre> 
</BODY> 
</HTML> 
0

使用此:

<?php 
$ip = $_SERVER['REMOTE_ADDR']; 
$locationArray = unserialize(file_get_contents('http://www.geoplugin.net/php.gp?ip=' . $ip)); 
echo '<pre>'; 
print_r($locationArray); 

?> 

我想喜歡geoplugin優於http://ipinfo.io每分鐘的請求限制爲最大。

欲瞭解更多信息,請訪問:http://www.geoplugin.com/

+0

它的工作原理!謝謝你 –

+0

@Himanshu,它是我的榮幸;) –

+0

是的。順便說一句,你應該檢查HTML5之一。 IDK是我將使用的兩個之一。但你絕對有用的人! –