Introduction
This article demonstrates using C# and MapPoint 2009 to look up a street address from a latitude/longitude. MapPoint does not use a Web Service. All data is stored locally, on the user's machine. From my research, MapPoint provides the most economical solution to perform a reverse geocode locally. Libraries from vendors such as ThinkGeo or GeoFrameworks cost upwards of $3000 while MapPoint costs $300.
Background
You can download the MapPoint North America 2009 trial from http://www.microsoft.com/downloads/details.aspx?FamilyID=60905dfe-5aea-44ec-b5fb-0e4130c3e7e5&DisplayLang=en. The download is a whopping 1.2GB, because it contains all of the geographic information for North America.
MapPoint does not include .NET assemblies, and can only be interfaced through COM. If you are my age (26), you may not have much experience using COM. The Windows SDK includes a tool called tlbimp.exe to generate a .NET assembly from a COM Type Library. The assembly, Interop.MapPoint.dll, is included in the code download, and you can generate it yourself with the command:
"C:\Program Files\Microsoft SDKs\Windows\v6.0A\bin\tlbimp.exe"
"C:\Program Files\Microsoft MapPoint 2009\MPNA83.tlb" /out:c:\Interop.MapPoint.dll
/namespace:Interop.MapPoint
For more information on COM, I recommend http://en.wikipedia.org/wiki/Component_Object_Model.
Using the code
The method Map.ObjectsFromPoint(int x, int y)
queries MapPoint for objects at a given latitude/longitude. This method returns street addresses, countries, restaurants, and anything else in the MapPoint database. We implement the method:
private StreetAddress LookupStreetAddress(Location loc)
{
FindResults arr = _map.ObjectsFromPoint(_map.LocationToX(loc), _map.LocationToY(loc));
return arr.OfType<location>().Where(o => o.StreetAddress
!= null).Select(o => o.StreetAddress).FirstOrDefault();
}
to cull the results for a street address. If location does not touch a street, it is unlikely that a street address will be returned. MapPoint does not include a method to find the nearest street address.
Our algorithm for GetNearestAddress
Streets seem to be around .0001 degrees in width, so our algorithm uses a grid of points .0001 degrees apart. We iterate through the points, from closest to farthest from the starting location, calling LookupStreetAddress
on each point until a match is found.
public StreetAddress GetNearestAddress(double lat, double lon)
{
if (lat == double.NaN || lon == double.NaN)
{
return null;
}
_map.GoToLatLong(lat, lon, 1);
for (int i = 0; i < 10; i++)
{
_map.ZoomIn();
}
StreetAddress addr;
for (int i = 0; i < 10; i++)
{
foreach (Location loc in GetPointsAround(lat, lon, i, .0001))
{
if ((addr = LookupStreetAddress(loc)) != null)
{
return addr;
}
}
}
return null;
}
And there you have it. I have used this with a GPS device and was able to accurately see my current address.