Skip to content

Instantly share code, notes, and snippets.

@SalesforceBobLightning
Last active May 31, 2018 22:52
Show Gist options
  • Save SalesforceBobLightning/2a17591087603a4a2af58a398333bb17 to your computer and use it in GitHub Desktop.
Save SalesforceBobLightning/2a17591087603a4a2af58a398333bb17 to your computer and use it in GitHub Desktop.
Using Google Maps Geocode API with Salesforce Apex
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