Wednesday, November 28, 2007

Find a Location with Virtual Earth 6.0

A working map in Virtual Earth 6.0 is great, but what is really handy is having a map load with a specific area. This can be a state, a country, a city, whatever. It can be a location you have set as a default starting point, or a location supplied by a user.

For today, we'll just use a static starting location.

Use the code we created yesterday. We'll just modify it slightly so our map doesn't default to the whole of the United States.

Step 1
First, find the line of Javascript code that looks like this:
map.LoadMap();

We're going to change this line to load a specific point on the map.

Step 2
Next, we're going to create a new Virtual Earth object called the VELatLong object. This is one that you will use a lot in your Virtual Earth development. This object represents a position on the map - a latitude and longitude.

Since we're just creating a static starting location for our map, we're going to hard code our lat/long location. To do so, on the line just before map.LoadMap(), we create a new VELatLong object and pass in the latitude and longitude in the constructor, like this:
var centerPosition = new VELatLong(47.49319671735114,-122.21654891967773);

Step 3
Next, we're going to use this VELatLong object and pass it into our LoadMap method to set our starting location.
map.LoadMap(centerPosition);

Go ahead and compile and browse to your page. You should see the map load with a new location. But there is still a problem. We're too far away to see anything of value.

Step 4
Finally, we're going to set a default zoom level. The zoom levels are integers ranging from 1 to 19, with 19 being fully zoomed in. The default is 4. Let's get up close and personal, and set a zoom level of 13.
map.LoadMap(centerPosition,13);

That should do it. Compile and browse and you should be looking at Renton Airport.

Tuesday, November 27, 2007

How To Show a Map in Virtual Earth 6.0

Displaying a map using Virtual Earth 6.0 is a snap. In four short steps, you can have a fully functional, dynamic map on your web site!

Step 1.
First, we need to add a link to the Javascript that drives Virtual Earth. VE is a client-side application, which means it needs Javascript to run. If you have Javascript-disabled browsers in your user pool, you'll be out of luck.

Here's the link you'll need for the latest version of Virtual Earth:
<script type="text/javascript" src="http://dev.virtualearth.net/mapcontrol/mapcontrol.ashx?v=6"></script>

Notice the ?v=6 appended to the end of the url. That indicates that you want to use version 6 of Virtual Earth.

You'll need this Javascript referenced and available on each page that you want the mapping functionality.

Step 2.
Next, you'll need to add a div to represent your map, like this:
<div id="myMap" style="z-index:100;position:relative; width:413px; height:327px; top:0px; left:0px; border-style:solid; border-width:1px; border-color:#C2C2C2;"></div>

Be sure this is inside of your form tags. You can style it anyway that you'd like. You can also call it anything that you'd like. I've called mine myMap. We'll use that ID in upcoming Javascript calls.

Step 3.
Next, we need to add a script block with a couple of short functions to initialize and set up the map. I've cryptically called these InitializeMap() and SetupMap(), as follows:

<script language="javascript" type="text/javascript">
//be sure this is outside the function block so you can reference it later.
var map = null;

function InitializeMap()
{
//pass in the ID of your div here
map = new VEMap('myMap');

//we need to give the map time to load before we use it...
setTimeout('SetupMap(); ', 1000);
}

function SetupMap()
{
try
{
map.LoadMap();
}
catch(e)
{
alert(e.message);
}
}
</script>

Step 4.
This is your last step. Here you need to get things kicked off. To do so, add a body.onLoad() method to your body element to fire off the InitializeMap() function, like this:
<body onload="InitializeMap()">

That's it! Compile your application and browse to your page. You should now have a map that allows panning and zooming. We'll discuss other things you can do with this map in later posts.

Monday, November 26, 2007

Visual Studio 2008 Available!

Visual Studio 2008 is available for download at MSDN subscriptions: http://msdn2.microsoft.com/en-us/subscriptions/bb608344.aspx

Thursday, November 15, 2007

devconnections 2007 - las vegas

I just returned from DevConnections 2007. It was well worth the time and money. Overall the conference was a great success. It offered a lot of great training. The rooms were large with perfect A/V setups. The sessions were 75 minutes each, offering just enough detail to get you started in the right direction with a handful of knowledge nuggets.

I would definitely recommend DevConn. It had all the benefits of TechEd without the trip to the East Coast, nor the added expense of two additional days of training. The three days DevConn offered was perfect. My brain wasn’t fried by the end, yet I was able to get a lot good training in the three days.

Trip Highlight
The best session was that with Juval Lowy. I first saw Juval about five years ago at a local conference when he taught a great session on test driven development. This time I listened to him teach about service-oriented development. It was awesome. Definitely the best of the bunch.

I'll detail more about this session and the trip in general in future posts. If you ever get a chance to go to DevConnections, TechEd, or the PDC, definitely go. They are a great time and full of information.