Using map.setBasemap(); for web tiled layers

3394
2
Jump to solution
08-10-2015 06:31 AM
RobertWinterbottom
Occasional Contributor

I am just curious if it is possible to use web tile layers such as stamen or mapbox through:

map.setBasemap('myBasemap');

I know I can do something like this to get it to recognize me calling map.setBasemap('myBasemap'):

esri.basemaps['myBasemap'] = {
  baseMapLayers: [{
    url: 'http://{subDomain}.tile.stamen.com/watercolor/{level}/{col}/{row}.jpg'
  }],
  title: 'My Layer'
};

but the problem with this is it obviously wont treat the url as a tiled layer and ends up looking for something like the following:

http://%7Bsubdomain%7D.tile.stamen.com/watercolor/%7Blevel%7D/%7Bcol%7D/%7Brow%7D.jpg

Is there a good way to get the map to recognize tiled layers this way?

I am trying to avoid adding this as a web tiled layer on top of a basemap as I would like to add this to a custom basemap gallery and to be able to use this layer the same way as other basemaps via map.setBasemap().

0 Kudos
1 Solution

Accepted Solutions
KristianEkenes
Esri Regular Contributor

Robert,

I'm not sure that I understand your issue completely, but I'll give it a shot.

In 3.14, instead of using esri.basemaps, you can create your own basemap instance using the Basemap and BasemapLayer classes. You can then pass your own Basemap instance into the BasemapGallery. I did this with the Stamen toner tiled layer in this bin: JS Bin - Collaborative JavaScript Debugging

You mentioned map.setBasemap in your last comment. I don't believe you can use setBasemap() to switch over to your own. BasemapGallery and BasemapToggle will allow you to do this, however.

That being said, this will all be easier in the 4.0 API. You can already see how much easier it is by taking a look at this sample: JS Bin - Collaborative JavaScript Debugging where I create a Stamen basemap and pass it into the BasemapToggle widget.

What's different about this sample is that not only is constructing each instance simpler, but you can also use the new basemap instance to set your map's basemap. In 4.0, instead of map.setBasemap(), you can set it directly on the basemap property like this:

map.basemap = stamen;

That's all there is to it! Go ahead and try it in the sample I referenced: JS Bin - Collaborative JavaScript Debugging . Open the console and type map.basemap = stamen. Then try switching to the others as well.

Hopefully this helps answer your question.

View solution in original post

2 Replies
KristianEkenes
Esri Regular Contributor

Robert,

I'm not sure that I understand your issue completely, but I'll give it a shot.

In 3.14, instead of using esri.basemaps, you can create your own basemap instance using the Basemap and BasemapLayer classes. You can then pass your own Basemap instance into the BasemapGallery. I did this with the Stamen toner tiled layer in this bin: JS Bin - Collaborative JavaScript Debugging

You mentioned map.setBasemap in your last comment. I don't believe you can use setBasemap() to switch over to your own. BasemapGallery and BasemapToggle will allow you to do this, however.

That being said, this will all be easier in the 4.0 API. You can already see how much easier it is by taking a look at this sample: JS Bin - Collaborative JavaScript Debugging where I create a Stamen basemap and pass it into the BasemapToggle widget.

What's different about this sample is that not only is constructing each instance simpler, but you can also use the new basemap instance to set your map's basemap. In 4.0, instead of map.setBasemap(), you can set it directly on the basemap property like this:

map.basemap = stamen;

That's all there is to it! Go ahead and try it in the sample I referenced: JS Bin - Collaborative JavaScript Debugging . Open the console and type map.basemap = stamen. Then try switching to the others as well.

Hopefully this helps answer your question.

RobertWinterbottom
Occasional Contributor

Hi Kristian Ekenes​, that's pretty much it.  What your doing in 4.0 is similar to what I want to be doing in 3.14.

We are not using the Basemap Gallery widget and instead have designed our own. Behind the scenes of our basemap gallery we just call map.setBasemap('terrain') when they select terrain for example.

What I would like to do is have a way to call map.setBasemap('stamen') and have it work, but I am not sure that is possible.  In the past we did what you linked in your first example but were not using the default basemap gallery anymore.  I will try playing with the Basemap and BasemapLayer classes later on and see if I can use those to make this work but I have a feeling your right in that map.setBasemap will not work with my own layers unless they are simple map services.

On a side note, really looking forward to 4.0 because as you said, this would be much more trivial with that API.

0 Kudos