All mapped out
By Tim Anderson
Mapping services from Microsoft and Google offer new opportunities for enhancing your applications, as Tim Anderson discovers.
HardCopy Issue: 43 | Found In: Development | Published: 01/02/2009 | Last Revision: 06/07/2010
Geographic programming is among the most compelling areas of software development, thanks to the emergence of easy-to-use APIs from the likes of Google and Microsoft, backed by remarkable online data, and the popularity of GPS (Global Positioning System) devices. Windows 7, the next version of Windows, has a location API built-in, a move which will stimulate the market for applications that know where you are. Yes, there are privacy implications - and yes, you can disable it - but there are many intriguing possibilities too.
Mapping is much more than simply putting ‘How to find us’ maps on your Website, important though that is. A common use is for visualising data that has a geographic element. A recent example is the UK’s interactive crime map Web sites, showing crime levels superimposed on Ordnance Survey maps. Visualising the geography of sales data - or any kind of data - can reveal trends and highlight opportunities. Customer databases can include location maps location, and you can create applications that assist in route planning for sales, deliveries or support.
Geographic data assists in predicting travel time and costs. Architects and surveyors can integrate map data into drawings and reports, and there are obvious applications for Web sites dealing with travel, holidays, accommodation and real estate. Location-aware mobile applications can answer the question ‘where is my nearest ...’ and provide directions.
Mapping services typically include several elements. Imagery services provide bitmaps of map data in various kinds of views depending on the service and on what data is available. Geocoding services enable you to find the location of an address or place, while reverse geocoding does the opposite. Route-finding services provide directions from one place to another. Mapping APIs let you call these services and overlay imagery with your own data in the form of icons, shapes, shading or pop-up information.
You can also handle events. Clicking on a hotel icon might open a booking form, for example. The most common use of maps is within Web pages but there are Microsoft .NET, Java and C++ APIs for some services which you can use in desktop applications, and Adobe Flash APIs that work both on the Web and on the desktop through Adobe AIR, the desktop runtime for Flash.
Database integration
Storing geographic data has unique challenges which is why SQL Server 2008 introduced spatial data types, and there are similar features available for other databases including Oracle (Oracle Spatial) and IBM’s DB2 (Spatial Extender).
SQL Server 2008 adds two data types. Geometry is a planar type which conforms to the Open Geospatial Consortium (OGC) Simple Features for SQL specification. Geography is a geodetic type, ideal for use with mapping APIs. An instance of the Geography type can be one, two or three dimensional. You can compare and query Geography data in several ways, including distance, intersection and equality.
A Geography instance can be created from geographic data in WKT or WBT (two OGC standards), or Geography Markup Language (GML). Here is an example using WKT:
DECLARE @g geography;
SET @g =
geography::STPointFromText ( ‘POINT(51.5138 -0.0983)’, 4326);
The first argument to STPointFromText is the WKT representation of the location of St Paul’s Cathedral, where the figures are the latitude and longitude. The second argument is the SRID (Spatial Reference ID) which specifies which coordinate system is being used: 4326 is that of the World Geodetic System 1984. You cannot directly compare geographic data across different SRIDs.
As you would expect, SQL Server’s spatial data types work well with Microsoft’s Virtual Earth API. However Google and Microsoft are not the only games in town. MapQuest has a well-established mapping platform with both free and enterprise licenses and a range of APIs including JavaScript, ActionScript (for Flash and Flex), Java, C++ and C#.
OpenLayers is an open source JavaScript library for displaying map data and is supported by the Open Source Geospatial Foundation. Unlike the other services, OpenLayers does not have its own mapping data but can be used with data from a variety of other sources.
Programming Virtual Earth
There are two approaches to programming Microsoft Virtual Earth: the Map Control or the Web services. The easiest way to use Virtual Earth is with the Virtual Earth Map Control, a JavaScript library. This is how you do it (see panel for the code):
- 1. Start a new Web page, making sure that the DOCTYPE is XHTML 1.0 Transitional and the charset is UTF-8.
- 2. Include the JavaScript library for the Map Control which is on the Virtual Earth Web site.
- 3. Add a DIV element which will become the container for the map. The size and position of the DIV determines the size and position of the map.
- 4. Handle the onLoad event for the page and in the event handler create a new VEMap object and a new VELatLong object representing a point.
- 5. Call the VEMap.LoadMap method with arguments specifying the point, the zoom level and the map type. The map type is specified by the VEMapStyle enumeration which can be Road, Shaded, Aerial, Hybrid, Birdseye or BirdseyeHybrid. A Birdseye view is an oblique angle.

