Created
March 27, 2020 08:38
-
-
Save gauravmehla/dbf154de28d37d3ae8c691d806313526 to your computer and use it in GitHub Desktop.
A simple snippet to get co-ordinates of array of address's.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* Get the latitude and longitude from address: | |
Author : Bastin Robins J | |
Modified By : Gaurav Mehla | |
*/ | |
// Add the link to webpage | |
<script src="https://maps.googleapis.com/maps/api/js?key=API_KEY_HERE" type="text/javascript"></script> | |
<script> | |
(async function(){ | |
// List of addressed you want to get co-ordinates of | |
let address = [ | |
'New York', | |
'Nariman Point, Mumbai, India' | |
] | |
//Function to covert address to Latitude and Longitude | |
var getLocation = function(address) { | |
return new Promise(function(resolve, reject){ | |
var geocoder = new google.maps.Geocoder(); | |
geocoder.geocode( { 'address': address}, function(results, status) { | |
if (status == google.maps.GeocoderStatus.OK) { | |
var latitude = results[0].geometry.location.lat(); | |
var longitude = results[0].geometry.location.lng(); | |
setTimeout(() => { | |
resolve({latitude, longitude}); | |
}, 500); // 500ms wait for API Quota Limit | |
} | |
}); | |
}); | |
} | |
for (let index = 0; index < address.length; index++) { | |
//Call the function with address as parameter | |
let res = await getLocation(address[index]); | |
console.log(index + 1, JSON.stringify(res)); | |
} | |
})(); | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment