使用IP地址查找纬度和经度

时间:2013-02-12 07:53:13

标签: php javascript bing-maps

在我的页面中,我使用bingmap API查找给定地址的纬度和经度。这是脚本。

但我需要做的是,如何通过提供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>

1 个答案:

答案 0 :(得分: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提供。