Hi all,
I really need your help! I am fairly new to the coding world, especially ArcGIS API for JavaScript. Let me get straight to the point. I am trying to create a web-based map application that contains information of Counties and Cities in California. Ultimately, when I select a particular jurisdiction, the map will automatically zoom into that jurisdiction and highlights its boundary. I also want to develop a popup window that illustrates some information of that selected jurisdiction.
I have been "googling" for a solution online and this is what I came up with so far. I got the list to populate but it doesn't zoom into the jurisdiction when I select it. Please look at my code below:
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta name="viewport" content="initial-scale=1, maximum-scale=1,user-scalable=no"/>
<title>Populate dropdown list with unique values</title>
<link rel="stylesheet" href="http://js.arcgis.com/3.10/js/dojo/dijit/themes/claro/claro.css">
<link rel="stylesheet" href="http://js.arcgis.com/3.10/js/esri/css/esri.css">
<style type="text/css">body,html,#main{margin:0;padding:0;height:100%;width:100%;}</style>
<script src="http://js.arcgis.com/3.10/"></script>
<script>
var map;
var resizeTimer;
require([
"esri/map", "esri/tasks/query", "dojo/parser", "dijit/layout/BorderContainer", "dijit/layout/ContentPane",
"dijit/form/ComboBox", "dojo/data/ItemFileReadStore","dojo/store/Memory", "dojo/on", "dojo/domReady!"],
function(Map, query, parser, BorderContainer, ContentPane, ComboBox, ItemFileReadStore, Memory, on) {
parser.parse();
map = new Map("map", {
basemap: "topo",
//center: [-122.45, 37.75], // longitude, latitude
center : [-120.676665, 37.306837],
zoom: 6
});
var queryTask = new esri.tasks.QueryTask("http://services.arcgis.com/IARhjUVNlIMumZL3/ArcGIS/rest/services/OTS_MAP/FeatureServer/1");
//Define query parameters
var query = new esri.tasks.Query();
query.outFields = ["NAME", "POP", "DVMT", "FATAL_OTS"];
query.returnGeometry = true;
query.where = "NAME <> ''"
queryTask.execute(query,populateList);
//var initialExtent = new esri.geometry.Extent(-85.915,38.105,-85.52,38.33,
// new esri.SpatialReference({wkid:4326}) );
// map = new esri.Map("map", {extent:initialExtent});
//Create tiled and dynamic map services and add to the map - for the dynamic service set the transparency
//and provide an id so we can access it later
map.addLayer(new esri.layers.ArcGISTiledMapServiceLayer("http://server.arcgisonline.com/ArcGIS/rest/services/ESRI_StreetMap_World_2D/MapServer"));
map.addLayer(new esri.layers.ArcGISTiledMapServiceLayer("http://services.arcgis.com/IARhjUVNlIMumZL3/ArcGIS/rest/services/CPP4OTS_MAP/MapServer",
{"opacity":.4,"id":"dynamic"}));
function populateList(results) {
//initialize InfoTemplate
infoTemplate = new esri.InfoTemplate("${NAME}", "County : ${NAME}<br/> Population : ${POP}<br/> Daily VMT : ${DVMT} <br/> Fatal OTS : ${FATAL_OTS}");
//create symbol for selected features
symbol = new esri.symbol.SimpleMarkerSymbol();
symbol.setStyle(esri.symbol.SimpleMarkerSymbol.STYLE_SQUARE);
symbol.setSize(10);
symbol.setColor(new dojo.Color([255, 255, 0, 0.5]));
//Populate the dropdown list box with unique values
var county;
var values = [];
var testVals={};
//Add option to display all zoning types to the dropdown list
values.push({name:"ALL"})
var features = results.features;
dojo.forEach (features, function(feature) {
county = feature.attributes.NAME;
if (!testVals[county]) {
testVals[county] = true;
values.push({name:county});
}
});
var dataItems = {
identifier: 'name',
label: 'name',
items: values
};
var store = new dojo.store.Memory({data:dataItems});
dijit.byId("mySelect").store = store;
//remove all graphics on the maps graphics layer
map.graphics.clear();
//Performance enhancer - assign featureSet array to a single variable.
var resultFeatures = results.features;
//Loop through each feature returned
for (var i = 0, il = resultFeatures.length; i < il; i++) {
//Get the current feature from the featureSet.
//Feature is a graphic
var graphic = resultFeatures;
graphic.setSymbol(symbol);
//Set the infoTemplate.
graphic.setInfoTemplate(infoTemplate);
//Add graphic to the map graphics layer.
map.graphics.add(graphic);
// This takes the graphics array you get back from your query and
// gets the overall extent for them. Make sure return geometry is set to
// true in your query.
var extent = esri.graphicsExtent(results.features);
// Use that to set the extent, 1.5 is something I use in my app, but play with
// it to find a setting you like, setting the second parameter to true will get you an extend
// that is at the closest level of your cached service.
map.setExtent(extent.expand(1.7), true);
}
}
// this replaces your applyLayerDef() function
dijit.byId("mySelect").on("change", function () {
//Filter the layer to display only the selected zoning types
var county = dijit.byId("mySelect").value;
if (county !== 'ALL') {
var layerDefs = [];
layerDefs[2] = "NAME = " + "'" + county + "'";
layerDefs.visibleLayers = [2];
map.getLayer("dynamic").setLayerDefinitions(layerDefs);
}
else {
map.getLayer("dynamic").setDefaultLayerDefinitions();
}
});
function resizeMap() {
//Handle browser resize
clearTimeout(resizeTimer);
resizeTimer = setTimeout(function() {
map.resize();
map.reposition();
}, 800);
}
});
</script>
</head>
<body class="claro">
<div id="main"
dojotype="dijit.layout.BorderContainer"
design="headline"
gutters="true">
<div id="header"
dojotype="dijit.layout.ContentPane"
region="top"
style="height:25px;">
<select id="mySelect"
dojotype="dijit.form.ComboBox"
style="width:300px;font-size:18px;"
autoComplete="true"
forceValidOption="false"
value="Select Zoning Type"></select>
</div>
<div id="map"
dojotype="dijit.layout.ContentPane"
region="center"
style="border:1px solid #000;margin:5px">
</div>
</div>
</body>
</html>
Solved! Go to Solution.
Riyas,
This is EXACTLY what I have imagined! You are such a life-saver! Thank you very much. I wish I could give you extra bonus points.
Tom
Hi Tom,
Glad it worked, Happy to help!!!
Regards
Riyas
Hi Riyas,
I need your help again. Do you have knowledge in working with asp.net? I'm trying to import my javascript that you helped me with into a webpage I'm working on..but the function doesn't seem to work properly.
Please let me know.
Thank you,
Tom
Hi Tom,
Yes I do, Let me know what the issue is, I'll see if i can be of any help here.
Hi Riyas,
We are creating a website using 2012 Visual Studio and 2012 SQL Server Management Studio Express. We want have drop-down lists that will generate tables from SQL and activate the map functions (the JavaScript code that you have helped me with) after selecting a jurisdiction.
We got the map to work with the drop-down list at certain points. Also, most of the map functions does not work at the moment because we didn't know how to make them compatible with asp.net (i.e. boundary highlighting and automatic popup info window). Only zoom function is working for now.
Anyways, when selecting a jurisdiction from a drop-down list, the map zooms into the selected boundary; but when I click the "confirm" button to generate tables, it refreshes the page, generates tables, and also refreshes the map extent. The map zooms back out to my original extent (California boundary) after I click the "confirm" button.
We are thinking about using UpdatePanel to force the map to not refresh but we couldn't get it to work. And I don't know if this is the right solution. That is our problem currently.
Thanks for your help.
Hi Tom,
Can you post some code? It'll be easier to analyse the issue with the code.
Hi Tom,
looks like your page is getting posted backed, you may want to explore using callback (Ajax). Google with key phrase postback and callback.
ArcGIS Javascript API is a client side scripting, when you postback your page, the whole page gets reloaded. This will cause your map to reload as well. Using a callback will prevent your page from reloading.
Also, as a thumb rule i generally try to avoid using server controls (one's with <asp: ) in my applications. I prefer working with client control and callbacks.
Hi Riyas,
i tried to implement your method by using update pane but I couldn't get it to work. when I run my map page, it generates errors.
Do I put the code directly into my map.aspx? Or in the site.master?
Thank you for your help,
Tom
Hi Riyas,
I finally got the map to work properly by using updatepanel function. Do you have any suggestion on how to make my all map functions to work in asp.net? The functions that are not working are "highlighting" and "automatic popup info window".
Tom