Skip to content

Instantly share code, notes, and snippets.

@cliffordp
Created April 9, 2022 03:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cliffordp/94cfb945b679b7567ede2a0e2f39e33c to your computer and use it in GitHub Desktop.
Save cliffordp/94cfb945b679b7567ede2a0e2f39e33c to your computer and use it in GitHub Desktop.
Zoho Flow custom function to get address from Google Maps geocoding
map addressToCoordinates(string addressString)
{
/**
* This snippet: https://gist.github.com/cliffordp/068b32913ba25d1cd998507398c950c8
*
* Get lat/long from Google.
* https://developers.google.com/maps/documentation/geocoding
*
* CRM limits Decimal Fields to 9 digits, which is sufficient:
* https://rapidlasso.com/2019/05/06/how-many-decimal-digits-for-storing-longitude-latitude/
*
* Deluge has its own available but may not be as accurate:
* https://www.zoho.com/deluge/help/map-tasks/geocode.html
*/
geoUrl = 'https://maps.googleapis.com/maps/api/geocode/json?key=___YOUR_API_KEY___&region=us&bounds=' + zoho.encryption.urlencode('37.169071,-100.206985|33.385586,-93.966751') + '&address=' + zoho.encryption.urlencode(addressString);
// https://www.zoho.com/deluge/help/webhook/invokeurl-api-task.html
response = invokeurl
[
url :geoUrl
type :POST
];
//info response;
valueMap = Map();
if('OK' == response.get('status'))
{
results = response.get('results').toCollection();
valueMap.put('resFormattedAddress',replaceFirst(results.get('formatted_address'),', USA',''));
valueMap.put('resLat',round(results.get('geometry').get('location').get('lat').toDecimal(),9));
valueMap.put('resLng',round(results.get('geometry').get('location').get('lng').toDecimal(),9));
}
// In Zoho Flow, do something like this: ${addressToCoordinates.resFormattedAddress}
return valueMap;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment