How to decode address to coordinates with Google maps api
Hello
For do this simple operation you need implement next function:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | 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:
1 2 3 4 5 6 7 8 9 10 11 | 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, }); } }); |