Skip to content

Instantly share code, notes, and snippets.

@shorepound
Last active August 29, 2015 13:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shorepound/8937383 to your computer and use it in GitHub Desktop.
Save shorepound/8937383 to your computer and use it in GitHub Desktop.
function getLocation(){
if (navigator.geolocation){
navigator.geolocation.getCurrentPosition(getAddress);
} else{
alert("Geolocation is not supported by this browser.");
}
}
function getAddress(position){
var lat = position.coords.latitude;
var lon = position.coords.longitude;
var apiurl = 'http://maps.googleapis.com/maps/api/geocode/json?latlng='+lat+','+lon+'&sensor=true';
var xhr = new XMLHttpRequest();
xhr.open("GET", apiurl);
xhr.onload = function() {
if(this.status==200){
var data = JSON.parse(xhr.responseText);
var address = data.results[0]
putInDom(address.formatted_address);
} else {
info.innerHTML = "Could not find your location.";
}
}
xhr.send();
}
function putInDom(address){
var location = document.getElementById("location");
var a = address.split(",");
location.value = a[1].trim()+", "+a[2].trim().split(" ")[0];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment