简体   繁体   中英

Getting geolocation from IP address

I need to track user details, for that client's location and IP address is needed.
I got IP address from

$this->input->ip_address();

in codiigniter.
Now the problem is how can i make their location in google map by using their respective IP address

just use ipinfo.io at

http://ipinfo.io/

it uses a location api that we can post ip address and it will returns the location details as json:

we can able to display the loacation on map with the langitude and longitude details from json response to google Maps API.

Here is the code i used:

this script creates a Google Map instance with lattitude & longitude from json response:

jQuery(document).ready(function(){
    jQuery.get("http://ipinfo.io/202.88.237.138", function (response)
               {
                   var lats = response.loc.split(',')[0]; 
                   var lngs = response.loc.split(',')[1];
                   map = new GMaps({
                       el: '#map',
                       lat: lats, //latitude
                       lng: lngs //longitude
                   });

               }, "jsonp");
});

and the map will displayed on:

<div style="border:1px solid red; height:745px;" id="map"></div>

Google Maps API gmaps.js is needed to run this..

As a viable (although often less accurate) alternative, the HTML5 spec includes Geolocation . As HTML 5 becomes more and more prevalent I think we can expect to see this become standard in the future. It also does not require the use of external web services or libraries. Everything you need to geolocate is built right into the clients browser.

I believe the primary method used is IP address as specified.

The Google Maps API will do the work of finding location using geoip for you. If the user is on a mobile device or has a more accurate way of locating themselves (like a GPS), it'll use that instead.

Here's a good starting point:
https://google-developers.appspot.com/maps/documentation/javascript/examples/map-geolocation

If you absolutely need to fetch location from IP only, there are third-party tools that you can scrape to get this information. You should make sure you have permission beforehand if you're using this in a larger project.

freegeoip.net provides a public HTTP API to search the geolocation of IP addresses. It uses a database of IP addresses that are associated to cities along with other relevant information like time zone, latitude and longitude.

You're allowed up to 15,000 queries per hour. If you need more than that, you can run the freegeoip as a web server on your own infrastructure. See: freegeoip on github

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM