If a user inputs an address, I want to convert to the equivalent LatLng.
I've read the documentation, and I think I can use the Geocoder class to do this, but can't figure out how to implement it.
Thanks for any help!
There is a last trick to get Address from Lat-Long (Geo-coordinates). You can simply hit google-maps web service passing the Latitude and longitude. It is simply a GET-Method web-service.
There is a pretty good example on https://developers.google.com/maps/documentation/javascript/examples/geocoding-simple
To shorten it up a little:
geocoder = new google.maps.Geocoder(); function codeAddress() { //In this case it gets the address from an element on the page, but obviously you could just pass it to the method instead var address = document.getElementById( 'address' ).value; geocoder.geocode( { 'address' : address }, function( results, status ) { if( status == google.maps.GeocoderStatus.OK ) { //In this case it creates a marker, but you can get the lat and lng from the location.LatLng map.setCenter( results[0].geometry.location ); var marker = new google.maps.Marker( { map : map, position: results[0].geometry.location } ); } else { alert( 'Geocode was not successful for the following reason: ' + status ); } } ); }
I don't think location.LatLng
is working, however this works:
results[0].geometry.location.lat(), results[0].geometry.location.lng()
Found it while exploring Get Lat Lon source code.
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