How to decode address to coordinates with Google maps api
Hello
For do this simple operation you need implement next function:
function address_func(addresses, callback) { var coords = []; for(var i = 0; i < addresses.length; i++) { currAddress = addresses[i]; var geocoder = new google.maps.Geocoder(); if (geocoder) { geocoder.geocode({'address':currAddress}, function (results, status) { if (status == google.maps.GeocoderStatus.OK) { coords.push(results[0].geometry.location); if(coords.length == addresses.length) { if( typeof callback == 'function' ) { callback(coords); } } } else { throw('No results found: ' + status); } }); } } }
And use this function as:
var address_array = ["514 Broadway, Brooklyn, New York, NY, United States"] address_func([select_val], function(results){ for(marker in results) { var marker_lat_lng = new google.maps.LatLng(results[marker].lat(), results[marker].lng()); var marker = new google.maps.Marker({ position: marker_lat_lng, map: map, }); } });