Skip to content

Instantly share code, notes, and snippets.

@manoj-singh-developer
Created November 2, 2018 11:22
Show Gist options
  • Select an option

  • Save manoj-singh-developer/97c03021393f7abb176fa22d72f51fde to your computer and use it in GitHub Desktop.

Select an option

Save manoj-singh-developer/97c03021393f7abb176fa22d72f51fde to your computer and use it in GitHub Desktop.
function ipLookUp () {
$.ajax('http://ip-api.com/json')
.then(
function success(response) {
console.log('User\'s Location Data is ', response);
console.log('User\'s Country', response.country);
getAdress(response.lat, response.lon)
},
function fail(data, status) {
console.log('Request failed. Returned status of',
status);
}
);
}
function getAddress (latitude, longitude) {
$.ajax('https://maps.googleapis.com/maps/api/geocode/json?' +
'latlng=' + latitude + ',' + longitude + '&key=' +
GOOGLE_MAP_KEY)
.then(
function success (response) {
console.log('User\'s Address Data is ', response)
},
function fail (status) {
console.log('Request failed. Returned status of',
status)
}
)
}
if ("geolocation" in navigator) {
// check if geolocation is supported/enabled on current browser
navigator.geolocation.getCurrentPosition(
function success(position) {
// for when getting location is a success
console.log('latitude', position.coords.latitude,
'longitude', position.coords.longitude);
getAddress(position.coords.latitude,
position.coords.longitude)
},
function error(error_message) {
// for when getting location results in an error
console.error('An error has occured while retrieving' +
'location', error_message)
ipLookUp()
}
});
} else {
// geolocation is not supported
// get your location some other way
console.log('geolocation is not enabled on this browser')
ipLookUp()
}
@manoj-singh-developer
Copy link
Author

manoj-singh-developer commented Nov 16, 2018

`<script>
$.ajax({
url: "https://geoip-db.com/jsonp",
jsonpCallback: "callback",
dataType: "jsonp",
success: function( location ) {
//alert(location.city);
//$("#city select").val(location.city);
$("#city").val(location.city).change();

        }
    });     
</script>`

@manoj-singh-developer
Copy link
Author

`

<title>GEOIP DB - jQuery example</title> <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
Country:
State:
City:
Latitude:
Longitude:
IP: <script> $.ajax({ url: "https://geoip-db.com/jsonp", jsonpCallback: "callback", dataType: "jsonp", success: 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> `

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment