最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to get city name by ip in google map? - Stack Overflow

programmeradmin1浏览0评论

Please help me to find cityname according to ip. Let me explain.

I am developing a webpage where I want it to automatically fetch the city name where it is opened and it should also display the location in the google map automatically.

Please help me to find cityname according to ip. Let me explain.

I am developing a webpage where I want it to automatically fetch the city name where it is opened and it should also display the location in the google map automatically.

Share Improve this question asked Mar 20, 2012 at 8:46 RashtraRashtra 753 silver badges12 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 5

You should use an IP to location API, like these:

http://ipinfodb./ip_location_api_json.php

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

Or manage to get data from other sources like:

http://www.iplocation/

http://ip2loc.jerodsanto/

Another source is: https://www.geoip-db. They provide a JSON and JSONP-callback solution.

  • JSON: https://geoip-db./json/
  • JSONP: https://geoip-db./json/geoip.php?jsonp=callback

A jQuery example:

<!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./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./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>

发布评论

评论列表(0)

  1. 暂无评论