我正在尝试选择要素,并使用ArcGIS javascript API将地图缩放到所选要素。
我已经进行了检查,以确保查询返回的是实际结果,因此应该检索到数据。我将脚本基于FeatureLayer入门-4.11。代码在下面。
<script>
require([
"esri/Map",
"esri/views/MapView",
"esri/layers/FeatureLayer"
], function(Map, MapView, FeatureLayer) {
var map = new Map({
basemap: "hybrid"
});
var view = new MapView({
container: "viewDiv",
map: map,
extent: {
// autocasts as new Extent()
xmin: -9177811,
ymin: 4247000,
xmax: -9176791,
ymax: 4247784,
spatialReference: 102100
}
});
/********************
* Add feature layer
********************/
// Carbon storage of trees in Warren Wilson College.
var featureLayer = new FeatureLayer({
url:
"http://someURL/FeatureServer/45"
});
map.add(featureLayer);
const query = new Query();
query.where = "LASTNAME = 'GLOVER'";
query.outSpatialReference = { wkid: 102100 };
query.returnGeometry = true;
query.outFields = [ "LASTNAME" ];
featureLayer.queryFeatures(query).then(function(results){
var newextent = esri.graphicsExtent(results.features);
map.setExtent(newextent,true);
});
});
</script>
我本来希望地图缩放到选定多边形的程度,但事实并非如此。没有错误消息出现-地图仅显示原始范围。
希望有人可以指出我正确的方向。
答案 0 :(得分:0)
用const query = new Query();
替换行const query = featureLayer.createQuery();
,因为您没有在Query
中声明require
,例如:
require([
"esri/Map",
"esri/views/MapView",
"esri/layers/FeatureLayer",
"esri/tasks/support/Query"
], function(Map, MapView, FeatureLayer, Query) {
从ArcGIS Javascript API版本4. *开始,您可以从featureLayer
:https://developers.arcgis.com/javascript/latest/api-reference/esri-layers-FeatureLayer.html#createQuery创建查询,并使用MapView
https://developers.arcgis.com/javascript/latest/api-reference/esri-views-MapView.html#goTo来控制视图:
featureLayer.queryFeatures(query).then(function(results){
const features = results.features;
view.goTo(features[0].geometry);
});