<script type="application/javascript">
function getip(json){
alert(json.ip); // alerts the ip address
}
</script>
<script type="application/javascript" src="http://jsonip.appspot.com/?callback=getip"></script>
I can get User IP by this code...
I want to find location of this IP. How can I?
This question is related to
javascript
jquery
ip
you can use ipinfodb after getting your api key you can query for a location against a specific ip like this http://api.ipinfodb.com/v2/ip_query.php?key=" + apiKey + "&ip=" + ip + "&output=xml
you can then then extract the location from the xml response
Just in case you were not able to accomplish the above code, here is a simple way of using it with jquery:
$.getJSON("http://www.geoplugin.net/json.gp?jsoncallback=?",
function (data) {
for (var i in data) {
document.write('data["i"] = ' + i + '<br/>');
}
}
);
Try TUQ GEO IP API it's free and really neat and sweet with jsonp support
It's quite easy with an API that maps IP address to location. Run the snippet to get city & country for the IP in the input box.
$('.send').on('click', function(){_x000D_
_x000D_
$.getJSON('https://ipapi.co/'+$('.ip').val()+'/json', function(data){_x000D_
$('.city').text(data.city);_x000D_
$('.country').text(data.country);_x000D_
});_x000D_
});
_x000D_
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>_x000D_
_x000D_
<input class="ip" value="8.8.8.8">_x000D_
<button class="send">Go</button>_x000D_
<br><br>_x000D_
<span class="city"></span>, _x000D_
<span class="country"></span>
_x000D_
You can use this google service free IP geolocation webservice
update
the link is broken, I put here other link that include @NickSweeting in the comments:
and you can get the data in json format:
Either one of the following links should take care of this:
http://ipinfodb.com/ip_location_api_json.php
Those links have tutorials for getting a users location through Javascript. However, they do so through an API to an external data service. If you have an extremely high traffic site, you might want to hosting the data yourself (or getting a premium api service). To host everything yourself, you will have to host a database with IP Geolocation and use ajax to feed the users location into Javascript. If this is the approach you want to take, you can get a free database of IP information below:
http://www.ipinfodb.com/ip_database.php
Please note that this method entails having to periodically update the database to stay accurate in tracing ips to locations.
You need a database that contains IP address and location mapping. Or you can use a lot of online tools to achieve this, for example: http://www.ipligence.com/geolocation
Google returs lots of result under keywords: "IP location"
A free open source community run geolocation ip service that runs on the MaxMind database is available here: https://ipstack.com/
Example
https://api.ipstack.com/160.39.144.19
Limitation
10,000 queries per month
A rather inexpensive option would be to use the ipdata.co API, it's free upto 1500 requests a day.
This answer uses a 'test' API Key that is very limited and only meant for testing a few calls. Signup for your own Free API Key and get up to 1500 requests daily for development.
$.get("https://api.ipdata.co?api-key=test", function (response) {_x000D_
$("#ip").html("IP: " + response.ip);_x000D_
$("#city").html(response.city + ", " + response.region);_x000D_
$("#response").html(JSON.stringify(response, null, 4));_x000D_
}, "jsonp");
_x000D_
<h1><a href="https://ipdata.co">ipdata.co</a> - IP geolocation API</h1>_x000D_
_x000D_
<div id="ip"></div>_x000D_
<div id="city"></div>_x000D_
<pre id="response"></pre>_x000D_
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
_x000D_
View the Fiddle at https://jsfiddle.net/ipdata/6wtf0q4g/922/
I wish to point out that if you use http://freegeoip.net/, you don't need to supply to it the IP address of the client's location. Just try these:
However, I am unable to retrieve the information with AJAX calls, probably because of some cross-origin policy. Apparently they have not allowed public access to their system.
A better way is to skip the "middle man" (ip)
jQuery.get("http://ipinfo.io", function(response) {
console.log(response.city);
}, "jsonp");
This gives you the IP, the city, the country, etc
$.getJSON('//freegeoip.net/json/?callback=?', function(data) {
console.log(JSON.stringify(data, null, 2));
});
Source: Stackoverflow.com