<script type="text/javascript"> dojo.require("dijit.layout.BorderContainer"); dojo.require("dijit.layout.ContentPane"); dojo.require("esri.map"); dojo.require("esri.tasks.locator"); dojo.require("esri.dijit.Popup"); dojo.require("esri.dijit.Legend"); dojo.require("dijit.form.CheckBox"); var map; var spatialReference; var locator; var identifyTask,identifyParams; var districtLayer; var pathName = "http://myserver/ArcGIS/rest/services"; var identifyLocator = false; var senateTemplate; var houseTemplate; var congressTemplate; function init() { spatialReference = new esri.SpatialReference({wkid: 102100 }); startExtent = new esri.geometry.Extent(-10583000, 4287025, -9979000, 4980462, spatialReference); //setup the popup window var popup = new esri.dijit.Popup({ fillSymbol: new esri.symbol.SimpleFillSymbol(esri.symbol.SimpleFillSymbol.STYLE_SOLID, new esri.symbol.SimpleLineSymbol(esri.symbol.SimpleLineSymbol.STYLE_SOLID, new dojo.Color([255,0,0]), 2), new dojo.Color([255,255,0,0.50])) }, dojo.create("div")); map = new esri.Map("map",{ infoWindow:popup, extent:startExtent }); locator = new esri.tasks.Locator("http://tasks.arcgis.com/ArcGIS/rest/services/WorldLocator/GeocodeServer"); dojo.connect(locator, "onAddressToLocationsComplete", showResults); var basemap = new esri.layers.ArcGISTiledMapServiceLayer("http://server.arcgisonline.com/ArcGIS/rest/services/Canvas/World_Light_Gray_Base/MapServer"); map.addLayer(basemap); districtLayer = new esri.layers.ArcGISDynamicMapServiceLayer(pathName+"/legisDistrict/MapServer",{id:'districts'}); map.addLayer(districtLayer); senateTemplate = new esri.dijit.PopupTemplate({ title: "Senate District {District}", mediaInfos: [{ "title": "{District}", "caption": "{Name}", "type": "image", "value": { "sourceURL": "{photoLink}", "linkURL": "" } }], fieldInfos: [ { fieldName: "webLink", label: "Open Web Page" , visible: true }] }); houseTemplate = new esri.dijit.PopupTemplate({ title: "House District {District}", mediaInfos: [{ "title": "{District}", "caption": "{Name}", "type": "image", "value": { "sourceURL": "{photoLink}", "linkURL": "" } }], fieldInfos: [ { fieldName: "webLink", label: "Open Representatives's Web Page" , visible: true }] }); dojo.connect(map,"onLoad",mapReady); } function mapReady(map){ //identify task is the same layer as districtLayer added above identifyTask = new esri.tasks.IdentifyTask(pathName + "/legisDistrict/MapServer"); identifyParams = new esri.tasks.IdentifyParameters(); identifyParams.tolerance = 3; // identifyParams.returnGeometry = true; identifyParams.layerIds = [0, 1, 2]; identifyParams.layerOption = esri.tasks.IdentifyParameters.LAYER_OPTION_VISIBLE; identifyParams.spatialReference = spatialReference; identifyParams.width = map.width; identifyParams.height = map.height; dojo.connect(map, "onClick", executeIdentifyTask); //resize the map when the browser resizes dojo.connect(dijit.byId('map'), 'resize', map, map.resize); } function executeIdentifyTask(evt) { identifyParams.mapExtent = map.extent; identifyParams.geometry = evt.mapPoint; var deferred = identifyTask.execute(identifyParams); //identify runs from geocoded point and not just click if (identifyLocator) { // geometry is coming from address function identifyParams.geometry = evt; } deferred.addCallback(function(response) { // response is an array of identify result objects // Let's return an array of features. return dojo.map(response, function(result) { var feature = result.feature; feature.attributes.layerName = result.layerName; if(result.layerName === 'senate'){ console.log("Senate District: " & feature.attributes.District); feature.setInfoTemplate(senateTemplate); } else if (result.layerName === 'house'){ console.log("House District: " & feature.attributes.District); feature.setInfoTemplate(houseTemplate); } else { console.log("congress not defined - create template for it"); } identifyLocator = false; return feature; }); }); map.infoWindow.setFeatures([ deferred ]); map.infoWindow.show(evt.mapPoint); } dojo.connect(map.infoWindow, "onHide", function() {map.graphics.clear();}); } dojo.addOnLoad(init); </script> </head> <body class="claro"> <div data-dojo-type="dijit.layout.BorderContainer" data-dojo-props="design:'headline'" style="width: 100%; height: 100%; margin: 0;"> <div id="mainHeader" dojotype="dijit.layout.ContentPane" region="top" > Missouri Legislative Districts <div id="subheader"> 2010 District Boundaries <div id="divAddress" style="position:absolute; top:10px; right:25px; z-index:999;"> <input id="txtAddress" style="width:250px" type="text" value="Find address or place" class="textBox" onclick="clearTextInput();" /> <button id="btnGO" dojotype="dijit.form.Button" onClick="locate();">GO</button> </div> </div> </div> <div id="map" data-dojo-type="dijit.layout.ContentPane" data-dojo-props="region:'center'" > <div dojoType="dijit.form.Button" id="btnHouse" class="togglebutton" onClick="toggleLayer(1);"> House Districts </div> <div dojoType="dijit.form.Button" id="btnSenate" class="togglebutton" onClick="toggleLayer(0);"> Senate Districts </div> <div dojoType="dijit.form.Button" id="btnCongress" class="togglebutton" onClick="toggleBaseLayer(2);"> Congressional Districts </div> </div> </div> </div> </body> </html>
Solved! Go to Solution.
#btnWrapper { position: absolute; z-index: 10; top: 0px; left: 30px; }
<div id="map" data-dojo-type="dijit.layout.ContentPane" data-dojo-props="region:'center'"> <div id="btnWrapper"> <div dojoType="dijit.form.Button" id="btnHouse" class="togglebutton" onClick="toggleLayer(1);">House Districts</div> <div dojoType="dijit.form.Button" id="btnSenate"class="togglebutton" onClick="toggleLayer(0);">Senate Districts</div> <div dojoType="dijit.form.Button" id="btnCongress"class="togglebutton" onClick="toggleBaseLayer(2);">Congressional Districts</div> </div> </div>
senateTemplate = new esri.dijit.PopupTemplate({ title: "Senate District {District}", mediaInfos: [{ "title": "{District}", "caption": "{Name}", "type": "image", "value": { "sourceURL": "{photoLink}", "linkURL": "" } }], fieldInfos: [ { fieldName: "District", label: "Senate District" , visible: true, format:{places: 0} } , {fieldName: "webLink", label: "Open Web Page" , visible: true }] }); houseTemplate = new esri.dijit.PopupTemplate({ title: "House District {District}", mediaInfos: [{ "title": "{District}", "caption": "{Name}", "type": "image", "value": { "sourceURL": "{photoLink}", "linkURL": "" } }], fieldInfos: [ { fieldName: "District", label: "House District" , visible: true, format:{places: 0} } , { fieldName: "webLink", label: "Open Web Page" , visible: true }] });
function executeIdentifyTask(evt) {
identifyParams.mapExtent = map.extent;
identifyParams.geometry = evt.mapPoint;
var deferred = identifyTask.execute(identifyParams);
//identify runs from geocoded point and not just click
if (identifyLocator) { // geometry is coming from address function
identifyParams.geometry = evt;
}
deferred.addCallback(function(response) {
// response is an array of identify result objects
// Let's return an array of features.
return dojo.map(response, function(result) {
var feature = result.feature;
feature.attributes.layerName = result.layerName;
if(result.layerName === 'senate'){
console.log("Senate District: " & feature.attributes.District);
feature.setInfoTemplate(senateTemplate);
}
else if (result.layerName === 'house'){
console.log("House District: " & feature.attributes.District);
feature.setInfoTemplate(houseTemplate);
} else {
console.log("congress not defined - create template for it");
}
identifyLocator = false;
return feature;
});
});
// InfoWindow expects an array of features from each deferred
// object that you pass. If the response from the task execution
// above is not an array of features, then you need to add a callback
// like the one above to post-process the response and return an
// array of features.
map.infoWindow.setFeatures([ deferred ]);
map.infoWindow.show(evt.mapPoint);
}