RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX


Virtual Earth 101 : Page 4

Find out how to use the Virtual Earth AJAX Map Control and the Windows Live Tools for Visual Studio to add sophisticated mapping features to your web applications.


Importing GeoRSS Data

Now that you have seen how to create maps and manually add shapes (pushpins, polylines and polygons) with both the AJAX Map Control and the ASP.NET Map Control, I'll show you how to import data to overlay your map.

GeoRSS is a variation on the popular RSS format that adds tags for point, polyline, and polygon data. For example, Listing 4 contains the same pushpoint, polyline, and polygon information used to create the AJAX Map of the Microsoft campus earlier in the article.

At this point you should recognize some of the tags, including the title and description. Obviously the <georss:point>, <georss:line> and <georss:polygon> correspond to pushpoints, polylines, and polygons. This GeoRSS file is very simple.

Importing GeoRSS Data with the AJAX Map Control

In Listing 5 you can see that all the code required to add those map objects manually has been stripped out and replaced with these three lines.

   var mylayer = new VEShapeLayer();
   var veLayerSpec = new VEShapeSourceSpecification(
      VEDataType.GeoRSS, "MSFT_AjAX.xml", mylayer);
   map.ImportShapeLayerData(veLayerSpec, null);
Figure 10. GeoRSS Data Import: This is the map displayed after importing the GeoRSS data.
The preceding code creates a new VEShapeLayer to contain the imported data points. Next, it creates a VeShapeSourceSpecification object, which specifies the import format (VEDataType.GeoRSS), the name of the GeoRSS file (MSFT_AJAX.xml), and the layer it should be imported into (mylayer). Finally, it calls the map's ImportShapeLayerData method to import the data.

Listing 4 contains the GeoRSS data being imported in the preceding example. Running the page from Listing 5 results in the same map being displayed (see Figure 10) as the earlier example that adds the map objects manually.

Importing GeoRSS Data with the ASP.NET Map Control

The steps to import GeoRSS data using the ASP.NET Map Control are very similar. The code below imports an existing GeoRSS file (MSFT_ASPNET.xml) and displays it.

   Imports Microsoft.Live.ServerControls.VE
   Partial Public Class GeoRSS_ASPNET
   Inherits System.Web.UI.Page
   Protected Sub Page_Load(ByVal sender As Object, _
   ByVal e As System.EventArgs) Handles Me.Load
   'Initialize map properties
           Map1.Center.Latitude = 47.6439
           Map1.Center.Longitude = -122.129
           Map1.ZoomLevel = 17
           Map1.MapStyle = _
   'Create new layer to contain shapes
   Dim layer As New ShapeLayer
   Dim ShapeSpecs As New ShapeSourceSpecification( _
             DataType.GeoRSS, "MSFT_ASPNET.xml", layer)
           Map1.ImportShapeLayerData(ShapeSpecs, "", True)
   End Sub
   End Class
The preceding code first initializes the initial map position, type, and zoom level. Next a shape layer is created and added to the map. The import happens on the next two lines. A ShapeSourceSpecification object is created with the import type specified (GeoRSS), the name of the GeoRSS file to import, and finally the layer to import the data on to.
Figure 11. Map with HTML Markup: This map results from importing the GeoRSS data with HTML markup included in the data.
The DataType enum values are GeoRss, ImportXML, and VECollection. ImportXML is used primarily to import KML files, while VECollection is used to import collections you may have created on Virtual Earth. I created Virtual Earth collections to get the latitude and longitude coordinates for all the samples in this article.

Lastly, the code calls the map's ImportShapeLayerData method, passing in the name of the ShapeSourceSpecification object, the name of the client-side JavaScript function to call when the import has completed, and a Boolean value that determines whether the map focus should shift to display the imported data. Running the preceding code produces the map displayed in Figure 11.

The GeoRSS data imported into the ASP.NET Map Control (see Listing 6) is similar to the data used with the AJAX Map Control example, but it differs in the polygon section at the end of the file. Just like the HTML markup added to some of the properties when adding map objects manually earlier, you can add HTML markup to the GeoRSS file (as shown here) to provide a richer user experience.

Additional Data Importing Options

The two Virtual Earth map controls can import more than just GeoRSS formatted data. Other supported formats include KML, GPX and VECollection. Keyhole Markup Language (KML), like GeoRSS, is an XML-based format for managing the display of geospatial data in Google Maps and Google Earth. The GPS eXchange format (GPX) is a format for managing GPS data.

Another prime candidate for importing data is SQL Server. While SQL Server 2005 does not have the new geospacial data types that SQL Server 2008 provides, you can easily store latitude and longitude values in your databases. For example, you could create a query to generate a list of retail stores along with their address, website URL, manager name, phone number, latitude and longitude coordinates, and so on to generate a GeoRSS file that you could import into a map. You could add links to your corporate logo, store picture, etc., to create a rich user experience when people click on a specific store location.

I hope this introduction to developing Virtual Earth applications gets you thinking about new ways of delivering data to your users. There is still much to learn. For example, SQL Server 2008 has new geospacial data types that are tailor made to working with Virtual Earth. The Virtual Earth MapCruncher feature allows you to overlay your own map images over the Virtual Earth maps. You can learn more about MapCruncher or download it here. The Virtual Earth Web Services 1.0, new in version 6.2, provides imagery, search, geocoding, and routing services.

There are additional server-side and client-side events you should learn how to handle. For example, one sample in the Interactive SDK demonstrates how to react to client-side events such as zooming in and out using JavaScript; another shows how to handle other mouse events, such as clicking or double-clicking. Other features that make it easy to provide driving directions, display points of interest, work with birds-eye views, 3D maps, and much more await your discovery. Check out the Virtual Earth Resources sidebar, which lists a number of resources to help you continue the learning process. Good luck and happy mapping!

Jim Duffy is founder and president of TakeNote Technologies, an award-winning training and software development company. He has a BS degree in Computer and Information Systems and over 19 years of programming and training experience. He is an energetic trainer, skilled developer, and has been published in leading developer-oriented publications. Jim, a Microsoft MVP award recipient, is a popular speaker at regional user groups and developer conferences. He is also a co-host of Computers 2K4, a call-in radio show on WRBZ (AM 850), 850 The Buzz, in Raleigh, NC. Jim is also the author of TakeNote's popular VFP database auditing product, FoxAudit. In addition to his duties with TakeNote, Jim is also the VP of Sales & Marketing for Red Matrix Technologies, a SQL Server tools vendor who's SQLAudit product adds complete audit trail support for SQL Server databases. Additional information about Jim, TakeNote Technologies, and Red Matrix Technologies can be found at www.takenote.com.
Email AuthorEmail Author
Close Icon
Thanks for your registration, follow us on our social networks to keep up-to-date