This Silverlight and Virtual Earth application is used to check weather and track shipping and other points of interest for Port Metro Vancouver.
So how do you obtain the latitude and longitude of the place you want to display? In some cases you will already have this information in a database. In other cases you may want to do this on the fly, or to populate the database with help from Virtual Earth. This is called geocoding, and you can do it with the VEMap.Find method.
The syntax of this method is confusing, but the important arguments are ‘what’ and ‘where’. The ‘what’ argument specifies what to search for, while the ‘where’ argument specifies the area to search for the place. However the ‘what’ value is specialist and can often be omitted, while the ‘where’ value should normally contain as precise a location as you can manage, or a postcode. The final ‘callback’ argument specifies a function to receive the results. For example, imagine you have an input box with an id of ‘thePlace’. You could call Find like this:
results = map.Find(null, document.getElementById( ‘thePlace’).value, null, null,null,10,true,true,true,true,GetResults);
Here is a simple callback function:
function GetResults( layer, resultsArray, places, hasMore, veErrorMessage) {
var res;
var i;
var resultlist = "";
if (places != null) {
for (i in places) {
res = places[i];
resultlist +=
"
Coordinates for "
+ res.Name + ": "
+ res.LatLong.Latitude
+ " " +
res.LatLong.Longitude;
}
document.getElementById( 'result').innerHTML = resultlist; } else { document.getElementById( 'result').innerHTML = "Place not found"; }
}
Since we specified ten results in the call to Find, this will return up to ten results. By default Virtual Earth will centre on the first place found. The Map Control has a substantial API including methods for route finding, adding pushpins, shapes and icons to maps, and handling control events. This is where you can integrate the map with your custom business data.
Virtual Earth Web Services are built with Windows Communication Foundation and can be called from any language that supports SOAP. There is smooth integration with .NET clients such as ASP.NET, Windows Forms or Windows Presentation Foundation. There are four services: Geocode (including reverse geocoding), Imagery, Route (for directions) and Search. The Imagery service returns static map images which you can display in your application. Note that the MapPoint Web Service is still supported but no longer being developed.
Programming Google Maps
Google’s Map API is as easy to use as Virtual Earth. One difference is that all developers are required to get a developer token, which is linked to a Google account. This token is embedded in calls to the API.

Housebuilder George Wimpey uses a Virtual Earth application to show the location of new homes.
There are a range of geographic APIs from Google. The Google Earth API uses a plug-in rather than pure JavaScript, enabling the 3D features of Google Earth to be embedded into a Web application. The service is currently in beta and does not support all browsers. You can use the Earth API to display Keyhole Markup Language (KML), an XML that is now an OGC standard.
Google Mapplets run as gadgets on the Google Maps site, rather than on your own Web site. The API is similar to the main Maps JavaScript API.
The Google Maps API is a JavaScript library for displaying maps on a Web site. It is still in beta but widely used.
The Maps API for Flash lets you use Google Maps from Flash applications. One advantage is that additional map types are available, including PHYSICAL_MAP_TYPE which shows a physical relief map.
Maps API library for Google Web Toolkit is a set of Java classes which get compiled to JavaScript for deployment, enabling developers to write applications in Java while running them in the browser with no Java runtime dependency.
The Static Maps API is purely URL based - no JavaScript is required. In essence, you request a map using a long URL with several arguments and Google returns a static image which you can embed in your site.
Coding a simple Google map is similar to displaying a Virtual Earth map:
- 1. Start a new HTML page and include the JavaScript library. The code to retrieve this includes your API key, which you can obtain free of charge for non-commercial use.
- 2. Add a DIV element that will be the container for the map. The size and position of this element determines the size and position of the map.
- 3. Handle the onLoad event and write script to create a new object of type GMap2. Then call the SetCenter method to specify what location to display, the zoom level, and the type of the map.
Once you have a GMap2 object you can code against it in various ways, including adding markers and shapes, adding controls so that the user can manipulate the map, and adding InfoWindows to display information to the user (see panel for code example). Geocoding with the Maps API is done by using the GClientGeocoder object. This has a GetLatLng method which looks like this:
getLatLng(address:String, callback:function)
The function is simpler than that for Virtual Earth and returns only one point (or nothing) in the callback. You can use another function, namely GetLocations, to get all matching results in cases where there is ambiguity.
Licensing mapping services
Microsoft’s Virtual Earth API is free for non-commercial use on a public Web site that does not require a login or password. There are a few limitations, such as no more than 50,000 geocode transactions in 24 hours. Commercial or government use needs a paid-for licence. Grey Matter can assist you in finding the best licence and quotation. Call 01364 654100 for further details.
The Google Maps API offers two terms of service: Standard and Premier. The Standard terms allow free use but only on Web sites that are publicly accessible and free. Using the Maps API on sites which have restricted or paid access, or on internal sites, requires the Premier terms which start at $10,000 per year and includes support, greater scalability and additional features such as secure delivery over HTTPS.
These mapping APIs are a great opportunity for creating new types of applications, and for enhancing existing applications with new visualisations and time-saving features. Users are now used to rich interactive maps on Web sites but it is still early days for geo applications as a whole, particularly bearing in mind the possibilities for applications on mobile devices and on netbooks and laptops equipped with GPS sensors.