This project has moved. For the latest updates, please go here.

Offtopic: Utilizing VirtualEarth + SharpMap in WinForm App.

Topics: General Topics
Feb 21, 2007 at 7:39 AM
I read SharpGIS' article about overlaying SharpMap Layers on top of MS Virtual Earth Maps in a web page.

I'm wondering what the best approach to implementing the same thing except in a C# Windows Form application. I know i can add a web browser ui control to my form. But, not sure how to manipulate what is displayed since the toolkit for manipulating the virtualearth image is javascript.

Any recommendations on the best approach would be appreciated.

Feb 21, 2007 at 3:17 PM

Look here, perhaps does this fits your needs:

Feb 22, 2007 at 6:31 AM
What a perfect little sample!

Feb 23, 2007 at 5:10 AM
Using your sample i was able to manipulate maps.

However, i'm still not sure about adding my owm map data.
I analyzed the code in VEMap.ashx (of SharpMaps ASP.NET VE Demo) which is where where the map is rendered and sent to the client as an image.

Is there an event or something i could attach to the VEMapBrowser?

public void ProcessRequest (HttpContext context) {
int Width = 0;
int Height = 0;
int tileX= 0;
int tileY = 0;
int ZoomLevel = 0;

//Parse request parameters
if (!int.TryParse(context.Request.Params"WIDTH", out Width))
throw (new ArgumentException("Invalid parameter"));
if (!int.TryParse(context.Request.Params"HEIGHT", out Height))
throw (new ArgumentException("Invalid parameter"));
if (!int.TryParse(context.Request.Params"ZOOMLEVEL", System.Globalization.NumberStyles.Float, numberFormat_EnUS, out ZoomLevel))
throw (new ArgumentException("Invalid parameter"));
if (!int.TryParse(context.Request.Params"X", System.Globalization.NumberStyles.Float, numberFormat_EnUS, out tileX))
throw (new ArgumentException("Invalid parameter"));
if (!int.TryParse(context.Request.Params"Y", System.Globalization.NumberStyles.Float, numberFormat_EnUS, out tileY))
throw (new ArgumentException("Invalid parameter"));

//Set up the map. We use the method in the App_Code folder for initializing the map
myMap = Map.CreateMap(new System.Drawing.Size(Width, Height));
string quadKey = VE.TileToQuadKey(tileX, tileY, ZoomLevel); // GetQuadKey(urlString);
// Use the quadkey to determine a bounding box for the requested tile
int x = 0;
int y = 262144;
VE.Box boundingBox = VE.QuadKeyToBox(quadKey, x, y, 1);
// Get the lat longs of the corners of the box
double lon = VE.XToLongitudeAtZoom(boundingBox.x * 256, 18);
double lat = VE.YToLatitudeAtZoom(boundingBox.y * 256, 18);
double lon2 = VE.XToLongitudeAtZoom((boundingBox.x + boundingBox.width) * 256, 18);
double lat2 = VE.YToLatitudeAtZoom((boundingBox.y - boundingBox.height) * 256, 18);

//Convert lon/lat to Mercator projection
ICoordinateTransformation lonlat2mercator = Map.Transform2Mercator(GeographicCoordinateSystem.WGS84);
SharpMap.Geometries.Point min = lonlat2mercator.MathTransform.Transform(new SharpMap.Geometries.Point(lon, lat));
SharpMap.Geometries.Point max = lonlat2mercator.MathTransform.Transform(new SharpMap.Geometries.Point(lon2, lat2));
//Zoom to box
myMap.ZoomToBox(new SharpMap.Geometries.BoundingBox(min,max));

//Generate map
System.Drawing.Bitmap img = (System.Drawing.Bitmap)myMap.GetMap();
//Stream the image to the client
context.Request.ContentType = "image/png";
System.IO.MemoryStream MS = new System.IO.MemoryStream();
img.Save(MS, System.Drawing.Imaging.ImageFormat.Png);
// tidy up
byte[] buffer = MS.ToArray();
context.Response.OutputStream.Write(buffer, 0, buffer.Length);