I am trying to make a TimeZone class that does the process to get a time zone. I am using the Google API's. In order to get the timezone you have to make a callout to the Google Geocode API and get the latitude and longitude coordinates. Then you have to use those coordinates to call the Google TimeZone API to receive the timezone. Right now I have a trigger that fires an ID to a class1. The class1 then calls my TimeZoneclass2. I have tried multiple ways to work around so that I can receive my timezone inside class1 after calling TimeZoneclass2. I keep getting null values returned and I think its because my class2 moves on to the next lines of code, while the TimeZoneclass2 is waiting for the response from the callouts, so therefore i get 'null' values. Any help would be appreciated. I've going on the third day of trying to get this to work for me. Thank you!
public class TimeZoneUtility
{
public static String Latitude {get; set;}
public static String Longitude {get; set;}
public static String TimeZone {get; set;}
public static void returnTimeZone(String address, String city, String state)
{
getGeoCode(address, city, state);
System.debug('Derrick: Lat: ' + Latitude);
System.debug('Derrick: LNG: ' + Longitude);
getTimeZone(Latitude, Longitude);
System.debug('Derrick: TimeZone: ' + TimeZone);
}
@future(callout = true)
public static void getGeoCode(String address, String city, String state)
{
address = address.replace(' ', '+');
city = city.replace(' ', '+');
state = state.replace(' ', '+');
String endpoint = 'https://maps.googleapis.com/maps/api/geocode/xml?address=' +address+ ',+' +city+ ',+' +state+ '&key=AIzaSyBGiCxIWQVjorNHlbTCA1-U-3XALdJ_Qb8';
// Instantiate a new http object
Http h = new Http();
// Instantiate a new HTTP request, specify the method (GET) as well as the endpoint
HttpRequest req = new HttpRequest();
req.setEndpoint(endpoint);
req.setMethod('GET');
// Send the request, and return a response
HttpResponse res = h.send(req);
Dom.Document doc = new Dom.Document();
doc.load(res.getBody());
Dom.XMLNode xroot = doc.getRootElement();
String status = xroot.getChildElement('status', null).getText();
Dom.XMLNode xrCharge = xroot.getChildElement('result', null).getChildElement('geometry', null).getChildElement('location', null);
Latitude = xrCharge.getChildElement('lat', null).getText();
Longitude = xrCharge.getChildElement('lng', null).getText();
System.debug('Derrick: LAT: ' + Latitude);
System.debug('Derrick: LNG: ' + Longitude);
}
@future(callout = true)
public static void getTimeZone(String lat, String lng)
{
System.debug('Derrick: LAT: ' + lat);
System.debug('Derrick: LNG: ' + lng);
String endpoint = 'https://maps.googleapis.com/maps/api/timezone/xml?location=' +lat+ ',' +lng+ '×tamp=1331161200&key=AIzaSyDu60hEW-wF2d2FhOS-xCszEAtSSWjbZYk';
// Instantiate a new http object
Http h = new Http();
// Instantiate a new HTTP request, specify the method (GET) as well as the endpoint
HttpRequest req = new HttpRequest();
req.setEndpoint(endpoint);
req.setMethod('GET');
//req.setMethod('POST'); // sets the type of method to be used for the HTTP request
//req.setBody(body);
// Send the request, and return a response
HttpResponse res = h.send(req);
//System.debug(res.getBody());
Dom.Document doc = new Dom.Document();
doc.load(res.getBody());
Dom.XMLNode xroot = doc.getRootElement();
TimeZone = xroot.getChildElement('time_zone_name', null).getText();
System.debug('Derrick: TimeZone: ' + TimeZone);
}