Reputation: 75
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.
Upvotes: 2
Views: 4254
Reputation: 12943
Another source is: https://www.geoip-db.com They provide a JSON and JSONP-callback solution.
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.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>
Upvotes: 1
Reputation: 8166
You should use an IP to location API, like these:
http://ipinfodb.com/ip_location_api_json.php
http://www.ipaddressapi.com/ ($)
Or manage to get data from other sources like:
Upvotes: 5