Hi,
It is quite simple to create tile server on localhost or another web server when you have images which can be identified by the zoom level and (x,y) position of the tile. All that you need to do is put your tiles in a separate directory in the C:\inetpub\wwwroot folder. So if you have the OpenStreet map tiles organized in a way similar to the structure on the OpenStreet servers (zoom\x_pos\y_pos.png) then you can create tile server on localhost in a couple of steps:
1. Create new folder like "os_images" in the "C:\inetpub\wwwroot" folder.
2. Copy whole structure of the OpenStreet images to this new folder.
Now you can create new map tile source and map provider which will access images from this folder:
/// <summary>
/// Tile source which read map tiles from the given URI.
/// </summary>
public class LocalhostTileSource : TiledMapSource
{
private string tileUriFormat;
/// <summary>
/// Initializes a new instance of the LocalhostTileSource class.
/// </summary>
/// <param name="tileUriFormat">Format string to access tiles on the localhost.</param>
public LocalhostTileSource(string tileUriFormat)
: base(1, 20, 256, 256)
{
this.tileUriFormat = tileUriFormat;
}
/// <summary>
/// Initialize provider.
/// </summary>
public override void Initialize()
{
// Raise provider intialized event.
this.RaiseIntializeCompleted();
}
/// <summary>
/// Gets the image URI.
/// </summary>
/// <param name="tileLevel">Tile level.</param>
/// <param name="tilePositionX">Tile X.</param>
/// <param name="tilePositionY">Tile Y.</param>
/// <returns>URI of image.</returns>
protected override Uri GetTile(int tileLevel, int tilePositionX, int tilePositionY)
{
int zoomLevel = ConvertTileToZoomLevel(tileLevel);
string url = this.tileUriFormat.Replace("{zoom}", zoomLevel.ToString(CultureInfo.InvariantCulture));
url = url.Replace("{x}", tilePositionX.ToString(CultureInfo.InvariantCulture));
url = url.Replace("{y}", tilePositionY.ToString(CultureInfo.InvariantCulture));
return new Uri(url);
}
}
/// <summary>
/// Map provider which read map tiles from the file system.
/// </summary>
public class LocalhostProvider : TiledProvider
{
/// <summary>
/// Initializes a new instance of the LocalhostProvider class.
/// </summary>
/// <param name="tileUriFormat">Format string to access tiles on the localhost.</param>
public LocalhostProvider(string tileUriFormat)
: base()
{
LocalhostTileSource source = new LocalhostTileSource(tileUriFormat);
this.MapSources.Add(source.UniqueId, source);
}
/// <summary>
/// Returns the SpatialReference for the map provider.
/// </summary>
public override ISpatialReference SpatialReference
{
get
{
return new MercatorProjection();
}
}
}
Now you can use new map provider in your application:
The ConvertTileToZoomLevel method is inherited from TiledMapSource class so you need not bother about its implementation.
The proxy tile server can be created in the same way. In this case you should create in addition simple page on your web site which will get image requests from your application, transfer it to the OpenStreet server and then return response. The code for the custom tile provider will be the same.
All the best,
Andrey Murzov
the Telerik team
Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>