【问题标题】:How to get city name by ip in google map?如何在谷歌地图中通过ip获取城市名称?
【发布时间】:2012-04-04 17:39:21
【问题描述】:

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

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

【问题讨论】:

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


    【解决方案1】:

    您应该使用 IP 定位 API,如下所示:

    http://ipinfodb.com/ip_location_api_json.php

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

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

    http://www.iplocation.net/

    http://ip2loc.jerodsanto.net/

    【讨论】:

    • 另外一个值得一提的是:maxmind.com/app/ip-location(有免费版,但如果我没记错的话,只有付费版才有城市精度)
    • 值得一提的是,这种方法可能非常不准确。你能说的最接近我的是“英国”,当然不是地区层面的,更不用说城镇了。
    【解决方案2】:

    另一个来源是: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>
    

    【讨论】:

      猜你喜欢
      • 2018-01-16
      • 2012-12-27
      • 2023-03-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-11-21
      相关资源
      最近更新 更多