Completed
Last Updated: 22 Apr 2019 13:27 by ADMIN
Release R2 2019
ADMIN
Martin Ivanov
Created on: 22 Aug 2017 08:35
Category: Map
Type: Feature Request
2
Map: Expose a mechanism for providing an API key to the OpenStreetMapProvider
There is a change in the Open Street Map Cycle and CylcleTransport services and they require an API key. Without it the service will returned tiles with a watermark saying "API KEY REQUIRED". Currently, the OpenStreetProvider doesn't expose a mechanism for providing a key. 

At this point you can provide a key by creating a custom map provider deriving from OpenStreeMapProvider and also a custom OsmTileMapSource. And override the original URL to use also an API Key.

public class CustomOpenStreetMapProvider : OpenStreetMapProvider
{
    public CustomOpenStreetMapProvider()
    {
        string sourceKey = (typeof(OpenStreetCycleTransportSource)).FullName;
        this.MapSources[sourceKey] = new CustomOpenStreetCycleTransportSource("your api key here");           
    }
}
 
public class CustomOpenStreetCycleTransportSource : OsmTileMapSource
{
    private static string UrlFormat = @"http://{prefix}.tile.thunderforest.com/transport/{zoom}/{x}/{y}.png?apikey={apikey}";
    private string apiKey;
 
    public CustomOpenStreetCycleTransportSource(string apiKey)
        : base(UrlFormat)
    {
        this.apiKey = apiKey;
    }
 
    protected override Uri GetTile(int tileLevel, int tilePositionX, int tilePositionY)
    {
        Uri originalUri = base.GetTile(tileLevel, tilePositionX, tilePositionY);
        string newUri = originalUri.OriginalString.Replace("{apikey}", this.apiKey);
        return new Uri(newUri);
    }
}

<telerik:RadMap>
    <telerik:RadMap.Provider>
        <local:CustomOpenStreetMapProvider/>
    </telerik:RadMap.Provider>
</telerik:RadMap>
0 comments