Showing posts with label Google Maps. Show all posts
Showing posts with label Google Maps. Show all posts

Virtual Earth and Google Maps Tile Server Client for .NET (and Compact Framework)

If you've ever used Live Search, you probably agree that it is hands down the best local search program available for Windows Mobile. It got me curious as to how the maps are rendered so smoothly. A quick investigation made me stumble upon Via Virtual Earth, which explained how to roll a tile server and a corresponding tile client.

I ended up coding a Tile Server client in C# that is compatible with Virtual Earth and Google Maps. This includes the satellite, terrain, traffic, and other such tiles. Here are the grand results:

tiledmaps

On the left, you see a simple client running Virtual Earth on Windows Mobile. Top right, you see the client running on Windows. Below that, you see that same program running Google Maps. Both clients are also displaying the directions from where I live (Seattle) to where I work (Bellevue).

These two programs are just test harnesses, and don't even demonstrate all of capabilities of the tile client. Features include:


    • Blending multiple tile sources (such as maps + traffic, or satellite + traffic)
    • Push pin images
    • Retrieval of directions from both Virtual Earth and Google Maps: the text and an array of geocodes that is used by the client to draw the path
    • Cell Tower geocoding

Sadly I can't release the fully featured map controls that I created that harness all the features, because that would infringe on company IP. However, I can release the full source to the tile and directions clients.

The client makes it trivial to draw a smooth scrolling map. You just instantiate it and tell it to draw on a Graphics object. From there, you can pan, zoom, etc. The client does everything asynchronously: it calls a handler you provide whenever a new tile has completed downloading. The following is the entirety of the Windows Mobile client test harness code:




VirtualEarthMapSession mySession = new VirtualEarthMapSession();
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
mySession.DrawMap(e.Graphics, 0, 0, ClientSize.Width, ClientSize.Height, (o) =>
{
Invoke(new EventHandler((sender, args) =>
{
Invalidate();
}));
}, null);
}

Point myLastPos = Point.Empty;
protected override void OnMouseMove(MouseEventArgs e)
{
mySession.Pan(MousePosition.X - myLastPos.X, MousePosition.Y - myLastPos.Y);
myLastPos = MousePosition;
Invalidate();
base.OnMouseMove(e);
}

protected override void OnMouseDown(MouseEventArgs e)
{
myLastPos = MousePosition;
}

private void myZoomInMenuItem_Click(object sender, EventArgs e)
{
mySession.ZoomIn();
Invalidate();
}

private void myZoomOutMenuItem_Click(object sender, EventArgs e)
{
mySession.ZoomOut();
Invalidate();
}



Note:

This code is really old. I had to update a few of the URLs to the tile servers prior to publishing the code so that it would work. Google tends to change their URL schema relatively often, usually its just a version number in the query string or something. In addition, it looks like the format of the HTML that Google returns for text directions is no longer the same, and consequently client fails when parsing it now. An enterprising developer other than myself would need to figure out how to parse the new HTML format and update the code. However, the rest of it seems to be working (including Virtual Earth directions).

Additionally, I did not create clients for the geocoding services provided by Google Maps nor Virtual Earth, seeing as though my former company had their own. That shouldn't be too difficult, Google has a free REST based geocoding service, and so does Yahoo, MapQuest, and others I believe.

Also, I would have done all the downloading using the anonymous methods technique I described below, but anonymous methods were not available yet. :(