Developer Product Briefs

Mashing Up Using Virtual Earth

Geographical mash-ups are made easy with the Microsoft Virtual Earth mapping API.

p>I've always been fascinated by maps and geography. Despite being digitally connected to the world, I have a lifelong fascination for physically knowing where I am. It goes well beyond that, in fact. In my youth my family never traveled, so I used maps to travel in my imagination around the world. Today, I am somewhat better traveled, but still spend my downtime browsing maps as others might browse the news headlines.

I became interested in Microsoft Virtual Earth when I sat in on a lunchtime session about this new technology at TechEd 2006. Virtual Earth is a part of the Windows Live hosted application and service site. When Microsoft first announced Virtual Earth, I felt that the company was simply copying an idea conceived of by Google—and doing it in a second-rate fashion.

The former might be true, but Microsoft delivers on its reputation of taking existing ideas and improving upon them. Virtual Earth combines traditional online maps with aerial and satellite photos to provide an experience that takes you seamlessly from maps to photos as you zoom in on a location. And Virtual Earth seems to have more high-resolution photos than its Google counterpart, making it more fun to play with.

In addition, Virtual Earth has a highly functional and easy to use API for writing your own map-based applications. You start by learning about the Virtual Earth Map Control, which is now at version 3.0 (although, the Virtual Earth site is incongruously labeled beta). The Map Control is called from Windows Live's developer site. This API provides many methods for manipulating the map, incorporation location, size, and other characteristics.

Virtual Earth's many digital sources of geographic data also make it a great platform for "mash-ups," or applications that combine components from different applications and data sources into a single use. Geographic mash-ups are particularly valuable, because the whole is more than the sum of the parts. That is, activity and location data combined are far more effective than both provided separately.

It is important that you approach development of your mash-up cautiously. Many developers think that a mash-up is about the applications used, but it is truly about the user experience. In order to create a successful geographic mash-up, you must find out what type of information makes sense for your users and how to present it geographically.

For example, in my own mash-up—which I explain in detail below—I tried to provide information that would be useful to Fawcette Technical Publications, Inc. (FTP) conference participants. With perhaps 10 or more conferences over the course of one year, FTP sends participants to a variety of different cities for conferences on a diverse range of topics. I wanted to provide FTP participants with relevant information on the locale so that they can settle in faster and have a better overall experience.

Some Basic Map Operations
You can create either an HTML or ASP.NET project in Visual Studio, depending on your application goals, to begin developing your mash-up. Once your project is created, open a new HTML document. From there, calling maps in Virtual Earth is easy. First, you  reference the map control as the source for your page to call:

<script src=""></script

When your page has referenced the map control, set up the call to display a default map by completing a LoadMap ( ) method call:

      function GetMap()
         map = new VEMap('myMap');

Last, you display the map:

<body onload="GetMap();">
      <div id='myMap' style="position:relative; width:600px; height:400px;"></div>

Without parameters, the LoadMap method provides a default map of the continental United States. You can use parameters to specify where the map is centered, and the zoom level, among other things. This provides you with a great deal of programmatic control over the type of map you display. For example, here is code that provides a latitude and longitude on which to center the map, while the next parameter, 12, provides the zoom level:

map.LoadMap(new VELatLong(42.715, -71.463), 12 ,'h' , false)

In this example the latitude and longitude are in degrees and degree decimals, rather than degrees-minutes-seconds. The zoom parameter goes from 1 (lowest zoom) to 19 (highest zoom), with a default value of 4. The next value represents the map style. Valid values are a for aerial, h for hybrid, o for oblique (bird's eye view), and r for road. The default style value is r. The last parameter is a Boolean value that specifies whether the map view is displayed as a fixed map that the user cannot change. You can see the map produced by this call to LoadMap in Figure 1.

I also programmatically inserted a pushpin into this map to designate a specific location. Inserting the pushpin requires a call to VEPushPin, with the location parameters. You can always use the latitude and longitude as variables, either to be input by the user or as a feed from another application or data source. (You might have to complete a conversion to get the coordinates into the appropriate data format.)

Here is code you can use to add a description that displays on the label for the pushpin:

var pin = new VEPushpin(
        new VELatLong(42.715, -71.463),
        'This is my house '

Building on the Map
The Virtual Earth map control will do a lot of fundamental things for you. For instance, if you input a beginning and end address or other location information, the GetRoute method will plot the shortest route from the source to the destination. Here is the easy-to-execute GetMap function:

         function GetMap()
            map = new VEMap('myMap');
            map.GetRoute("299 Dunstable Road, Nashua NH 03062", "Manchester Airport");

In this example, GetRoute uses an address for one of the parameters, and a more general location or place name for the other. Of course, you can also use variables, and input these parameters from the keyboard or import them from another data source. You can see the map produced by this function in Figure 2.

With these operations as a starting point, I completed this mapping application and imported data from a variety of sources on each of the cities in which FTP has a conference. For example, I offered participants the ability to plot routes from the airport and major highways to the conference venue, and pinpoint hotels, restaurants, and other points of interest in the area. Virtual Earth itself handles the route plotting. In many cases, the data for hotels, subway stops, and points of interest was available on city or travel Web sites. You can view more results from my mash-up on future FTP conferences pages.

About the Author

Peter Varhol is the executive editor, reviews of Redmond magazine and has more than 20 years of experience as a software developer, software product manager and technology writer. He has graduate degrees in computer science and mathematics, and has taught both subjects at the university level.

comments powered by Disqus
Upcoming Events

.NET Insight

Sign up for our newsletter.

I agree to this site's Privacy Policy.