Last active
May 31, 2018 22:52
-
-
Save SalesforceBobLightning/2a17591087603a4a2af58a398333bb17 to your computer and use it in GitHub Desktop.
Using Google Maps Geocode API with Salesforce Apex
This file contains 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
public class GoogleMapsGeocodeApi { | |
public string geocode(String address){ | |
String url = 'https://maps.googleapis.com/maps/api/geocode/json?address=' | |
+ EncodingUtil.urlEncode(address, 'UTF-8') | |
+ '&components=country:GB' | |
+ '&key=' + getGoogleMapsAPIKey(); | |
return getHttp(url); | |
} | |
private string getHttp(string url) { | |
Http h = new Http(); | |
HttpRequest req = new HttpRequest(); | |
HttpResponse res = new HttpResponse(); | |
req.setMethod('GET'); | |
req.setEndpoint(url); | |
req.setTimeout(120000); | |
res = h.send(req); | |
return res.getBody(); | |
} | |
private string getGoogleMapsAPIKey() { | |
return ''; // get key from custom settings | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment