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
Milan HrdlickaMilan Hrdlicka 

Data to be filled automatically once other details are entered

Hello,

I have a custom object "Shop__c". When I enter address details to appropriate fields, once these are entered I need t latitude and longtitude details (geolocation) to be filled automatically.
Longtitude and Latitude data are placed in a variable in separate class.
Can anyone advise what would be the best to achieve this?
Appreciate your help,
Milan
 
Alain CabonAlain Cabon
Hi,

What do you use for getting the latitude/longitude values from an address?

Close solution: https://developer.salesforce.com/forums/?id=906F00000009EKmIAM

Alain
Milan HrdlickaMilan Hrdlicka
Hi Alain, I am using json agoogle map service to retrieve the data. Thanks.

class1:
-----------

public class googleAddress {

    public class Address_components {
        public String long_name;
        public String short_name;
        public List<String> types;
    }

    public class Geometry {
        public Location location;
        public String location_type;
        public Viewport viewport;
    }

    public List<Results> results;
    public String status;
    public Location firstLoc;

    public class Results {
        public List<Address_components> address_components;
        public String formatted_address;
        public Geometry geometry;
        public Boolean partial_match;
        public String place_id;
        public List<String> types;
    }

    public class Viewport {
        public Location northeast;
        public Location southwest;
    }

    public class Location {
        public Double lat;
        public Double lng;
    }

    
    public static googleAddress parse(String json) {
        
        googleAddress returnAddr;
        
        returnAddr = (googleAddress) System.JSON.deserialize(json, googleAddress.class);
        if (!returnAddr.results.isEmpty()){
            returnAddr.firstLoc = returnAddr.results[0].geometry.location;
        }
        return returnAddr; 
        
        
    }
}

class2:
-----------

public class JSONpokus1 {
    
    @future(callout=true)
      public static void parseJSONResponse() {
          Http httpProtocol = new Http();
          HttpRequest request = new HttpRequest();
          String endpoint = 'http://maps.googleapis.com/maps/api/geocode/json?address=Nuselská 23, 140 00, Praha 4';
          request.setEndPoint(endpoint);
          request.setMethod('GET');
          HttpResponse response = httpProtocol.send(request);
          String jsonString = response.getBody();
          System.debug(jsonString);
          googleAddress addr = googleAddress.parse(jsonString);
          
googleAddress.Location loc = addr.firstLoc;
double lng = loc.lng;
double lat = loc.lat;
          
          system.debug(lng);
          system.debug(lat);
      }
    

    

                       
}

 
Alain CabonAlain Cabon
Ok, your problem is the implementation of asynchronous callouts wich work through a pattern called a continuation (for updating the data) because you are using @future ?

https://developer.salesforce.com/blogs/developer-relations/2015/02/apex-continuations-asynchronous-callouts-visualforce-pages.html

But it is not necessary if you use a Visualforce page and an Apex controller.
https://developer.salesforce.com/docs/atlas.en-us.pages.meta/pages/pages_mobile_example.htm

<apex:page controller="mapController" showHeader="false">

Alain