Last active
August 29, 2015 13:56
-
-
Save shorepound/8937383 to your computer and use it in GitHub Desktop.
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
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