Add Bing address to Azure Mobile Services data

For a project a use Azure Mobile Services to store GPS locations. But GPS locations are very hard to read – well, unless your debugging it all the time, then GPS locations start to mean something. The more human readable form is an address. To obtain an address for a GPS location you can use the Bing Service to do reverse geo-encoding. I wanted to do this automatically in my INSERT node.js script in the Azure Mobile Service.

As it took me some research to find the right solution, I thought it might be nice to share it with the world 🙂 So here is what I did…

First I created a mobile service to store data in the Azure portal (for more information, see for instance this article Get Started with Mobile Services for Windows Store Apps). That wasn’t the hard part. When you have this there is a solution to run an out-of-the-box sample for a TODO list, complete with an Windows app. On the Dashboard of the Mobile Service is are links to get you started (in red boxes below).

AMS-dashboard

If you want to add your own structure to the data and use it in your own app (as I wanted), the second link under GET STARTED is what you need. It looks something like this:

ams-customcode

Once you’ve run this code once (preferable after adjusting it to your needs), you have data in Mobile Service. But with this you als can see the node.js code that’s (be default) used in the backend of this Mobile Service. When you click Data on the Dashboard, select the table you’ve created and click on Script, you have access to those. By default the INSERT script looks like this:

function insert(item, user, request) {

request.execute();

}

The class I use to store and retrieve data from this table has properties like Latitude, Longitude and Time. I also added a property for Address. But of course I want that address to be populated automatically in the backend. For this I registered an app for the Bing API (For information how to obtain this, read this MSDN article). Then I changed the insert function to look like this:

function insert(item, user, request) {

    // Get the service configuration module.
    var config = require('mobileservice-config');
    var bingKey= config.appSettings.BING_KEY;   

    // create the request and construct the URL
    var httpRequest = require('request');
    var url = 'http://dev.virtualearth.net/REST/v1/Locations/' +
                item.Latitude + "," + item.Longitude +
                '?output=JSON&includeEntityTypes=Address&includeNeighborhood=1&' +
                'key=' + bingKey; 

    // execute the request and handle it
    httpRequest.get({ url: url }, 
        function(err, response, body) {
            // Handle the JSON response
            var x = JSON.parse(body);
            if (response.statusCode === 200 && x.resourceSets[0].estimatedTotal > 0) {
                item.Address = x.resourceSets[0].resources[0].name;
            }
            else {
                item.Address = "Unknown address";
            }
            // Important: store the item
            request.execute();            
        });
}

First I get the Bing API key from the configuration (I show how to do that in a minute). This is a secure way of working with secrets in your backend logic. Next I construct the URL for reverse-geocode the GPS location. This is documented in the Bing API. I add the Latitude and Longitude properties and some parameters what to get from the API. The returned data is in JSON, so we parse that and check for the result. You can receive more than one address with various accuracies. I always pick the first one now. If we don’t get any results, I set the address to “Unknown address”.

To store the Bing Api KEY, go to the Dashboard of the Mobile Service and click the CONFIGURE tab. When you scroll down you’ll see the “app settings”. Here you can add a name for the key and the value of the key. That one is retrieved in the first part of the script.

I hope this helps to build even more solutions.

 
Comments

No comments yet.