Wednesday, December 9, 2015

Geocoding address with Google Maps API

<script src="https://maps.googleapis.com/maps/api/js?sensor=false">
<script>
var geocoder;
function geocodeAddr(address) {
 if(geocoder == 'undefined' || geocoder == null) {
  geocoder = new google.maps.Geocoder();
 }
 geocoder.geocode( { 'address': address}, function(results, status) {
 if (status == google.maps.GeocoderStatus.OK) {
  alert('Latitude: ' + results[0].geometry.location.lat());
  alert('Longitude: ' + results[0].geometry.location.lng());
 } else {
   alert('Geocode was not successful for the following reason: ' + status);
 }
  });
}
</script>

<input type="button" onclick="geocodeAddr('Cameron Highlands Resort,By The Golf Course, 39000 Tanah Rata,Cameron Highlands, Pahang, Malaysia')" />

With the geocodeAddr function above, the latitude and longitude of the given address will be alerted.


Done!!

No comments:

Post a Comment

LinkWithin

Related Posts Plugin for WordPress, Blogger...