Thursday, 7 May 2009

Even more with Virtual Earth

Onwards and upwards we go

Lets add a pushpin to our map. So if you open the existing solution we can dive right in!

Create a new ASPX file and drag a map control onto it and a script manager if there is not one there already

Now we will add 2 text boxes and a button

<asp:Label ID="Label2" runat="server" Text="Latitude"></asp:Label>&nbsp;
<asp:TextBox ID="txtLat" runat="server"></asp:TextBox>
<br />
<asp:Label ID="Label1" runat="server" Text="Longitude"></asp:Label>&nbsp;
<asp:TextBox ID="txtLong" runat="server"></asp:TextBox>
<br />
<asp:Button ID="btn" runat="server" Text="Add Point" onclick="AddPoint" />

Right now into the code behind

We will add some code to add a point to the map

double dblLat = ConvertDbl(txtLat.Text);
double dblLong = ConvertDbl(txtLong.Text);

LatLongWithAltitude point = new LatLongWithAltitude(dblLat, dblLong);
Shape s = new Shape(ShapeType.Pushpin, point);

s.Title = "Hello from the demo";

Map1.AddShape(s);

First thing you will notice is the function ConvertDbl. This is a small helper function because of the way that Norwegian systems interpret how a double works. Norwegian uses a comma (,) as decimal separator rather than a point.

So we create a new LatLongWithAltitude object and pop our co-ordinates in. Then we create a shape to using a PushPin shape and our point. We can add other attributes to our shape at this point.

Then we add the shape to the Map. If you are adding a lot of shapes, its best to use a ShapeLayer so that you can hold them otherwise they are added to the base ShapeLayer

Right so putting in 58.97 as latitude and 5.75 as longitude we get a point that is in downtown Stavanger in Norway.

Point

Solution can be downloaded from here

And yes I am experimenting with different plug-ins to highlight code in Windows Live Writer :)

No comments: