Azure + Bing Maps: Working with Bing Maps in a rich HTML application

This is the 7th article in our "Bring the clouds together: Azure + Bing Maps"
series. You can find a preview of live demonstration on
. For a list of articles in the series,
please refer to

Update: Now that Bing Maps AJAX SDK 7.0 is released, this post has been
updated to provide code for both SDK 6.3 and SDK 7.0. Our final sample code is
built for v7.0.


Our previous posts focused on the services side. We've introduced how to
design a scalable cloud database, how to work with spatial data in SQL
Azure, how to access data using Entity Framework, and how to write data
centric web services using WCF Data Services.

In a team environment, multiple developers can work simultaneously. So
while data and service developers are working on the server side, it is
perfectly valid for a client developer to start working with client

This article targets client software developers who want to integrate
Bing Maps to their rich HTML applications. We assume the audiences have
no previous knowledge of Bing Maps SDK, but have basic knowledge of HTML
4, JavaScript, CSS, and jQuery. No HTML
5 knowledge is used.

We classify this post as an article for beginners. We'll briefly
introduce how to develop with Bing Maps SDK. After complete reading this
post, you will be able to display a map, invoke REST services to obtain
location information, and operate pushpins. To learn more advanced
topics, please refer to the
interactive SDK.
This post only targets HTML applications. We'll write how to integrate
Bing Maps with Silverlight and Windows Phone applications in future

Bing Maps SDKs

Bing Maps is a Software as a
Service (SaaS) offer from Microsoft. Both consumers and developers can use it.

For consumers, Bing Maps offers two versions. A HTML version which supports
basic map navigation and searching, and a Silverlight version which offers
smoother browsing experience and more third party applications integration.

For developers, there several versions of Bing Maps SDK:

  • Bing
    Maps AJAX Control
    , which targets rich HTML applications.
  • Bing
    Maps Silverlight Control
    , which targets desktop Silverlight applications
    running on Windows, Mac, or out of browser. Those are applications that are
    not submitted to
  • Bing Maps Silverlight Control for Windows Phone, which is included in

    Windows Phone Developer Tools
    . This is a slight variation of the desktop
    Silverlight version that adopts a smaller touch screen.
  • Bing Maps App
    , which is currently in beta, allows third party developers to submit
    their applications directly to, instead of the developer's own
    server or another cloud offering such as Azure. After being reviewed,
    consumers can use those applications just as if they're part of Bing Maps
  • Bing
    Maps SOAP Services
    , which can be used in any platforms that support SOAP
  • Bing
    Maps REST Services
    , which can be used in any platforms that support HTTP
  • Bing
    Maps Spatial Data Services
    , which allows client applications to
    calculate spatial data without a custom service application.

The SOAP/REST services offer the following features:

  • Geocode/Location Service, which is used to search a place's information
    (such as name and address) given the latitude/longitude, or the other way
  • Imagery Service, which returns individual images that composes a map.
    This service is usually used on platforms (such as WPF, native Windows
    applications, and Java desktop applications) that do not have a built-in map
  • Route Service, which helps to calculate a route between multiple stops.
  • Search Service, which is used to perform general searches. This is a
    SOAP only service.

As you can see, Bing Maps can be used in almost any platforms. If you're
working with HTML, Silverlight, or Windows Phone, you can take advantage of the
map controls which have done almost everything for you. If you're working with
other platforms, you can either embed a web browser in a client application, or
invoke the SOAP/REST services. The SOAP/REST services can be used in
HTML/Silverlight/Windows Phone as well, to obtain information that are not
included in the map controls.

Obtain a Bing Maps developer key

Most SDKs, although not all, require a developer key to use. It is free to
register a key. Just follow these steps:

  • Type an application name and a URL, choose the application type, and
    click Create key. You can type anything you like, including localhost.

Display a basic map

To display a map, follow these steps:

1. Create a container, which is usually a div. Give it an ID so we can
reference it later.


2. If you're using SDK 6.3, there's a compatibility issue between IE9 beta and Bing Maps
AJAX Control. So it is recommended to force IE9 to use IE8's standard to render
the document. This issue doesn't affect the AJAX map on It also
doesn't affect Silverlight applications. If you're using SDK 7.0, IE9 beta
is supported, so you don't need to fallback to IE8's standard.


3. Link to the SDK JavaScript code. Currently the highest SDK version is
v7.0. But you can also use v6.3 if you don't want to change existing code.



4. After the HTML document is loaded, we can intialize the map. create a new instance of the map object, set the credential (the key you registered
in the previous step), and load the map.

Note the initialization code for SDK 6.3 is different from 7.0.

If you're using SDK 6.3, create a new instance of VEMap, and pass the ID of
the container as a parameter. Then invoke SetCredentials to set the credential
(the key you registered in the previous step). Finally invoke LoadMap to load
the map. LoadMap takes an optional parameter representing the central location
(latitude/longitude) of the map.

If you're using SDK 7.0 create a new instance of Microsoft.Maps.Map. The
constructor itself accepts parameters representing the map's credential and
center point. So no additional method invoking is needed.

In SDK 7.0, you will encounter the namespace Microsoft.Maps in a lot of
places. So you may want to give it an alias:

Bing = Microsoft.Maps;

Later you can use new Bing.Map instead of new Microsoft.Maps.Map. This is
what we will use in the remaining of the post. But please do not confuse. The
actual namespace is Microsoft.Maps. Bing is just a short alias we use in the

Also note our application heavily uses jQuery. If
you haven't worked with jQuery before, it is recommended to get started from


mapCredential = 'change to your key';




// SDK


LoadMap() {

    map =
new VEMap('MainMap');


VELatLong(31, 121));


// SDK

LoadMap() {

    map =
new Bing.Map($('#MainMap')[0],


credentials: mapCredential,

center: new Bing.Location(31, 121)



Now a bare map will be displayed:


Attach event handlers

To attach event handlers to the map control, you invoke the AttachEvent
method in SDK 6.3, or invoke Microsoft.Maps.Events.addHandler in SDK 7.0. Note
certain event names are different for different SDKs.

// SDK



// SDK

'mousedown', Map_OnMouseDown);

'mouseup', Map_OnMouseUp);

There're a bunch of events on the map class you can handle. Please refer to
(SDK 6.3) and
(SDK 7.0) for the complete list.
But be aware, while there is an onclick event, it will be fired whenever the
mouse is clicked. That is, if you hold down the mouse, pan the map, and then
release the mouse, this event will also be fired. In certain cases, this may not
be the behavior you want. For example, you may wish to add a pushpin only if the
user clicks the map without panning around. In such cases, you need to handle
onmousedown/up events directly, and write logic to figure out if the mouse has

// SDK




