如何在谷歌地图中通过IP获取城市名称?

时间:2012-03-20 08:46:24

标签: javascript google-maps-api-3 geolocation ip tracking

请帮我根据ip找到cityname。让我解释一下。

我正在开发一个网页,我希望它能够自动获取打开它的城市名称,并且还应该自动在谷歌地图中显示该位置。

2 个答案:

答案 0 :(得分:5)

您应该使用IP到位置API,例如:

http://ipinfodb.com/ip_location_api_json.php

http://www.ipaddressapi.com/($)

或设法从其他来源获取数据,如:

http://www.iplocation.net/

http://ip2loc.jerodsanto.net/

答案 1 :(得分:1)

另一个来源是:https://www.geoip-db.com他们提供了JSON和JSONP回调解决方案。

一个jQuery示例:

<!DOCTYPE html> 
<html> 
<head> 
<meta name="viewport" content="initial-scale=1.0, user-scalable=no"/> 
<meta http-equiv="content-type" content="text/html; charset=UTF-8"/> 
<title>Geo City Locator</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js"></script>
</head> 
<body > 
    <div>Country: <span id="country"></span></div>
    <div>State: <span id="state"></spa></div>
    <div>City: <span id="city"></span></div>
    <div>Latitude: <span id="latitude"></span></div>
    <div>Longitude: <span id="longitude"></span></div>
    <div>IP: <span id="ip"></span></div>    

    <script>
      $.getJSON('https://geoip-db.com/json/geoip.php?jsonp=?').done(function(location) {
        $('#country').html(location.country_name);
        $('#state').html(location.state);
        $('#city').html(location.city);
        $('#latitude').html(location.latitude);
        $('#longitude').html(location.longitude);
        $('#ip').html(location.IPv4);               
     });
</script>

相关问题