The Poorhouse recently had to convert thousands of UK postcodes into their equivalent latitudes and longitudes. Much as atlases plus rulers are fun, it got tedious quickly. Luckily there was a copy of Microsoft Mappoint Europe 2006 lying around nearby, and it turns out it is pretty much super easy to do using its API.
The program was developed in Microsoft Access to make it easy to feed a decent sized amount of data into the resulting application, but you could do it via any sort of VBA-enabled program at the very least. First thing to do is to include a reference to the Mappoint object library. In the VBA screen, using the Tools -> References menu, ensure you have a tick in the relevant box. In this case it's called "Microsoft MapPoint 13.0 Object Library (Europe)".

Now you have easy access to all the objects you need.
Create and prepare variables to hold the Mappoint objects Application, Map, Location, and FindResults. The names are pretty much self-explanatory.
Dim objApp As New MapPoint.Application
Dim objMap As MapPoint.Map
Dim objLoc As MapPoint.Location
Dim objFR As MapPoint.FindResultsYou can use the ActiveMap property of the Mappoint Application to doing your postcode searching on.
Set objMap = objApp.ActiveMapNow the one-liner that actually does the key work. The Mappoint Map object contains a method called FindAddressResults. Feed it an address and it will return set of Location objects that it considers are matches for the address. This is good, because a Location object is exactly what you need to get information on manipulate a specific place within Mappoint.
The syntax is:
object.FindAddressResults([Street], [City], [OtherCity], [Region], [PostalCode], [Country])Here we were just interested in finding a location as a result of a postcode, so the first few parameters were irrelevant. To search for, say, the postcode "SW2 3BL", you could therefore theoretically do:
Set objLoc = objMap.FindAddressResults(, , , , "SW2 3BL", geoCountryUnitedKingdom)(1)Note that this version of Mappoint does not appear to be able to find postcodes in Jersey, Guernsey or the Isle of Man, so there's no point in looking them up.
Hopefully, if successful, you will have been returned a single valid Location, hence the above setting the Location object called "objLoc" to the first object in the collection returned by FindAddressResults. You can't always guarantee this will be the case though, so even though it's a tad more typing you can use an intermediate FindResults step to check if you're going to get a valid result with the above code.
The FindResults object is a collection of Locations (or Pushpins) that has a property called ResultsQuality that, believe it or not, tells you the perceived quality of the results Mappoint has given you for your search. If it is a 1, or the constant "geoFirstResultGood", you are largely good to go and use this for whatever you want. Otherwise you may want to alert your user / program / whatever that you may have found nothing, nothing very precise or an ambiguous multitude of results rather than use the first result as a definitive source.
Here's one way of checking it, rather than risking that everything will be OK as is the case with the above code:
Set objFR = objMap.FindAddressResults(, , , , "SW2 3BL", geoCountryUnitedKingdom)
Select Case objFR.ResultsQuality
Case geoFirstResultGood
' go ahead and use it
Case geoAmbiguousResults
' warn ambiguous
Case geoNoGoodResult
' warn no good result found
Case geoNoResults
' warn no results at all found
End SelectOnce you have established you have got a good enough result to use, simply grab it as a Location object, and use the in-built Longitude and Latitude properties to get the relevant figures you need. For example, to display them in an annoying little message-box:
Set objLoc = objMap.FindAddressResults(, , , , "SW2 3BL", geoCountryUnitedKingdom)(1)
Msgbox "Latitude of SW2 3BL is " & objLoc.Latitude & ". Longitude is " & objLoc.LongitudeNot so hard is it! So the final Poor-app simply took an Access recordset of postcodes, looped them through the above process, and recorded the results into a second recordset for later review. Much more fun that Googlemapping 5000 locations one by one.

Comments
Converting a list of postcodes to Lat+Lon using MP2006
Awesome post - works brilliantly.
Do you have any tips on how I can modify this code to add a couple of columns to a table of addresses in excel with the latitude and longditude?
Thanks
AK
Hi, Glad it's helpful! I'm
Hi,
Glad it's helpful!
I'm not as familiar with coding VBA in Excel as Acccess, but given I believe they both have the same facilities etc. I reckon you could use the exact same code, being sure to put the reference to the Mappoint object library in.
Then if you know how to read and write cells in VBA, I guess loop through your addresses storing the names in a VBA variable, run the above code to change into co-ordinates storing the co-ordinates as a variable, and then output it into a different cell.
I have a little Access program that loops through a table adding co-ordinates to addresses like this, using the recordset object, so I reckon it would work just fine with Excel, but instead of the code that manipulates the recordset object you'd write it to input and output from predefined regions of an Excel sheet?
Converting a list of postcodes to Lat+Lon using MP2006
Hi there
Yes this code worked fine in Excel - in fact, that's all I've used it in. Your proposed solution is exactly the way to go.
I'm more literate in Excel, but want to be able to give this code to someone who will use it in Access eventually, so at some point I'm going to need to write something v. similar / same as your Access macro that uses the recordset.
but before I start on that problem I have a more pressing one: I can't seem to make this code work for the Netherlands. Switching the geoCountry over to "Netherlands" is ok, but as soon as I enter a Netherlands postcode in the [postcode] field, I get "Runtime Error '-2147181454 (80049c72)': Automation error". Any ideas what's causing this?
Once it's working, should we post both of the macros? Would be useful to give people a version in Access + Excel, and I don't have a website...
Cheers
Aly
Hi, good idea, am away from
Hi, good idea, am away from main computer at present, but there's no reason I can't put my Access version up when back - perhaps it will meet your needs or at least with the knowledge you have you can adapt it to whatever you want. It's nothing fancy, just an internal tool I use here and there at work. If you also have a version to share in Excel or Access and are happy to put it up here then great, that would be fantastic, thanks.
Regarding the Netherlands, I have a vague memory that there's an error in some documentation file that might explain this. If you tried to set it to
"geoCountryNetherlands", try instead to set it to "geoCountryTheNetherlands", or vice versa. If that doesn't work, try using the numeric code 176 instead.
Those error messages are ridiculous aren't they?! They are not in any way helpful to diagnose the problem.
PS: Sorry your comments keep getting marked as spam! The spam filter seems to have gone beserk.
Hi - as promised, I put my
Hi - as promised, I put my simple Access application up for download, see it here.
Hope it's helpful, would enjoy seeing your Excel solution (or improvements to Access) when you have it!
Post new comment