You need to sign in to do that
Don't have an account?

How to return values from this class
Hello,
How can I return lat and lon from the class? I am passing Address to the getLatitude_Longitude method and would like to return the lat and lon.
How can I return lat and lon from the class? I am passing Address to the getLatitude_Longitude method and would like to return the lat and lon.
public class GetMapLocation { @future (callout=true) static public void getLatitude_Longitude(String Address){ Address = EncodingUtil.urlEncode(Address, 'UTF-8'); // build callout Http h = new Http(); HttpRequest req = new HttpRequest(); String yourAPiKey = 'MyKey'; // Enter your Api key which you have created in google api console String key = '&key=' + yourAPiKey; String endPoint = 'https://maps.googleapis.com/maps/api/geocode/json?address=' + Address; req.setEndpoint(endPoint + key); req.setMethod('GET'); req.setTimeout(5000); System.debug('Request ' + req); try{ // callout HttpResponse res = h.send(req); // parse coordinates from response JSONParser parser = JSON.createParser(res.getBody()); system.debug('Response get Body ' + res.getBody()); double lat = null; double lon = null; //system.debug(parser.nextToken()); while (parser.nextToken() != null) { system.debug(parser.getCurrentToken()); system.debug(JSONToken.FIELD_NAME); if ((parser.getCurrentToken() == JSONToken.FIELD_NAME) && (parser.getText() == 'location')){ // system.debug('a'); 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 system.debug(lat); if (lat != null){ system.debug(lat+' '+lon); } } catch (Exception e) { system.debug(e); } } }
static public String getLatitude_Longitude(String Address){
string result = '';
.....
if ((parser.getCurrentToken() == JSONToken.FIELD_NAME) &&
(parser.getText() == 'location')){
// system.debug('a');
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();
result = string.valueof(lat) + ',' + string.valueOf(lont);
}
}
return result;
}
Or you can create an inner class:
public class LatLonClass
{
public double Lat {get;set;}
public double Lon {get;set;}
}
static public LatLonClass getLatitude_Longitude(String Address){
LatLonClass result = new LatLonClass ();
.....
if (txt == 'lat')
lat = parser.getDoubleValue();
else if (txt == 'lng')
lon = parser.getDoubleValue();
result.lat = lat;
result.lon = lon;
return result;
}
All Answers
static public String getLatitude_Longitude(String Address){
string result = '';
.....
if ((parser.getCurrentToken() == JSONToken.FIELD_NAME) &&
(parser.getText() == 'location')){
// system.debug('a');
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();
result = string.valueof(lat) + ',' + string.valueOf(lont);
}
}
return result;
}
Or you can create an inner class:
public class LatLonClass
{
public double Lat {get;set;}
public double Lon {get;set;}
}
static public LatLonClass getLatitude_Longitude(String Address){
LatLonClass result = new LatLonClass ();
.....
if (txt == 'lat')
lat = parser.getDoubleValue();
else if (txt == 'lng')
lon = parser.getDoubleValue();
result.lat = lat;
result.lon = lon;
return result;
}
Thank you.
I am getting this following error: Future methods do not support return type of String
I have a @future (Callout=true) as I am calling this class from a trigger.
The reason is Callout method is running in a different thread. It is in fire and forget mode. So trigger does not wait the callout completion.
In this case, we usually write the lat and lon value to a Salesforce object through DML.