function pointToExtent(map, pointX, pointY, tolerance){ var xmin = pointX - tolerance; var ymin = pointY - tolerance; var xmax = pointX + tolerance; var ymax = pointY + tolerance; return new esri.geometry.Extent(xmin, ymin, xmax, ymax, map.spatialReference) }
function zoomRow(id){ policeUnits2.clearSelection(); var query = new esri.tasks.Query(); query.objectIds = [id]; policeUnits2.selectFeatures(query,esri.layers.FeatureLayer.SELECTION_NEW,function(features){ extent = pointToExtent(map, features[0].geometry.x, features[0].geometry.y, 6) map.setExtent(extent); }); }
var highlightSymbol = new esri.symbol.SimpleMarkerSymbol({ "color": [255,255,255,64], "size": 12, "angle": -30, "xoffset": 0, "yoffset": 0, "type": "esriSMS", "style": "esriSMSCircle", "outline": { "color": [0,0,0,255], "width": 1, "type": "esriSLS", "style": "esriSLSSolid" } }); policeUnits2.setSelectionSymbol(highlightSymbol);
<!DOCTYPE html> <html> <head> <meta http-equiv="cache-control" content="max-age=0" /> <meta http-equiv="cache-control" content="no-cache" /> <META HTTP-EQUIV="Pragma" CONTENT="no-cache"> <META HTTP-EQUIV="Expires" CONTENT="-1"> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <!--The viewport meta tag is used to improve the presentation and behavior of the samples on iOS devices--> <meta name="viewport" content="initial-scale=1, maximum-scale=1,user-scalable=no"> <title>Police AVL</title> <link rel="stylesheet" href="http://js.arcgis.com/3.8/js/dojo/dijit/themes/claro/claro.css"> <link rel="stylesheet" href="http://js.arcgis.com/3.8/js/esri/css/esri.css"> <style type="text/css"> .dj_ie .infowindow .window .top .right .user .content { position: relative; } .dj_ie .simpleInfoWindow .content {position: relative;} </style> <style> html, body, #main { height: 100%; width: 100%; margin: 0; padding: 0; overflow:hidden; } #leftPane{ height:100%; width:25%; font-family:"Roboto Condensed", sans-serif; font-size: 0.90em; } #mapDiv { height:100%; width:75%; margin:0; padding:0; } #header{ font-weight:600; font-size:14pt; color:#666666; padding-left:20px; } #HomeButton { position: absolute; top: 90px; left: 20px; z-index: 50; } #BasemapToggle { position: absolute; top: 10px; right: 20px; z-index: 50; } #locateContainer{ position:absolute; right:10px; top:5px; border-radius:5px; } </style> <script src="http://js.arcgis.com/3.8/"></script> <script> var map; require([ "esri/map", "esri/InfoTemplate", "esri/layers/FeatureLayer", "dojo/parser", "esri/dijit/HomeButton", "esri/dijit/BasemapToggle", "dijit/layout/BorderContainer", "dijit/layout/ContentPane", "dijit/form/Button", "dijit/form/TextBox", "dojox/grid/DataGrid", "dojo/data/ItemFileReadStore", "dojo/domReady!" ], function( Map, InfoTemplate, FeatureLayer, parser, HomeButton, BasemapToggle ) { parser.parse(); map = new Map("mapDiv", { basemap: "topo", center: [-81.797, 26.153], zoom: 12 }); //Address Locator dojo.connect(dojo.byId("addressInput"), 'onkeyup',function(event){ if(event.keyCode == 13 && this.value.length>0){ locateAddress(this.value); } }); //Create Home Button var home = new HomeButton({ map:map }, "HomeButton"); home.startup(); //Create Basemap Toggle var toggle = new BasemapToggle ({ map:map, basemap: "hybrid" }, "BasemapToggle"); toggle.startup(); map.on("load", initOperationalLayer); function initOperationalLayer() { var infoTemplate = new InfoTemplate("${VehicleId}", "Speed: ${Velocity:NumberFormat} MPH"); var policeUnits = new FeatureLayer("http://GIS10204:6080/arcgis/rest/services/GeoEvent/gx440P/MapServer/0",{ mode: FeatureLayer.MODE_ONDEMAND, outFields: ["*"], infoTemplate: infoTemplate }); map.addLayer(policeUnits); policeUnits.setRefreshInterval(0.083); map.infoWindow.resize(155,75); } function locateAddress(atText){ var l = esri.tasks.Locator('http://gis.naplesgov.com/naplesgis/rest/services/Locators/Naples_Address_Locator/GeocodeServer'); l.outSpatialReference = map.spatialReference; var params = {address : {'Single Line Input': atText}}; l.addressToLocations(params, function(c){ if(c.length == 0){ alert("No location found at this address"); } var currentLoc = c[0].location; var g = new esri.Graphic(currentLoc); map.graphics.clear(); map.graphics.add(g); map.centerAndZoom(currentLoc,19); }, function(){ console.log("No location found at this address"); }) } policeUnits2 = new esri.layers.FeatureLayer ("http://GIS10204:6080/arcgis/rest/services/GeoEvent/gx440P/MapServer/0",{ mode:esri.layers.FeatureLayer.MODE_SELECTION, outFields:["VehicleId", "Velocity", "OBJECTID"] }); //define a selection symbol var highlightSymbol = new esri.symbol.SimpleMarkerSymbol({ "color":[255,255,255,64], "size":12, "angle":-30, "xoffset":0, "yoffset":0, "type":"esriSMS", "style":"esriSMSCircle", "outline":{ "color":[0,0,0,255], "width":1, "type":"esriSLS", "style":"esriSLSSolid" } }); policeUnits2.setSelectionSymbol(highlightSymbol); dojo.connect(policeUnits2,'onLoad',function(layer){ var query = new esri.tasks.Query(); query.where = "1=1"; layer.queryFeatures(query,function(featureSet){ var items = dojo.map(featureSet.features,function(feature){ return feature.attributes; }); var data = { identifier:"OBJECTID", items:items}; store = new dojo.data.ItemFileReadStore({data:data}); grid.setStore(store); grid.setSortIndex(1,"true"); //sort on VehicleId }); }); map.addLayer([policeUnits2]); //modify the grid so only the VehicleId field is sortable grid.canSort = function(col){ if(Math.abs(col) == 2) { return true; } else { return false; } }; function makeZoomButton(id){ var zBtn = "<div data-dojo-type='dijit/form/Button'><img src='images/zoom.png'"; zBtn = zBtn + " width='18' height='18'"; zBtn = zBtn + " onClick=\"zoomRow('"+id+"')\"></div>"; return zBtn; } function pointToExtent(map, pointX, pointY, tolerance){ var xmin = pointX - 100; var ymin = pointY - 100; var xmax = pointX + 100; var ymax = pointY + 100; return new esri.geometry.Extent(xmin, ymin, xmax, ymax, map.spatialReference) } function zoomRow(id){ policeUnits2.clearSelection(); var query = new esri.tasks.Query(); query.objectIds = [id]; policeUnits2.selectFeatures(query,esri.layers.FeatureLayer.SELECTION_NEW,function(features){ extent = pointToExtent(map, features[0].geometry.x, features[0].geometry.y, 6) map.setExtent(extent); //zoom to the selected feature //var unitExtent = features[0].geometry.getExtent().expand(1.0); //map.setExtent(unitExtent); }); } var cityLimits = new esri.layers.ArcGISDynamicMapServiceLayer("http://gis.naplesgov.com/naplesgis/rest/services/City/CityLimits/MapServer"); map.addLayer(cityLimits); var addressPoints = new esri.layers.ArcGISDynamicMapServiceLayer("http://gis.naplesgov.com/naplesgis/rest/services/City/AddressPointSlbl/MapServer"); map.addLayer(addressPoints); }); </script> </head> <body class="claro"> <div data-dojo-type="dijit/layout/BorderContainer" style="width: 100%; height: 100%"> <div data-dojo-type="dijit/layout/ContentPane" id="header" data-dojo-props="region:'top'">City of Naples Police AVL <div id="locateContainer"> <label>Address: </label> <input id='addressInput'></input> </div> </div> <div id="mapDiv" data-dojo-type="dijit/layout/ContentPane" data-dojo-props="region:'center'"> <div id="BasemapToggle" ></div> <div id="HomeButton"></div> </div> <div data-dojo-type="dijit/layout/ContentPane" id="leftPane" data-dojo-props="region:'leading'"> <table data-dojo-type="dojox/grid/DataGrid" jsid="grid" id="grid" selectionMode="none"> <thead> <tr> <th field="OBJECTID" formatter="makeZoomButton" width="25px"> <img alt="+" src="images/zoom.png"/> </th> <th field="VehicleId" width="100px">Vehicle Id</th> </tr> </thead> </table> </div> </div> </body> </html>
var grid1 = dijit.byId("grid"); on(grid1,"rowClick", onRowClickHandler) function onRowClickHandler(evt){ policeUnits2.clearSelection(); var id= grid1.getItem(evt.rowIndex).OBJECTID; var query = new esri.tasks.Query(); query.objectIds = [id]; policeUnits2.selectFeatures(query,esri.layers.FeatureLayer.SELECTION_NEW,function(features){ extent = pointToExtent(map, features[0].geometry.x, features[0].geometry.y) map.setExtent(extent); }); }
With the AMD model, it's not as easy to call a function from HTML. It's recommended to call the dom/dijit, and then execute the event. In the code you provided, you are calling the 'makeZoomButton' from the DataGrid. I couldn't figure out how to execute this function by calling the DataGrid dijit, however, I have a workaround that you can use. Add the following to your code:var grid1 = dijit.byId("grid"); on(grid1,"rowClick", onRowClickHandler) function onRowClickHandler(evt){ policeUnits2.clearSelection(); var id= grid1.getItem(evt.rowIndex).OBJECTID; var query = new esri.tasks.Query(); query.objectIds = [id]; policeUnits2.selectFeatures(query,esri.layers.FeatureLayer.SELECTION_NEW,function(features){ extent = pointToExtent(map, features[0].geometry.x, features[0].geometry.y) map.setExtent(extent); }); }
zBtn = zBtn + " onClick=\"zoomRow('"+id+"')\"></div>";
on(grid1, "rowClick", onRowClickHandler)
Jake,I just tried it, and it's still not zooming. Would I change the code in makeZoomButton below, that shows the zoomRow, since it is no longer there, and what would I replace it with?Sorry, I am new to this kind of coding, and I have just been taking code snippets out of ESRI samples and putting them together, so any help is appreciated.zBtn = zBtn + " onClick=\"zoomRow('"+id+"')\"></div>";
That was my mistake. I forgot to mention that you will need to import the dojo/dom and dojo/on modules. Try the attached.
Take a look at the jsfiddle here. The only thing I changed was the map extent, and the service to the Vehicle layer.
<!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>Cityof Naples Police AVL</title> <link rel="stylesheet" href="http://js.arcgis.com/3.8/js/dojo/dijit/themes/claro/claro.css"> <link rel="stylesheet" href="http://js.arcgis.com/3.8/js/dojo/dojox/grid/resources/Grid.css"> <link rel="stylesheet" href="http://js.arcgis.com/3.8/js/esri/css/esri.css"> <style> body,html,#main{margin:0;padding:0;height:100%;width:100%;} #map{ padding:0; border:solid 1px; } #HomeButton{ position:absolute; top:90px; left:20px; z-index:50; } #header{ font-weight:600; font-size:14pt; padding-left:20px; } #locateContainer{ position:absolute; right:10px; top:5px; border-radius:5px; } </style> <script>var dojoConfig = { parseOnLoad:true };</script> <script src="http://js.arcgis.com/3.8/"></script> <script> dojo.require("esri.map"); dojo.require("esri.layers.FeatureLayer"); dojo.require("dojo.parser"); dojo.require("dijit.layout.BorderContainer"); dojo.require("dijit.layout.ContentPane"); dojo.require("dijit.form.Button"); dojo.require("dijit.form.TextBox"); dojo.require("dojox.grid.DataGrid"); dojo.require("dojo.data.ItemFileReadStore"); dojo.require("dojo.data.ItemFileWriteStore"); var map, statesLayer; function init() { map = new esri.Map("map",{ basemap: "topo", center: [-81.797, 26.153], zoom: 12 }); //add the states demographic data var policeUnits = new esri.layers.ArcGISDynamicMapServiceLayer("http://GIS10204:6080/arcgis/rest/services/GeoEvent/gx440P/MapServer") map.addLayer(policeUnits); policeUnits.setRefreshInterval(0.083); //Address Locator dojo.connect(dojo.byId("addressInput"), 'onkeyup',function(event){ if(event.keyCode == 13 && this.value.length>0){ locateAddress(this.value); } }); function locateAddress(atText){ var l = esri.tasks.Locator('http://gis.naplesgov.com/naplesgis/rest/services/Locators/Naples_Address_Locator/GeocodeServer'); l.outSpatialReference = map.spatialReference; var params = {address : {'Single Line Input': atText}}; l.addressToLocations(params, function(c){ if(c.length == 0){ alert("No location found at this address"); } var currentLoc = c[0].location; map.centerAndZoom(currentLoc,19); }, function(){ console.log("No location found at this address"); }) } statesLayer = new esri.layers.FeatureLayer("http://GIS10204:6080/arcgis/rest/services/GeoEvent/gx440P/MapServer/0",{ mode:esri.layers.FeatureLayer.MODE_SELECTION, outFields:["VehicleId","Velocity","OBJECTID"] }); //define a selection symbol var highlightSymbol = new esri.symbol.SimpleMarkerSymbol({ "color":[0,112,255,255], "size":14, "angle":0, "xoffset":0, "yoffset":0, "type":"esriSMS", "style":"esriSMSDiamond", }); statesLayer.setSelectionSymbol(highlightSymbol); dojo.connect(statesLayer,'onLoad',function(layer){ var query = new esri.tasks.Query(); query.where = "1=1"; layer.queryFeatures(query,function(featureSet){ var items = dojo.map(featureSet.features,function(feature){ return feature.attributes; }); var data = { identifier:"OBJECTID", items:items}; store = new dojo.data.ItemFileReadStore({data:data}); grid.setStore(store); grid.setSortIndex(1,"true"); //sort on the state name }); }); map.addLayers([statesLayer]); var cityLimits = new esri.layers.ArcGISDynamicMapServiceLayer("http://gis.naplesgov.com/naplesgis/rest/services/City/CityLimits/MapServer"); map.addLayer(cityLimits); var addressPoints = new esri.layers.ArcGISDynamicMapServiceLayer("http://gis.naplesgov.com/naplesgis/rest/services/City/AddressPointSlbl/MapServer"); map.addLayer(addressPoints); //modify the grid so only the STATE_NAME field is sortable grid.canSort = function(col){ if(Math.abs(col) == 2) { return true; } else { return false; } }; } function pointToExtent(map, pointX, pointY){ var xmin = pointX - 100; var ymin = pointY - 100; var xmax = pointX + 100; var ymax = pointY + 100; return new esri.geometry.Extent(xmin, ymin, xmax, ymax, map.spatialReference) } function makeZoomButton(id){ var zBtn = "<div data-dojo-type='dijit.form.Button'><img src='images/zoom.png'"; zBtn = zBtn + " width='18' height='18'"; zBtn = zBtn + " onClick=\"zoomRow('"+id+"')\"></div>"; return zBtn; } function zoomRow(id){ statesLayer.clearSelection(); var query = new esri.tasks.Query(); query.objectIds = [id]; statesLayer.selectFeatures(query,esri.layers.FeatureLayer.SELECTION_NEW,function(features){ //zoom to the selected feature extent = pointToExtent(map, features[0].geometry.x, features[0].geometry.y) map.setExtent(extent); }); } //function(DataGrid, ItemFileWriteStore){ //if policeUnits.setRefreshInterval()==true; //var newStore = new ItemFileReadStore({data: {data}); //var grid = dijit.byId("gridId"); //grid.setStore(newStore); //}; //} dojo.ready(init); </script> </head> <body class="claro"> <div id="mainWindow" data-dojo-type="dijit.layout.BorderContainer" data-dojo-props="design:'headline'" style="width:100%; height:100%;"> <div data-dojo-type="dijit.layout.ContentPane" id="header" data-dojo-props="region:'top'">City of Naples Police AVL <div id="locateContainer"> <label>Address: </label> <input id='addressInput'> </div> </div> <div id="map" data-dojo-type="dijit.layout.ContentPane" data-dojo-props="region:'center'" > <div id="HomeButton"></div> </div> <div data-dojo-type="dijit.layout.ContentPane" data-dojo-props="region:'left'" style="width:142px"> <table data-dojo-type="dojox.grid.DataGrid" jsid="grid" id="grid" selectionMode="none"> <thead> <tr> <th field="OBJECTID" formatter="makeZoomButton" width="25px"> <img alt="+" src="images/zoom.png"/> </th> <th field="VehicleId" width="100px">Vehicle Id</th> </tr> </thead> </table> </div> </div> </body> </html>
Hi MichelleI have some time to take a look at your code.In the future, you should use http://jsfiddle.net to generate a working, live sample.It makes debugging much easier.
var home = new HomeButton
var home = new esri.dijit.HomeButton
require([ "esri/map", "esri/dijit/HomeButton" ], function (Map, HomeButton) { var home = new HomeButton(); });
dojo.requrie("esri.Map"); dojo.requrie("esri.dijit.HomeButton"); var home = new esri.dijit.HomeButton();
Aangemelde leden kunnen berichten plaatsen, updates volgen en meer. Nieuw hier? Registreer een gratis account.
Find useful guides, FAQs, and documents to help you navigate and make the most of Esri Community.