首先,html页面中加一个普通的按钮
类似:input type="button" value="点" onClick="executequery(); "
以下为JavaScript代码:
dojo.require("esri.map");
dojo.require("esri.tasks.query");
var map, queryTask, query, draw;
var featureSet;
function init() {
var startExtent = new esri.geometry.Extent(-100.7, 36.8, -95.8, 40.2, new esri.SpatialReference({wkid:4326}) );
map = new esri.Map("mapDiv", {extent:startExtent});
var tiledLayer = new esri.layers.ArcGISTiledMapServiceLayer("http://server.arcgisonline.com/ArcGIS/rest/services/ESRI_Imagery_World_2D/MapServer");
map.addLayer(tiledLayer);
var dynamicLayer = new esri.layers.ArcGISDynamicMapServiceLayer("http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/Petroleum/KGS_OilGasFields_Kansas/MapServer");
map.addLayer(dynamicLayer);
//dojo.connect(map, "onClick", executeQueryTask);
draw = new esri.toolbars.Draw(map);
dojo.connect(map.infoWindow, "onHide", function() {map.graphics.clear();});
queryTask = new esri.tasks.QueryTask("http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/Petroleum/KGS_OilGasFields_Kansas/MapServer/0");
query = new esri.tasks.Query();
query.returnGeometry = true;
query.outFields = ["FIELD_NAME", "FIELD_KID", "PROD_GAS", "PROD_OIL", "STATUS"];
}
function executequery(){
draw.activate('point');
draw.onDrawEnd = function doQuery(geometry){
try{
map.infoWindow.hide();
map.graphics.clear();
featureSet = null;
query.geometry = geometry;
queryTask.execute(query, function(fset) {
showFeature(fset.features[0],geometry);
});
}catch(e){alert(e.message);}
};
}
function showFeature(feature,geometry) {
try{
map.graphics.clear();
var 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]));
feature.setSymbol(symbol);
var attr = feature.attributes;
var title = attr.FIELD_NAME;
var content = "Field ID : " + attr.FIELD_KID
+ "<br />Produces Gas : " + attr.PROD_GAS
+ "<br />Produces Oil : " + attr.PROD_OIL
+ "<br />Status : " + attr.STATUS;
map.graphics.add(feature);
map.infoWindow.setTitle(title);
map.infoWindow.setContent(content);
console.log(geometry);
var screenPoint = toScreenPoint(geometry);
console.log(screenPoint);
(geometry) ? map.infoWindow.show(screenPoint,map.getInfoWindowAnchor(screenPoint)) : null;
}catch(e){
alert(e.message);
}
}
function toScreenPoint(_point)
{
return esri.geometry.toScreenPoint(map.extent, map.width, map.height, _point);
}
dojo.addOnLoad(init);