Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save alkema/871263 to your computer and use it in GitHub Desktop.
Save alkema/871263 to your computer and use it in GitHub Desktop.
grab lat + lng from js, geocode and then fill in input as formatted address
<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false"></script>
<script>
var geocoder = new google.maps.Geocoder();
function success(position) {
var latlng = new google.maps.LatLng(position.coords.latitude, position.coords.longitude);
geocoder.geocode({'latLng' : latlng}, function(results, status){
$('#saddr').val(results[0].formatted_address);
});
}
function error(msg) {
var s = document.querySelector('#status');
s.innerHTML = typeof msg == 'string' ? msg : "failed";
s.className = 'fail';
// console.log(arguments);
}
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(success, error);
} else {
error('not supported');
}
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment