Last active
December 18, 2015 08:39
-
-
Save ajitam/5755323 to your computer and use it in GitHub Desktop.
Get user location. Is there a better way?
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// http://jsfiddle.net/2vbY8/1/ | |
var locator = { | |
current_location: null, | |
getLocation: function() { | |
if (navigator.geolocation) { | |
// Browser DOES support HTML location | |
navigator.geolocation.getCurrentPosition(locator.locationOK,locator.locationFAIL); | |
} else { | |
// Browser DOESN'T support HTML5 location | |
// Check if we can detect location from IP | |
if (google.loader.ClientLocation != null) { | |
var lat = google.loader.ClientLocation.latitude; | |
var lng = google.loader.ClientLocation.longitude; | |
locator.current_location = { | |
"coords": { | |
"latitude": lat, | |
"longitude": lng | |
} | |
,"type": "ip" | |
}; | |
$("#location-type").text(locator.current_location['type']); | |
$("#location-lat").text(locator.current_location['coords']['latitude']); | |
$("#location-lng").text(locator.current_location['coords']['longitude']); | |
} else { | |
// Can't detect location | |
locator.current_location = { | |
"coords": { | |
"latitude": null, | |
"longitude": null | |
} | |
,"type": "not-supported" | |
}; | |
$("#location-type").text(locator.current_location['type']); | |
$("#location-lat").text(locator.current_location['coords']['latitude']); | |
$("#location-lng").text(locator.current_location['coords']['longitude']); | |
} | |
} | |
}, | |
locationOK: function(position) { | |
// User allowed location sharing | |
locator.current_location = { | |
"coords": { | |
"latitude": position.coords.latitude, | |
"longitude": position.coords.longitude | |
} | |
,"type": "html5" | |
}; | |
$("#location-type").text(locator.current_location['type']); | |
$("#location-lat").text(locator.current_location['coords']['latitude']); | |
$("#location-lng").text(locator.current_location['coords']['longitude']); | |
}, | |
locationFAIL: function() { | |
// User denied location sharing | |
locator.current_location = { | |
"coords": { | |
"latitude": null, | |
"longitude": null | |
} | |
,"type": "denied" | |
}; | |
$("#location-type").text(locator.current_location['type']); | |
$("#location-lat").text(locator.current_location['coords']['latitude']); | |
$("#location-lng").text(locator.current_location['coords']['longitude']); | |
} | |
}; | |
$(document).ready(function() { | |
locator.getLocation(); | |
}); | |
/*! html file | |
<h1>Locator.js</h1> | |
<h2>Location - type</h2> | |
<p id="location-type"></p> | |
<h2>Location - position</h2> | |
<p>latitude: <span id="location-lat">/</span></p> | |
<p>longitude: <span id="location-lng">/</span></p> | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment