Skip to content

Instantly share code, notes, and snippets.

@isauravmanitripathi
Last active March 24, 2019 17:34
Show Gist options
  • Save isauravmanitripathi/d9ee22ade5cd02fdd7654295c0e1ef39 to your computer and use it in GitHub Desktop.
Save isauravmanitripathi/d9ee22ade5cd02fdd7654295c0e1ef39 to your computer and use it in GitHub Desktop.
Help you to get location of your user in real time, in php
<h1>Getting your current location</h1>
<input type="button" id="go" value="click me to view your location">
<script>
$(document).ready(function () {
// wire up button click
$('#go').click(function() {
// test for presence of geolocation
if (navigator && navigator.geolocation) {
navigator.geolocation.getCurrentPosition(geo_success, geo_error);
} else {
error ('geolocation is not supported.');
}
});
});
function geo_sucess(position) {
printLatLong(position.coords.latitude, position.coords.longitude);
}
// The PositionError object returned contains the following attributes:
// code: a numeric response code
// PERMISSIO_DENIED = 1
// POSITION_UNAVAILABLE = 2
// TIMEOUT = 3
// message: Primarily for debugging. It's recommeded not to show this error
// to users.
function geo_error(err) {
if (err.code == 1) {
error('The user denied the request for location information ')
} else if (err.code == 2) {
error('Your location information is unavailable.')
} else if (err.code == 3) {
error ('The request to get your location timed out.')
} else {
error('An unkown error occured while requesting your location.')
}
}
// output lat and long
function printLatLong(lat, long) {
$('body').append('<p>Lat: ' + lat + '</p>');
$('body').append('<p>Long: ' + long + '</p>');
}
function error(msg) {
alert (msg);
}
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment