Google Geocoding from Visualforce

August 9, 2008 Appirio

Kyle
Roche

Mashups are becoming a common part of most implementations. Replacing legacy applications are sometimes phased into adoption by creating a mashup in Salesforce.com of the current application(s) and slowly replacing components as they are reconstructed using native Salesforce. Google Maps Mashups are among the most popular. However, we found most of the examples were either using the AJAX toolkit or were hard coding the Lattitude / Longitude coordinates. In this example, we’ll demonstrate how to use Google’s Geocoding API to geocode an Account address from with your Visualforce controller. The key difference is that this example geocodes the address using server side scripting.

Start off by creating a new APEX Class called GoogleGeocodeExtension. This will be our controller extension. Remember, controller extensions have constructors that take an argument of controller for which they are extending. In this example, we’ll be extending the standard controller for the Account object. Make sure your class looks like the following.

public class GoogleGeocodeExtension {
private final Account acct;

public GoogleGeocodeExtension (ApexPages.StandardController stdController) {
this.acct = (Account)stdController.getRecord();
}
}

Google’s Geocoding API can be accessed via server side scripting. You can choose different output formats like XML, CSV, JSON (default). In our case, we’ll keep things simple and return the results in CSV format. Add the following property to your APEX class.

public string[] Coordinates
{
get
{
if (Coordinates == null)
{
Account myAccount = [select name,billingstreet,billingcity,billingstate,billingpostalcode from Account where id=:acct.id];
String url = ‘http://maps.google.com/maps/geo?’;
url += ‘q=’ + EncodingUtil.urlEncode(myAccount.BillingStreet,’UTF-8′) + ‘,’ + EncodingUtil.urlEncode(myAccount.BillingCity,’UTF-8′) + ‘,’ + myAccount.BillingState;
url += ‘&output=csv&key=yourgooglemapkeyhere’;

Http h = new Http();
HttpRequest req = new HttpRequest();

req.setHeader(‘Content-type’, ‘application/x-www-form-urlencoded’);
req.setHeader(‘Content-length’, ‘0’);
req.setEndpoint(url);
req.setMethod(‘POST’);

HttpResponse res = h.send(req);
String responseBody = res.getBody();
Coordinates = responseBody.split(‘,’,0);
}
return Coordinates;
}
set;
}

This APEX property queries the billing address for the Account record and passes it to the Google Geocoding API. Because spaces and other special characters can appear in addresses and city names we need to use the urlEncode() method to properly format these strings.

We chose to use the CSV format on our response. So, we simply need to split the string by the comma delimiter so we can access each field individually. To keep things simple you can add the following two properties to your controller extension.

public string CoordinateLat { get { return Coordinates[2]; } }
public string CoordinateLong { get { return Coordinates[3]; } }

Like any other APEX property in a controller or extension you can access these in your Visualforce page using the standard binding syntax {!CoordinateLat}.

Previous Article
FIrst look at Dynamic APEX
FIrst look at Dynamic APEX

Kyle Roche I opened a case in my Dev org a few days ago to request that Dynamic APEX be enabled. I took my ...

Next Article
Extending Visualforce's UI – Ext JS DataGrid
Extending Visualforce's UI – Ext JS DataGrid

Frank Hansen Kyle Roche In this posting we’re going to use the Ext JS library to build out a custom DataGri...