Map_OnMouseDown(e) {

    mouseDownLocation =
new VEPixel(e.mapX, e.mapY);




Map_OnMouseUp(e) {

var pixel = new
VEPixel(e.mapX, e.mapY);

// Only add a pushpin if the user is not panning
the map.

if (mouseDownLocation !=
null && mouseDownLocation.x == pixel.x &&
mouseDownLocation.y == pixel.y) {

// Do something…



// SDK

Map_OnMouseDown(e) {

    mouseDownLocation =
new Bing.Point(e.pageX, e.pageY);



Map_OnMouseUp(e) {

var pixel = new
Bing.Point(e.pageX, e.pageY);

// Only add a pushpin if the user is not panning
the map.

if (mouseDownLocation !=
null && mouseDownLocation.x == pixel.x &&
mouseDownLocation.y == pixel.y) {


// Do something…



Invoke Location Service

When the user clicks the map, we will add a pushpin on the clicked location.
This operation doesn't require a round trip to the service, because you can use
the PixelToLatLong (SDK 6.3) or tryPixelToLocation (SDK 7.0) method to obtain the clicked point's location, which is
enough to create a pushpin. However, our application requires more than the
location's latitude and longitude. We also need at least the name (place) of the
clicked location. This must be done with a service call.

Since we're working with an HTML application, the simplest solution is to use
the REST services, which supports both XML and JSON formats. We'll use JSON here
since we're using JavaScript and jQuery. To obtain detailed information of a
location given the latitude and longitude, we can use the Geocode/Location
Service. There're several usages of this service. If you know the
latitude/longitude, the URI of the service is:[latitude],[longitude]?key=[your

You can also specify additional query strings such as o (format) and jsonp
(to workaround the cross domain issue).

// SDK

latLong = map.PixelToLatLong(pixel);

// SDK

var latLong = map.tryPixelToLocation(pixel,;


// Invoke the Location REST service to
obtain information of the clicked place.



+ latLong.Latitude + ',' + latLong.Longitude
+ '?o=json&jsonp=LocationCallback&key=' +





The response looks like the following:





© 2010 Microsoft and its suppliers. All rights reserved. This API cannot be
accessed and the content and any results may not be used, reproduced or
transmitted in any manner without express written permission from Microsoft






























As you can see, the response may contain multiple results (resources) that
are located near the provided latitude/longitude. Our application only cares
about the first resource. Each resource contains multiple information, such as
the name of the location, and the exact latitude/longitude. Those are the
information we need for our application.


LocationCallback(result) {

if (result.resourceSets.length > 0) {

var resourceSet =

if (resourceSet.resources.length > 0) {

var resource = resourceSet.resources[0];


// Add a pushpin.

// SDK

point =
VEShape(VEShapeType.Pushpin, new
VELatLong(resource.point.coordinates[0], resource.point.coordinates[1]));



// SDK 7.0:

var pushpin =
Bing.Location(resource.point.coordinates[0], resource.point.coordinates[1]));





For more information about the response format, please refer to

Create a shape and add it to the map

As demonstrated in the above code, to add a pushpin to the map, first create
a VEShape (SDK 6.3) or Microsoft.Maps.Pushpin (SDK 7.0), and then invoke VEMap.AddShape
or Microsoft.Maps.Map.entities.push. There're 3 kinds of shapes: pushpin
(a single point), polyline, and polygon. Our application only uses pushpin. But
it is also very easy to add a polyline and a polygon. Simply provide multiple
points. And in SDK 7.0, choose a different class. Take the sample from

var points = new Array(
new VELatLong(45.01188,-111.06687, 0, VEAltitudeMode. RelativeToGround),
new VELatLong(45.01534,-104.06324, 0, VEAltitudeMode. RelativeToGround),
new VELatLong(41.01929,-104.06, 0, VEAltitudeMode. RelativeToGround),
new VELatLong(41.003,-111.05878, 0, VEAltitudeMode. RelativeToGround)
var myPolygon = new VEShape(VEShapeType.Polygon, points);
var myPolygon = map.AddShape(myPolygon);


In SDK 6.3, you can also set a title for the shape by invoking the SetTitle method, which
will be displayed when the user hovers mouse over the shape.

In SDK 7.0, this behavior is not provided out of box. So we have to it
ourself. Since JavaScript is a dynamic language, we can add properties
dynamically to an object. For example, you can write pushpin.title =;, even if the Pushpin class doesn't have a title property defined.

To display a popup, you're required to create the html elements, and write
some code to display the elements. Here we provide a simple implementation.
First we put a div inside the map:







Then set the styles for the elements. Note the popup's z-index is set to a
very large value, so the map images won't cover it.



z-index: 10000;

position: absolute;

opacity: 0;

width: 200px;

text-align: center;





position: relative;

margin-top: 20px;


Now we can handle the pushpin's mouseover/out events, and display/hide the

Bing.Events.addHandler(pushpin, 'mouseover',

Bing.Events.addHandler(pushpin, 'mouseout',


Pushpin_MouseOver(e) {


var pushpinPopup = $('#PushpinPopup');   

var pixel =
map.tryLocationToPixel(, Bing.PixelReference.control);

pixel.x - 100);


opacity: 1 });


Pushpin_MouseOut() {

opacity: 0 })



This post provided an introduction to Bing Maps AJAX Controls and Bing Maps
REST Services. As you can see, it is very easy to use Bing Maps in your own
application. Most of the code described in this post uses standard JavaScript
and jQuery. If you wish to learn more, please refer to the documentation at
, and the interaction
SDK at

The next post will introduce
jQuery Templates,
a joint investment of Microsoft and jQuery. You'll learn how it helps you to
display structured data (the travel stop list).


Comments (0)

Skip to main content