2013-02-12 52 views
1

在我的頁面中,我使用了bingmap API來查找給定地址的經度和緯度。這是腳本。使用IP地址查找經度和緯度

但我需要做的是,如何通過提供IP地址作爲輸入來獲取郵政編碼。任何人都可以幫助我實現這一功能。在此先感謝

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script> 
    <script type="text/javascript"> 
     $(document).ready(function() { 
      $("#btnSearch").click(function() { 
       //check user has entered something first 
       if ($("#txtAddress").val().length > 0) { 
        //send location query to bing maps REST api 
        $.getJSON('http://dev.virtualearth.net/REST/v1/Locations?query=' + $("#txtAddress").val() + '&key=mykey&jsonp=?', function (result) { 
         if (result.resourceSets[0].estimatedTotal > 0) { 
          var loc = result.resourceSets[0].resources[0].point.coordinates; 
          $("#results").html('latitude:' + loc[0] + ', longitude:' + loc[1]); 
         } 
         else { 
          $("#results").html("sorry that address cannot be found"); 
         } 
        }); 
       } 
       else { 
        $("#results").html("please enter an address"); 
       } 
      }); 
     }); 
    </script> 
    <label for="myaddress">Type an address:</label> 
    <input type="text" id="txtAddress" /> 
    <input type="button" id="btnSearch" value="Find Location" /> 
    <p id="results"></p> 
+0

檢查http://stackoverflow.com/questions/348614/how-to-determine-a-zip-code-and-city-from-an-ip-address – ripa 2013-02-12 08:11:40

回答

1

嘗試

$ip = "YOUR_IP_ADDRESS"; 

$_API = "API_KEY"; 
$_URL = "http://api.ipinfodb.com/v3/ip-city/?key=$_API&ip=$ip&format=json";

輸出

{ "statusCode" : "OK", "statusMessage" : "", "ipAddress" : "YOUR_IP_ADDRESS", "countryCode" : "IN", "countryName" : "INDIA", "regionName" : "GUJARAT", "cityName" : "AHMEDABAD", "zipCode" : "ZIPCODE", "latitude" : "23.0333", "longitude" : "72.6167", "timeZone" : "+05:30" }

API_KEY將可在Ipinfodb

+0

如何獲取API密鑰。我對這項任務很陌生。所以,你能告訴我如何獲得API密鑰 – Rithu 2013-02-12 08:14:42

+0

只是在那裏註冊自己,然後他們會引導你一個很棒的例子。 – 2013-02-12 08:18:01