How to Geocode a Street Address in JavaScript
Many businesses, such as the various ride-sharing and food/product delivery companies that have popped up, rely on precise geo-mapping to successfully perform their services. Due to this reliance, accurate location data using latitude and longitude is key. The following API will allow you to geocode any street address to return its latitude and longitude; much as the aforementioned businesses require exact data, this API requires inputs for the street address, city, and state or province in order to run smoothly.
To use this API in JavaScript, we will first install the jQuery library:
bower install jquery
Once the installation is complete, we can call the function:
var settings = {
"url": "https://api.cloudmersive.com/validate/address/geocode",
"method": "POST",
"timeout": 0,
"headers": {
"Content-Type": "application/json",
"Apikey": "YOUR-API-KEY-HERE"
},
"data": JSON.stringify({
"StreetAddress": "<string>",
"City": "<string>",
"StateOrProvince": "<string>",
"PostalCode": "<string>",
"CountryFullName": "<string>",
"CountryCode": "<string>"
}),
};$.ajax(settings).done(function (response) {
console.log(response);
});
This will validate the address and return the latitude and longitude as promised. To retrieve your personal API key and gain access to 800 monthly calls, simply register for a free account on the Cloudmersive website.