Bing Map v7 Address Query using jQuery

How to initialise Bing map using an street address query.

First reference the Bing library, keeping it as an external reference. They changed one of their JavaScript urls once and 404ed on me. There are references for both SSL and non-secure pages.

Next grab your API Key after creating a Bing Maps account.

Get the coordinates of the address you want before initialising the map using the below function, passing in the API key value as well as ‘target’ – the id of your map placeholder and ‘query’ – the actual street address, taking care to exclude company / building names if possible to avoid confusion.

function getMap(query, key, target) {
    $.ajax({
        url: "http://dev.virtualearth.net/REST/v1/Locations",
        dataType: "jsonp",
        data: {
            key: key,
            query: query
        },
        jsonp: "jsonp",
        success: function (data) {
            try{
            var c = data.resourceSets[0].resources[0].geocodePoints[0].coordinates;
            var x = c[0];
            var y = c[1];
            map = new Microsoft.Maps.Map(document.getElementById(target), {
                credentials: key,
                enableSearchLogo: false,
                showDashboard: false,
                showScalebar: false,
                enableClickableLogo: false,
                zoom: 14,
                center: new Microsoft.Maps.Location(x, y)
            });
            map.entities.clear();
            var pushpin = new Microsoft.Maps.Pushpin(map.getCenter(), null);
            map.entities.push(pushpin);
            pushpin.setLocation(new Microsoft.Maps.Location(x, y));
            } catch(e) {}
        }
    });
}

More map options can be found here: http://www.bingmapsportal.com/ISDK/AjaxV7

This entry was posted in Web Related and tagged , , , , , , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

*


*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>