Finding Latitude / Longitude with Bing

I was recently commissioned to develop a series of upgrades to an admin system. An aspect of those upgrades included providing a mechanism for the user to obtain latitude and longitude values for any given UK address.

"Piece of cake," I thought. That was until I was told that instead of using my current favourite maps API in the form of Google Maps, I had to use Bing / Multimap instead.

Having never played around with the Bing API before, I delved into the documentation and examples. Whilst not being as easily accessible or thorough as the Google Maps documentation, I was able to produce a script that does exactly what the specification required: return latitude / longitude values for any given UK address.

Pre-Requisites

In order to use the Bing Maps API, I had to request an API key from them. This is a relatively simple process and they even have a dedicated website for it in the form of the Bing Maps Portal. In addition to HTML, PHP and Javascript, I also used the jQuery library.

HTML / PHP

First, I built a form whereby the user could input address details. The documentation stated that you can break down these details into constituent parts, namely: addressLine, locality, adminDistrict, postalCode and countryRegion.

In addition to the form, I also required two containing divs to be present to display the generated results from my query to the API.

I also added some styles to my page to prevent certain aspects of the produced map from displaying, such as the copyright notices, scale bar, compass and map type selectors.

Javascript

The javascript loops over the form elements and builds a URL string to send to the API server. The returned information is stored in the header of the page and then parsed by the GeocodeCallback function.

Initially, as there are no elements present in the form, a blank map of the UK is produced. To actually generate the map takes only a couple of lines of code. One to instantiate the Maps class, one to pass your API key, and one to centre the map over a particular area.

Once the form has been submitted and JSON returned, the javascript then iterates over the result pulling out all possible matches along with latitude and longitude values. Markers are also generated and laid over the map.

Conclusion and Demo

In my opinion, Bing maps are nowhere close to replacing Google Maps as my API of choice. However, playing around with the API has opened my eyes to an alternative, which does seem to be fairly easy to use and implement.

Click here to access the repository on BitBucket