function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Heather DeMalioHeather DeMalio 

How to populate the address using reverse geolocation

Hello, 

I am looking to populate the address fields by using the longitude and latitude coordinates. 

I can look this information up with Google Maps API but I don't know how to get this information into my Salesforce fields, 
Here is an example of google link https://maps.googleapis.com/maps/api/geocode/json?latlng=40.714224,-73.961452&result_type=street_address&key=AIzaSyB9SidQvZbhRlDax3Vvg8ak0hE0nxk7seo

Can anyone assist?

Thanks
-Heather
monsterloomismonsterloomis
Hi Heather,

There are a lot of ins and outs to geocoding and binding the results to a record, and a lot hinges on how your application works. If it's a UI-based application with a map, there are some differences to the patterns you might use to bind these returned values to your salesforce record. In a trigger, there are other considerations around making callouts, in that they have to be asynchronous. But before we get too far ahead, and in the spirit of jumping right in, here's a class you could create and invoke from an anonymous apex window in the console. Depending on the answers to the questions I've just asked, there is quite a bit that you'd have to do to this example to make it production-ready, but the goal here is to give you an idea of how it works. I've commented a bit to give you a sense for what's going on, but it should be enough to get you going - at least until the next question. :-)  (NOTE: see the comment below about remote site settings. It will fail unless you've set that up first. Setup --> Security --> Remote Site Settings): 
 
public class ReverseGeocoderExample {
    
    public static void doItAll() {
        
        Http h = new Http();
		HttpRequest req = new HttpRequest();
        req.setMethod('GET');
		// this will fail unless you set up https://maps.googleapis.com/ in your remote site settings
        req.setEndpoint('https://maps.googleapis.com/maps/api/geocode/json?latlng=40.714224,-73.961452&result_type=street_address&key=AIzaSyB9SidQvZbhRlDax3Vvg8ak0hE0nxk7seo');
		HttpResponse res = h.send(req);
        
		// create an account we'll use for this example
        Account a = new Account(
        	Name = 'Reverse Geocode Test'
        );

        // you can either deserialize to a class type, or untyped, as I've done here. 
        Map<String,Object> gr = (Map<String,Object>)JSON.deserializeUntyped(res.getBody());
		List<Object> results = (List<Object>)gr.get('results');
        Map<String,Object> firstResult = (Map<String,Object>)results[0]; // just taking the first one for this example
              
        // initialize your street address variables
        String streetNumber;
        String streetName;
        
		List<Object> firstAddressComponents = (List<Object>)firstResult.get('address_components'); 
        Map<String,Object> streetNumberComp = (Map<String,Object>)firstAddressComponents[0];
        streetNumber = String.valueOf(streetNumberComp.get('short_name'));
        Map<String,Object> streetNameComp = (Map<String,Object>)firstAddressComponents[1];
        streetName = String.valueOf(streetNameComp.get('short_name'));
        
        System.debug('Street address: ' + streetNumber + ' ' + streetName);
        a.BillingStreet = streetNumber + ' ' + streetName;

        insert a;
        System.debug('Account Id:' + a.Id); // so you can see the result
                
    }
    
}

You can call this using the console, like so:
 
ReverseGeocoderExample.doItAll();
Good luck! Hope this helps.

Dave (monsterloomis)