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.

No comments: