How to add Tile Layer in Azure Maps Web SDK MapControl with custom Header

Mandeep Chauhan 21 Reputation points
2023-05-18T04:03:15.4733333+00:00

I am trying to consume Azure Maps Web SDK in my solution for rendering a Tile Layer from our own tile server.

TileLayerOptions can be used to specify tileUrl of our tile server. The blocker I am facing is that I need to add a specific Header with all the requests going to tileUrl. Is there a way to customise tileUrl requests to add a header?

Azure Maps
Azure Maps

An Azure service that provides geospatial APIs to add maps, spatial analytics, and mobility solutions to apps.


1 answer

Sort by: Most helpful
  1. rbrundritt 20,451 Reputation points Volunteer Moderator
    2023-05-18T16:15:46.1166667+00:00

    You can set headers on any request made by the map, tiles and others. The trick is to use the maps transformRequest option. Here is an example of setting custom headers.

    map = new atlas.Map('myMap', {
    	//Transform certain requests made by the map.
    	transformRequest: function (url, resourceType) {
    		//Check to see if a tile is being requested. If so, apply some sort of logic to check if the tile is related to the service request you want to modify.
    		if (resourceType === 'Tile' && url.startsWith('https://myTileServerDomain/path')) {
    		
    			//Return the URL and custom header. Optionaly modify the URL if desired.	
                return {
                    url: url,
                    headers: { 'myCustomHeader': 'myCustomHeaderValue' }
                };
            }
    
    		//Return the URL if no changes.
    		return { url: url };
    	},
    
    	authOptions: {
    		authType: 'subscriptionKey',
    		subscriptionKey: '<Your Azure Maps Key>'
    	}
    });
    

    Also, worth noting this same method can be used to modify requests, such as redirecting requests through a proxy service.

    Was this answer helpful?

    3 people found this answer helpful.

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.