Same basemap for all scripts

898
5
03-05-2014 03:07 AM
GloriaForthun1
New Contributor
I have multiple scripts that call for a basemap and I end up having a lot of basemaps in my application.  But if I take away a line of code  such as:
function init() {
        var startExtent = new esri.geometry.Extent(-83.5528, 31.7627, -77.3874, 35.5635, new esri.SpatialReference({wkid:4326}) );
   map = new esri.Map("mapDiv", { extent: startExtent });
        var streetMap = new esri.layers.ArcGISTiledMapServiceLayer("http://server.arcgisonline.com/ArcGIS/rest/services/ESRI_StreetMap_World_2D/MapServer");
   map.addLayer(streetMap);

Or:
map = new esri.Map("map",{
         basemap:"streets",
            center:[-81.045,33.998],
            zoom:7,
            sliderStyle:"small"
      });    

Then the script doesn't work.  But if I leave them in, then I have multiple maps in my application.  I think what I need to know is how do I get a basemap and keep it for use in for all the other scripts?   

Thanks.
0 Kudos
5 Replies
JohnGravois
Frequent Contributor
welcome to our forum!

i'm not quite sure i understand your question.  in your post you have supplied to code examples.  the first loads an Esri WGS84 tiled basemap as an ArcGISTiledMapServiceLayer after the map object has been instantiated.  the second specifies on of our default Web Mercator basemaps in the map constructor itself (which is actually just a shortcut to load a different specific ArcGISTiledMapServiceLayer immediately when the map loads.

if you need a basemap in any JavaScript application you write, you are responsible for using one of the techniques above to add it to the map.  hope that helps!
0 Kudos
GloriaForthun
New Contributor
Maybe I need to be more specific.  I have a script that I use for a Locator service and I use the first of the above references for the map.  Then I have another script for doing a basemap toggle which uses the second map reference.  So I have one application with my html and css codes and inside this I am calling 2 different javascripts.  What I want is one map for everything but what I get are 2 maps.  My map division is cut into 2 map divisions with each script using one division.  That doesn't work.  I want just one map.  But I just can't figure out how to get this to work.  Any help is appreciated.  Thanks.
0 Kudos
JohnGravois
Frequent Contributor
hi gloria,

its hard to say without seeing your code, but from the sounds of it you have to make sure you are only instantiating a single map object in your application.

ie: only one call like this
var map = new esri.Map(...


if you'd like to upload a .zipped copy of the app, i'd be happy to try and help further.
0 Kudos
GloriaForthun
New Contributor
Sure, here it is.  Thanks.
0 Kudos
JohnGravois
Frequent Contributor
gloria,
i'm not sure if your own scripts were located somewhere different with respect to your html file on your own web server, but i was able to see a map in your application after:

1. creating another folder inside the application folder called 'scripts' and moving the .js files inside:
yourfolder/test 12.html
yourfolder/scripts/ToolsGeoRoute.js
2. modifying the paths in 'test 12.html' to acknowledge this change and point to the files in a scripts folder one level deeper than where the .html file is
<script type="text/javascript" src="scripts/ToolsGeoRoute.js"></script>


additionally, i commented out the map initialization inside ToolsGeoRoute.js, because you are already creating a map in toggle.js

function init() {
    /*map = new esri.Map("map", {
        extent: new esri.geometry.Extent({"xmin":-83.5528,"ymin":31.7627,"xmax":-77.3874,"ymax":35.5635,"spatialReference":{"wkid":4326}})  
    });*/


hope that helps!
0 Kudos