I would like to add a basemap layer to a custom web app made with arcgis api javascript framework syntax. I am using ArcGIS API 3.22 to call an arcgis online web map that has no basemap. I would like to set it using arcgis api for js. Can you add a baselayer with a map that has already been created? I cannot use something like:
var map = new esri.Map("mapDiv", { center: [-56.049, 38.485], zoom: 3, basemap: "streets" });
because the map has been created.
Thanks,
Brandon
Solved! Go to Solution.
Hi Robert,
Unfortunately no. I have used part of your script example. I believe I almost have it.
This is what I have:
AMD: require(["dojo/topic", "dijit/Dialog", "dojo/dom-style",
"dojo/dom", "dijit/form/Button", "dojo/on",
"dijit/registry", "esri/map", "esri/layers/VectorTileLayer", "dojo/domReady!"]
Function: function (topic, Dialog, domStyle,
dom, Button, on,
registry, Map, VectorTileLayer) {
Script: var myButton5 = dom.byId("togglebasemap");
on(myButton5, "click", function () {
var baseLyr = map.getLayer(map.layerIds[0]);
if(baseLyr.url === "https://www.arcgis.com/sharing/rest/content/items/0e0aa048cb9a42de91ae287fc5632fac/resources/styles/..."){
map.removeLayer(baseLyr);
var vectorTileLayer = new VectorTileLayer("http://www.arcgis.com/sharing/rest/content/items/e19e9330bf08490ca8353d76b5e2e658/resources/styles/r...");
map.addLayer(vectorTileLayer, 0);
} else if(baseLyr.url === "http://www.arcgis.com/sharing/rest/content/items/e19e9330bf08490ca8353d76b5e2e658/resources/styles/r..." ||
"http://public.gis.lacounty.gov/public/rest/services/LACounty_Cache/LACounty_Base/MapServer"){
map.removeLayer(baseLyr);
var vectorTileLayer2 = new VectorTileLayer("http://www.arcgis.com/sharing/rest/content/items/0e0aa048cb9a42de91ae287fc5632fac/resources/styles/r...");
map.addLayer(vectorTileLayer2, 0);
}
},
});
});
HTML: <button id="togglebasemap" data-dojo-type="dijit/form/Button" type="button" style="font-size:11px;">Basemap Toggle</button>
Brandon,
There are to many unknown variable here for me to just be able to look at what you provided be able to tell much. So, does the button show up? Does the click event get fired?
Hi Robert,
The button shows up in the map, but the click event does not fire. I tested this yesterday:
var myButton5 = dom.byId("togglebasemap");
on(myButton5, "click", function () {
alert("Hello! I am an alert box!!");
}
});
and got an alert message. The basemap script does not work nor does it fire on the other hand.
Brandon,
Did you change all occurrences of map to app.map?
This is what I have:
var togglebasemap = dom.byId("togglebasemap");
on(togglebasemap, "click", function () {
var baseLyr = app.map.getLayer(app.map.layerIds[0]);
if(baseLyr.url === "https://www.arcgis.com/sharing/rest/content/items/0e0aa048cb9a42de91ae287fc5632fac/resources/styles/..."){
app.map.removeLayer(baseLyr);
var tiled = new ArcGISTiledMapServiceLayer("http://public.gis.lacounty.gov/public/rest/services/LACounty_Cache/LACounty_Base/MapServer");
app.map.addLayer(tiled, 0);
} else if(baseLyr.url === "http://public.gis.lacounty.gov/public/rest/services/LACounty_Cache/LACounty_Base/MapServer"){
app.map.removeLayer(baseLyr);
var vectorTileLayer = new VectorTileLayer("http://www.arcgis.com/sharing/rest/content/items/0e0aa048cb9a42de91ae287fc5632fac/resources/styles/r...");
app.map.addLayer(vectorTileLayer, 0);
}
});
This works for now. I will repost the new script if I make changes.
Brandon
Great, be sure to mark this as answered then.
Thanks
Hi Robert,
I would also like to change the text of the button onclick using css. This is what I have:
CSS:
#togglebasemap {
position: absolute;
visibility: visible;
}
#togglebasemap: :after {
content:'Vector';
visibility: visible;
}
HTML:
<button id="togglebasemap" data-dojo-type="dijit/form/Button" type="button">Aerial</button>
It does not work. Is something wrong? Is this the best way if I get it to work or is this not the best way?
Brandon
No actually css is not the way to handle that. Here is some changes to your code:
require(["dojo/topic", "dijit/Dialog", "dojo/dom-style",
"dojo/dom", "dijit/form/Button", "dojo/on",
"dijit/registry", "esri/map", "esri/layers/VectorTileLayer", "dojo/domReady!"
]
Function: function(topic, Dialog, domStyle,
dom, Button, on,
registry, Map, VectorTileLayer) {
var myButton5 = dom.byId("togglebasemap");
on(myButton5, "click", function(evt) {
var baseLyr = map.getLayer(map.layerIds[0]);
if(baseLyr.url === "https://www.arcgis.com/sharing/rest/content/items/0e0aa048cb9a42de91ae287fc5632fac/resources/styles/...") {
map.removeLayer(baseLyr);
var vectorTileLayer = new VectorTileLayer("http://www.arcgis.com/sharing/rest/content/items/e19e9330bf08490ca8353d76b5e2e658/resources/styles/r...");
map.addLayer(vectorTileLayer, 0);
//This is where you would change the button text.
evt.target.innerHTML = "basemap1";
} else if(baseLyr.url === "http://www.arcgis.com/sharing/rest/content/items/e19e9330bf08490ca8353d76b5e2e658/resources/styles/r..." ||
"http://public.gis.lacounty.gov/public/rest/services/LACounty_Cache/LACounty_Base/MapServer") {
map.removeLayer(baseLyr);
var vectorTileLayer2 = new VectorTileLayer("http://www.arcgis.com/sharing/rest/content/items/0e0aa048cb9a42de91ae287fc5632fac/resources/styles/r...");
map.addLayer(vectorTileLayer2, 0);
//This is where you would change the button text.
evt.target.innerHTML = "basemap2";
}
},
});
});
Hi Robert,
This is what I have: