Oh my god. It's full of code!

Apex Google Powered Navigation Class

Hey all,
Expanding on what I was talking about yesterday I have packaged my google maps navigation tool into an apex class. Using this class you can easily get directions to and from any address using the tried and tested google maps system. Simply provide the origin, destination, desired data return format, and a toggle indicating if you want to expand abbreviations in the directions (like rd to road, nw to northwest, dr to drive, etc).

You have several options for how you want the data to be returned. You can either return the raw xml data from google (kml as they call it) get a nice JSON data structure, a semi-colon separated list, or a semi-colon separated list that is url formated. Invoking the class is super easy.

String directions = getDirections.getDirections('address 1','address 2','dataReturnType', expandAbbreviations);

One thing that I dislike about the current incarnation is that the XML parsing is a bit slow/inefficient. I am not very strong on my XML-fu so currently it must iterate over every XML element to find the desired data. It would be faster to tell it more accuratly where to look, but I don’t really know how. So if you are a smarty pants developer and know how to make the search faster, please feel free to let me know. I’ll be happy to update it and credit you for the fix.

With this tool, you could easily use it as a controller for a visualforce page, and create your own RESTful style webservice, or use it to help build other neat services. It’s a nice developer resource I think.

Download it here or from the projects page.

NOTE: To use this class you will have to add http://maps.google.com to your remote sites in your org. Just go to setup->security->remote sites and add the url.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s