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 

getting latitude and logtitude data

I have two classes below used for retrieving data using a web call out - this worked when I ran below code in the developer console:
getGPS gps = new getGPS();
gps.getLng();
gps.getLat();

In Log I got longtitude and latitude whci is NOT HAPPENNING NOW FOR SOME REASON.
Can anyone shed a bit of light into this as this is giving me a headache, this ceased to work and I dont know why.

Appreciate your help, Milan

-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
public class getGPS {
    
    String endPointString = 'http://maps.googleapis.com/maps/api/geocode/json?address='Šafaříkova 785/1, 120 00  Praha, Vinohrady';
    double lng;
    double lat;
    
    public double getLng() {
        return lng;
        }
    
    public double getLat() {
        return lat;
        }
    
  

      public void getGPSdata() {
          Http httpProtocol = new Http();
          HttpRequest request = new HttpRequest();
          String endpoint = endPointString;
          request.setEndPoint(endpoint);
          request.setMethod('GET');
          HttpResponse response = httpProtocol.send(request); 
          String jsonString = response.getBody();
          googleAddress addr = googleAddress.parse(jsonString);
          
googleAddress.Location loc = addr.firstLoc;
double lng = loc.lng;
double lat = loc.lat;
          
          this.lng=lng;
          this.lat=lat;

      }
    
                       
}
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
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; 
        
        
    }
}
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Ajay mishraAjay mishra
Hi Milan,

I have also implementated the MAP API.

Which is working well in my scenario. which update record on Account.

Please find below code: 
string Geoservice ='https://maps.google.com/'+ StrAddress;
HttpRequest req = new HttpRequest(); 
Http http = new Http();
HTTPResponse res;
req.setEndPoint(Geoservice);
req.setTimeout(20000);
req.setMethod('GET');
res = http.send(req);
string response= res.getBody();
system.debug(response);
JSONParser parser = JSON.createParser(response);
         
while (parser.nextToken() != null) 
{
    if ((parser.getCurrentToken() == JSONToken.FIELD_NAME) && (parser.getText() == 'Location')) 
    {
        parser.nextToken();
        parser.nextToken();
        parser.nextToken();
        lat=parser.getText();
        parser.nextToken();
        parser.nextToken();
        lang= parser.getText();
    }
}

system.debug('Long and Lat  :- '+lat+'='+lang);



Thanks
Ajay Mishra
Email: mishraajay.m1@gmail.com
Milan HrdlickaMilan Hrdlicka
Hi Ajay, many thanks for your reply.
So should I replace my "googleAddress" class with this code?
This is giving me so much confusion :)
Sorry to be bothersome.
Milan