I have a list of areas and lattitude/longitude which I'm asking the user to select their location when they hit my site. I'd like to have their location prefilled using HTML5's geolocation if possible, but I'm not exactly sure the best way to do that. There seems to be a lack of tutorials on the web, at least from what I can find. Has anyone done this? Do you know of a good tutorial/resource?
update
If I have coordinates of bunch of cities, how can I use javascript to determine the closest location?
If supported, run the getCurrentPosition() method. If not, display a message to the user. If the getCurrentPosition() method is successful, it returns a coordinates object to the function specified in the parameter (showPosition) The showPosition() function outputs the Latitude and Longitude.
HTML5 Geolocation API lets you share your location with your favorite web sites. A JavaScript can capture your latitude and longitude and can be sent to backend web server and do fancy location-aware things like finding local businesses or showing your location on a map. var geolocation = navigator.
HTML5 Geolocation. The Geolocation is one of the best HTML5 API which is used to identify the user's geographic location for the web application. Most of the geolocation services use Network routing addresses such as IP addresses, RFID, WIFI and MAC addresses or internal GPS devices to identify the user's location.
So yes, getting a user's lat/lon is 'free'--no need to pay for that feature by itself.
Try this example:
window.onload = function(){
if(navigator.geolocation)
navigator.geolocation.getCurrentPosition(handleGetCurrentPosition, onError);
}
function handleGetCurrentPosition(location){
location.coords.latitude;
location.coords.longitude;
}
function onError(){...}
Go here for a larger demo. http://od-eon.com/labs/geolocation/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With