Adding multiple layers to a map

This recipe will make use of every other recipe in this chapter and build on them. We are going to make an interactive map with four layers on it. Two maps, the Canadian provinces from the previous recipe and a layer with some Canadian cities. We will also add a control to show or hide the individual layers.

Adding multiple layers to a map

Getting ready

Since this recipe is going to make use of everything in the previous recipes, it is advised to read the beginning of this chapter.

How to do it...

The following are the steps required to add multiple layers to a map:

  1. Start by generating the data. Use the region data from the previous recipe and we will create data for Canadian cities using the following site as reference, http://www.infoplease.com/ipa/A0001796.html. The following code snippet shows you how to store one city:
    dataCity.push( new LongLat("Calgary", new Coordinate("W", 114, 1), new Coordinate("N", 51, 1)));
  2. Instantiate a Map class just as we have been doing this entire chapter.
  3. Create the two Bing layers, one as a road map and the other as an aerial map.
  4. Go over to the region data and create the visuals for the provinces that will go into our region layer.
  5. Create the last layer: the city layer. Much like the Adding points of interest to a map recipe, where we added points of interest to a map, here our markers will represent cities. Create the class CityMarker to be the visual for CustomMarkers.
  6. Add a LayerManager control so that we can turn on and off different layers on our map. We add it as shown in the following code snippet:
    _map.addControl(new LayerManager());

How it works...

What we want to create here is a map that has multiple layers of information that you can toggle on and off. To do so we will create four layers. The two base layers will be maps from Bing. The map that is going to be beneath all the others is the aerial one. The second map will be the road map. You should notice that we gave the road map an alpha of 0.6 so that we can see the map under it a bit. By playing with the alpha you can give a different look to your background layer.

Another point to notice is that when you create a Bing map layer, you don't need to give it a layerName value or an identifier like other layers. This will actually cause problems with the LayerManager control that we will add later on. To solve these problems, you can manually set the layerName value and the identifier yourself as shown in the following code snippet:

bing.layerName = "Bing Aerial";
bing.identifier = "Bing Aerial";

Note that you have to use "Bing Aerial" and "Bing Road" for their respective layers as other Strings will generate errors with the LayerManager control.

We are ready to add our third layer, the region layer that we created in the previous recipe.

The last layer will hold the city marker for some cities in Canada. The CityMarker class shows how to use DisplayObject, in this case a Sprite class, to make a marker. This marker is even interactive as it displays the name of the city as the user rolls over it. In order to make this work properly, we modified the CustomMarker class in its draw function. We removed the fact that it was centering the marker as it was causing problems when we added and removed the label. Instead we use the offset parameters to center our marker.

Finally step 6 shows how to add a LayerManager so that the user can control what data is displayed on the map.

There's more...

The LayerManager control is useful but you could make a better one.

Making your own LayerManager control

The LayerManager control provided by OpenScales is useful and quick to get on a map, but it can be quite cumbersome to use (the sliders are not that smooth). By promoting the layers to class members (now they are only local variables) you could easily create your own LayerManager control that could better suit you needs.

Animating turning on/off layers

And if you start playing with making your own LayerManager control, you should use a tweening library such as TweenLite or Gtween to animate the transitions when turning on and off layers. This will make your maps seems less twitchy.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.116.62.45