I'm not sure I completely understand your question but here are a few things I noticed: -you're using a map service in web mercator(wkid 102100) but specifying a geographic extent(wkid 4326). Use esri.geometry.geographicToWebMercator() to convert your extent before passing it to your map: var initialExtent = new esri.geometry.Extent({"xmin":-120.227821999585,"ymin":20.5332698820995,"xmax":-65.9816769016675,"ymax":71.4062353670687,"spatialReference":{"wkid":4326}}); map = new esri.Map("mapDiv", { extent: esri.geometry.geographicToWebMercator(initialExtent) }); -you're adding a tile map service but using the dynamic map service constructor. Use: var layer = new esri.layers.ArcGISTiledMapServiceLayer("http://server.arcgisonline.com/ArcGIS/rest/services/World_Street_Map/MapServer"); -to handle resizing of the browser use this: dojo.connect(map, 'onLoad', function() { // handle resize of the browser dojo.connect(dijit.byId('map'), 'resize', map,map.resize); //Listen for click event on the map, when the user clicks on the map call executeQueryTask function. dojo.connect(map, "onClick", executeQueryTask); }); Note: the map's onLoad event is also where you should connect your onClick handler Here's a tweaked version of your whole page: <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> <meta http-equiv="X-UA-Compatible" content="IE=7,IE=9" /> <!--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>QueryTask with geometry, results as an InfoWindow</title> <link rel="stylesheet" type="text/css" href="http://serverapi.arcgisonline.com/jsapi/arcgis/2.5/js/dojo/dijit/themes/claro/claro.css"> <style type="text/css"> html, body {height: 100%; width: 100%; margin: 1; padding: 0;} body{background-color:#730; overflow:hidden; font-family: "Trebuchet MS"; border:5px solid black} #map{overflow:hidden; padding:0;} .AppTitle{font-size:3em; color: yellow;} .esriScalebar{height: 60%;} </style> <script type="text/javascript" src="http://serverapi.arcgisonline.com/jsapi/arcgis/?v=2.5"></script> <script type="text/javascript" language="Javascript"> dojo.require("esri.map"); dojo.require("esri.tasks.query"); dojo.require("dojo.parser"); dojo.require("dijit.layout.BorderContainer"); dojo.require("dijit.layout.ContentPane"); dojo.require("esri.dijit.Scalebar"); var map, queryTask, query; var symbol, infoTemplate; function init() { var initialExtent = new esri.geometry.Extent({"xmin":-120.227821999585,"ymin":20.5332698820995,"xmax":-65.9816769016675,"ymax":71.4062353670687,"spatialReference":{"wkid":4326}}); map = new esri.Map("mapDiv", { extent: esri.geometry.geographicToWebMercator(initialExtent) }); //create and add new layer var layer = new esri.layers.ArcGISTiledMapServiceLayer("http://server.arcgisonline.com/ArcGIS/rest/services/World_Street_Map/MapServer"); map.addLayer(layer); dojo.connect(map, 'onLoad', function() { // handle resize of the browser dojo.connect(dijit.byId('map'), 'resize', map,map.resize); dojo.connect(map, "onClick", executeQueryTask); }); //build query task queryTask = new esri.tasks.QueryTask("http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/Demographics/ESRI_Census_USA/MapServer/5"); query = new esri.tasks.Query(); query.returnGeometry = true; query.outFields = ["STATE_NAME", "STATE_ABBR", "SQMI", "MALES", "FEMALES"]; //create the infoTemplate to be used in the infoWindow. //All ${attributeName} will be substituted with the attribute value for current feature. infoTemplate = new esri.InfoTemplate("${STATE_NAME}", "Abbreviation: ${STATE_ABBR}<br />Square Miles: ${SQMI}<br /> # of Males: ${MALES}<br /> # of Females: ${FEMALES}"); symbol = 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.5])); } function executeQueryTask(evt) { query.geometry = evt.mapPoint; queryTask.execute(query, showResults); } function showResults(featureSet) { //remove all graphics on the maps graphics layer map.graphics.clear(); var features = featureSet.features; dojo.forEach(features,function(feature){ var graphic = feature; graphic.setSymbol(symbol); //Set the infoTemplate. graphic.setInfoTemplate(infoTemplate); //Add graphic to the map graphics layer. map.graphics.add(graphic); }); dojo.connect(map.graphics, "onMouseMove", function(evt) { var g = evt.graphic; map.infoWindow.setContent(g.getContent()); map.infoWindow.setTitle(g.getTitle()); map.infoWindow.show(evt.screenPoint,map.getInfoWindowAnchor(evt.screenPoint)); }); dojo.connect(map.graphics, "onMouseOut", function() {map.infoWindow.hide();} ); } dojo.addOnLoad(init); </script> </head> <body class="claro"> Click on a state to get more info. When state is highlighted, move mouse over state to get more info. <div id="mapDiv" style="width:600px; height:600px; border:1px solid #000;"></div> </body> </script> </head> <body class="claro"> <div dojotype="dijit.layout.BorderContainer" design="headline" gutters="false" style="width: 100%; height: 100%;"> <div id="map" dojotype="dijit.layout.ContentPane" region="center"> </div> <div dojotype="dijit.layout.ContentPane" region="top" style="width: 100%; height: 20%;"> <div style="position: absolute; top: 60px; left:900px;"> <span class="AppTitle"> Newbie Test</span> </div> </div> </div> </body> </html>
var initialExtent = new esri.geometry.Extent({"xmin":-120.227821999585,"ymin":20.5332698820995,"xmax":-65.9816769016675,"ymax":71.4062353670687,"spatialReference":{"wkid":4326}}); map = new esri.Map("mapDiv", { extent: esri.geometry.geographicToWebMercator(initialExtent) });
var layer = new esri.layers.ArcGISTiledMapServiceLayer("http://server.arcgisonline.com/ArcGIS/rest/services/World_Street_Map/MapServer");
dojo.connect(map, 'onLoad', function() { // handle resize of the browser dojo.connect(dijit.byId('map'), 'resize', map,map.resize); //Listen for click event on the map, when the user clicks on the map call executeQueryTask function. dojo.connect(map, "onClick", executeQueryTask); });
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> <meta http-equiv="X-UA-Compatible" content="IE=7,IE=9" /> <!--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>QueryTask with geometry, results as an InfoWindow</title> <link rel="stylesheet" type="text/css" href="http://serverapi.arcgisonline.com/jsapi/arcgis/2.5/js/dojo/dijit/themes/claro/claro.css"> <style type="text/css"> html, body {height: 100%; width: 100%; margin: 1; padding: 0;} body{background-color:#730; overflow:hidden; font-family: "Trebuchet MS"; border:5px solid black} #map{overflow:hidden; padding:0;} .AppTitle{font-size:3em; color: yellow;} .esriScalebar{height: 60%;} </style> <script type="text/javascript" src="http://serverapi.arcgisonline.com/jsapi/arcgis/?v=2.5"></script> <script type="text/javascript" language="Javascript"> dojo.require("esri.map"); dojo.require("esri.tasks.query"); dojo.require("dojo.parser"); dojo.require("dijit.layout.BorderContainer"); dojo.require("dijit.layout.ContentPane"); dojo.require("esri.dijit.Scalebar"); var map, queryTask, query; var symbol, infoTemplate; function init() { var initialExtent = new esri.geometry.Extent({"xmin":-120.227821999585,"ymin":20.5332698820995,"xmax":-65.9816769016675,"ymax":71.4062353670687,"spatialReference":{"wkid":4326}}); map = new esri.Map("mapDiv", { extent: esri.geometry.geographicToWebMercator(initialExtent) }); //create and add new layer var layer = new esri.layers.ArcGISTiledMapServiceLayer("http://server.arcgisonline.com/ArcGIS/rest/services/World_Street_Map/MapServer"); map.addLayer(layer); dojo.connect(map, 'onLoad', function() { // handle resize of the browser dojo.connect(dijit.byId('map'), 'resize', map,map.resize); dojo.connect(map, "onClick", executeQueryTask); }); //build query task queryTask = new esri.tasks.QueryTask("http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/Demographics/ESRI_Census_USA/MapServer/5"); query = new esri.tasks.Query(); query.returnGeometry = true; query.outFields = ["STATE_NAME", "STATE_ABBR", "SQMI", "MALES", "FEMALES"]; //create the infoTemplate to be used in the infoWindow. //All ${attributeName} will be substituted with the attribute value for current feature. infoTemplate = new esri.InfoTemplate("${STATE_NAME}", "Abbreviation: ${STATE_ABBR}<br />Square Miles: ${SQMI}<br /> # of Males: ${MALES}<br /> # of Females: ${FEMALES}"); symbol = 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.5])); } function executeQueryTask(evt) { query.geometry = evt.mapPoint; queryTask.execute(query, showResults); } function showResults(featureSet) { //remove all graphics on the maps graphics layer map.graphics.clear(); var features = featureSet.features; dojo.forEach(features,function(feature){ var graphic = feature; graphic.setSymbol(symbol); //Set the infoTemplate. graphic.setInfoTemplate(infoTemplate); //Add graphic to the map graphics layer. map.graphics.add(graphic); }); dojo.connect(map.graphics, "onMouseMove", function(evt) { var g = evt.graphic; map.infoWindow.setContent(g.getContent()); map.infoWindow.setTitle(g.getTitle()); map.infoWindow.show(evt.screenPoint,map.getInfoWindowAnchor(evt.screenPoint)); }); dojo.connect(map.graphics, "onMouseOut", function() {map.infoWindow.hide();} ); } dojo.addOnLoad(init); </script> </head> <body class="claro"> Click on a state to get more info. When state is highlighted, move mouse over state to get more info. <div id="mapDiv" style="width:600px; height:600px; border:1px solid #000;"></div> </body> </script> </head> <body class="claro"> <div dojotype="dijit.layout.BorderContainer" design="headline" gutters="false" style="width: 100%; height: 100%;"> <div id="map" dojotype="dijit.layout.ContentPane" region="center"> </div> <div dojotype="dijit.layout.ContentPane" region="top" style="width: 100%; height: 20%;"> <div style="position: absolute; top: 60px; left:900px;"> <span class="AppTitle"> Newbie Test</span> </div> </div> </div> </body> </html>
Angemeldete Mitglieder können Beiträge verfassen, Updates folgen und mehr. Neu hier? Registriere ein kostenloses Konto.
Find useful guides, FAQs, and documents to help you navigate and make the most of Esri Community.