0

I am using the GoogleMaps JS Api and I'm looking to cleanse addresses. Is it possible for me to call this via a controller or do I need to do it within the browser?

I can use this to pass parameters but I'm unsure how to do that server-side (nor client side for that matter):

https://developers.google.com/maps/documentation/geocoding/start#ReverseGeocoding

1

Yes, you can do so. This GitHub repo gives an example of doing so. Here's a snapshot of the code at the time of this answer:

public class LocationCallouts {

     @future (callout=true)  // future method needed to run callouts from Triggers
      static public void getLocation(id accountId){
        // gather account info
        Account a = [SELECT BillingCity,BillingCountry,BillingPostalCode,BillingState,BillingStreet FROM Account WHERE id =: accountId];

        // create an address string
        String address = '';
        if (a.BillingStreet != null)
            address += a.BillingStreet +', ';
        if (a.BillingCity != null)
            address += a.BillingCity +', ';
        if (a.BillingState != null)
            address += a.BillingState +' ';
        if (a.BillingPostalCode != null)
            address += a.BillingPostalCode +', ';
        if (a.BillingCountry != null)
            address += a.BillingCountry;

        address = EncodingUtil.urlEncode(address, 'UTF-8');

        // build callout
        Http h = new Http();
        HttpRequest req = new HttpRequest();
        req.setEndpoint('http://maps.googleapis.com/maps/api/geocode/json?address='+address+'&sensor=false');
        req.setMethod('GET');
        req.setTimeout(60000);

        try{  
            // callout
            HttpResponse res = h.send(req);

            // parse coordinates from response
            JSONParser parser = JSON.createParser(res.getBody());
            double lat = null;
            double lon = null;
            while (parser.nextToken() != null) {
                if ((parser.getCurrentToken() == JSONToken.FIELD_NAME) && 
                    (parser.getText() == 'location')){
                       parser.nextToken(); // object start
                       while (parser.nextToken() != JSONToken.END_OBJECT){
                           String txt = parser.getText();
                           parser.nextToken();
                           if (txt == 'lat')
                               lat = parser.getDoubleValue();
                           else if (txt == 'lng')
                               lon = parser.getDoubleValue();
                       }

                }
            }

            // update coordinates if we get back 
            if (lat != null){
                a.Location__Latitude__s = lat;
                a.Location__Longitude__s = lon;
                update a;        
            }    

        } catch (Exception e) {
        }   
    }
}

Note: This is not my code, but is simple enough that you should be able to use it almost verbatim. In a controller, you won't need a future method as long as you remember to use it before using a DML statement.

Credit goes to the author of the code.

2
  • very nice! I'm trying to do the inverse right now Geo to address and I'm trying to figure out the parsing logic. – tjcinnamon Jul 22 '16 at 20:40
  • @tjcinnamon Just take the JSON example in the docs, head to json2apex, generate your class from the JSON, then you can parse it using the provided method. – sfdcfox Jul 22 '16 at 21:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.