Designing a Map Cache


 

Map caches work best with maps that do not change frequently. These include street maps, imagery, and terrain maps. We will use a street map in the following sample. The full code of this sample is available in the standard samples (see the MapCaching.aspx sample).

 

1. When you cache a map, you draw it at more than one scale so that users can zoom in and out of the map. When choosing properties for your cache, you'll need to determine what scales you want to use for caching. In the code below, a map of a county is used. The map has the 1:228000 scale at its full extent. The scale is divided by two to create seven zoom levels, from 0 to 6.

 

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)

map.ZoomLevels.Add(228000) ' 0
map.ZoomLevels.Add(114000) ' 1
map.ZoomLevels.Add(57000)  ' 2
map.ZoomLevels.Add(28500)  ' 3
map.ZoomLevels.Add(14250)  ' 4
map.ZoomLevels.Add(7125)   ' 5
map.ZoomLevels.Add(3562)   ' 6

 

Next, set the ImageOpacity property of the Map control to 0.3 to make the main map image semi-transparent. This is required because tile layers are always displayed below other layers. The ImageFormat property is set to the PNG format because it supports semi-transparent images.

 

map.ImageOpacity = 0.3
map.ImageFormat = ImageFormat.Png

 

Finally, call the method that adds the tile layer to the Map control.

 

AddTileLayer()

End Sub

2. To make a cache, create an instance of the TileLayer class. The instance will be shared between multiple users of your application and it is necessary to assign it a global identifier to ensure that it is created only once. In the code below the identifier is 'CountyMap'.


Protected Sub AddTileLayer()
' try to create a new TileLayer
Dim tileLayer As TileLayer = TileLayer.Create("CountyMap")

If tileLayer Is Nothing Then
        ' the TileLayer has been already created, add it to the map
        tileLayer = TileLayer.Find("CountyMap")
        map.AddLayer(tileLayer)
        Return
End If

3. We use the TileLayer class to design a map by adding layers and setting their properties just as described in the tutorial for ordinary maps (see the full code in the MapCaching.aspx sample).
 

' add layers to the TileLayer
Dim layer As AspMap.Layer
layer = tileLayer.AddLayer(MapPath("MAPS/STREETS/County.shp")
layer.Symbol.Size = 2
layer.Symbol.LineColor = Color.FromArgb(199, 172, 116)
layer.Symbol.FillColor = Color.FromArgb(242, 236, 223)

layer = tileLayer.AddLayer(MapPath("MAPS/STREETS/Street.shp")
layer.MaxScale = 150000
layer.LabelField = "NAME"
layer.LabelFont.Size = 10
layer.LabelMaxScale = 37000
layer.ShowLabels = True
...

4. Now, set the properties of the cache. First, enable client-side caching of the tiles in the browser's cache. Next, set the tile cache mode to allow the tiles to be created dynamically, that is, when someone first visits them. You can also choose to pre-create the tiles using the GenerateTiles method for each zoom level.

 

' enable client-side caching of tile images
tileLayer.EnableClientCache = True

' enable dynamic caching
tileLayer.TileCacheMode = TileCacheMode.Dynamic

The tiles will be stored in a cache directory, you will need to create it in your application's root directory. The name of the cache directory in the code below is 'TileCache'. Set the maximum size of this directory to 300 megabytes.


' set a directory where the TileLayer will store dynamically generated tile images
' this directory must have read/write/delete permissions
tileLayer.TileCacheDirectory = "~/TileCache"

' set the maximum size of the tile cache directory to 300 megabytes
tileLayer.MaxDynamicCacheSize = 300


5. Call the Initialize method. This method lets you specify the number of concurrent requests that can be handled at the same time. Memory resources required to handle a single request are pre-allocated when the Initialize method is called. Handling each additional request will require an additional amount of memory as for the single request.
As a starting point, set this parameter to the number of available processors. This way the load will be distributed evenly among all the processors.


' initialize the TileLayer
tileLayer.Initialize(System.Environment.ProcessorCount)

6. Finally, add the tile layer to the Map control.
 

' add the TileLayer to the map
map.AddLayer(tileLayer)

End Sub

Cache directory structure

You can open the cache directory in Windows Explorer to examine the tiles. The tiles are organized into a directory hierarchy. The top-level directory has the same name as the global identifier of the TileLayer class - 'CountyMap' in our example. Next, you will find subdirectories for each scale/level, and finally, the actual image files that represent a particular row/column.

 

Updating a map cache

When you create a map cache, you are creating a snapshot of your map at the time you create the cache. When you edit or update the data, layer styles, or add new layers to the TileLayer class, you have to clean up the cache. To clean up the cache, just delete the top-level directory ('CountyMap' in our example) from the cache directory. Also, people who have used the cache need to delete any outdated tiles that may be stored on their machines.

 

 

 

 

 

© 2018 VDS Technologies
Converted from CHM to HTML with chm2web Standard 2.85 (unicode)