Last active
August 29, 2015 14:09
-
-
Save emoran/351b79f755d4bf171ef0 to your computer and use it in GitHub Desktop.
Allow use it when you need to get information about longitude and latitude from a trigger
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* A google Maps function to get Latitude and Longitude from Goole Maps API | |
* @author Edgar Yucel Morán Sanchez | |
* @version 1.0 | |
* @date 11/10/2014 | |
*/ | |
public class CL_GoogleMaps_Geolocations { | |
public CL_GoogleMaps_Geolocations() {} | |
Set<Id> id_accounts = new Set<Id>(); | |
/** | |
* | |
*/ | |
public void initLocation(Account[] accounts){ | |
System.debug('### Accounts: '+accounts); | |
for(Account acc:accounts){ | |
if(acc.Ubicaci_n__Latitude__s == null){ | |
id_accounts.add(acc.Id); | |
} | |
} | |
getLocation(id_accounts); | |
} | |
@future (callout=true) | |
public static void getLocation(Set<Id> setIdAccounts){ | |
System.debug('### Set cuentas: '+setIdAccounts); | |
List<Account> accounts_to_Update =new List<Account>(); | |
for(Account account:[Select Id,Name,BillingStreet,BillingCity,BillingState, | |
BillingPostalCode,BillingCountry | |
from Account where Id IN:setIdAccounts]){ | |
String address = ''; | |
if (account.BillingStreet != null) | |
address += account.BillingStreet +', '; | |
if (account.BillingCity != null) | |
address += account.BillingCity +', '; | |
if (account.BillingState != null) | |
address += account.BillingState +' '; | |
if (account.BillingPostalCode != null) | |
address += account.BillingPostalCode +', '; | |
if (account.BillingCountry != null) | |
address += account.BillingCountry; | |
address = EncodingUtil.urlEncode(address, 'UTF-8'); | |
//Build the callout | |
Http h = new Http(); | |
HttpRequest req = new HttpRequest(); | |
req.setEndpoint('http://maps.googleapis.com/maps/api/geocode/json?address='+address+'&sensor=false'); | |
req.setMethod('GET'); | |
req.setTimeout(60000); | |
try{ | |
HttpResponse res = h.send(req); | |
JSONParser parser = JSON.createParser(res.getBody()); | |
System.debug('### Response: '+res.getBody()); | |
//Map<Object,List<Object>> mapa =res.getBody(); | |
double lat = null; | |
double lon = null; | |
while (parser.nextToken() != null) { | |
if ((parser.getCurrentToken() == JSONToken.FIELD_NAME) && | |
(parser.getText() == 'location')){ | |
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(); | |
} | |
} | |
} | |
System.debug('LAT: '+lat); | |
System.debug('LON: '+lon); | |
if(lat!=null){ | |
account.Ubicaci_n__Latitude__s = lat; | |
account.Ubicaci_n__Longitude__s = lon; | |
update account; | |
} | |
} | |
catch(Exception er){ | |
System.debug('### ERROR: '+er.getMessage()); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment