Where Am I And How Do I Get There

Tags: Tutorial, Google Maps, Geocoding, Reverse Geocoding

The geocoding namespace has gotten a major overhaul in this latest revision. There are many new features and many new classes to expose these features. However the changes also mean that some code may break, so be aware of this when upgrading.

The refactoring means that all geocoding results are delivered in a GoogleResult class. This class was previously only used for geocoding, but is now the default response for all services in the geocoding namespace. 

So what are these new features?

Reverse Geocoding

Reverse geocoding is one exciting new service that has been included.Recently Google announced reverse geocoding. So now you can click on a map and get the address. The map control has previously used the Geonames webservice to perform reverse geocoding. This was a fairly rudimentary service, which only reversed geocoded down to city level. Google's service delivers address information, so obviously that is now used.

To perform a reverse geocoding request simply call the ReverseGeocode method of the GoogleGeocoder (Reimers.Map.Geocoding.GoogleGeocoder), and either pass the latitude longitude coordinates as double values, or pass a GoogleLatLng object. This will return a GoogleResult object with the address if the search was successful.

C#

var geocoder = new GoogleGeocoder();
GoogleResult result = geocoder.ReverseGeocode(51.5, 0.0);

if (result.Status == GeocodeStatus.G_GEO_SUCCESS)
{
	foreach (Location loc in result.Locations)
	{
		Address returnedAddress = loc.Address;
	}
}

Directions

Now that you know where you are, you may want to know where you are going.

Apart from reverse geocoding the control also implements Google's directions service. Previously you could request a route and get a collection of overlays. This has been obsoleted by the new GoogleDirections class.While it may seem more complicated at first, it will give you total control over your routing directions.

The GoogleDirections class gives you information (summary, distance, duration and copyright info) about the total trip you have requested. It also contains an enumeration of routes (normally only one is returned). The route information is contained in the GoogleRoute class.

Like the GoogleDirections the GoogleRoute class gives you some information about the route. It also contains every step of the way as GoogleSteps. This class gives you the detailed driving or walking directions.

So instead of simply adding the returned overlays like before, you can now loop through the various steps on the route and present them as you want.

Serialization

If you want to make use of all these new features without having to post the page back to the server, the control now includes classes to help you with the JSON serialization/deserialization of the information. This means that you can serve the information from a ScriptService and use it directly in your clientside script.

You will find numerous JavaScriptConverters in the Reimers.Map.Json.Converters namespace. These can either be registered in your web.config file, like so:

XML

<jsonserialization>
	<converters>
		<add name="GoogleDirectionsConverter" type="Reimers.Map.Json.Converters.GoogleDirectionsConverter"/>
	</converters>
</jsonserialization>

Another way to access is from the newly exposed MapUtils class, that has a public JsonSerializer property that will give you a serializer with the converters already registered.

Latest Tweets