map 和 Layer wkid 不同时如何缩放到特征选择?
How to zoom to feature selection when map and Layer wkid are different?
我有一个使用 Esri ArcGis Js Api v3.11 的地图服务。
在该地图上,用户可以查询每个 FeatureLayer 并返回一个简单的网格。行单击事件附加了以下处理程序:
_grid.on('dgrid-select', function(event) {
var data = event.rows[0].data;
//get the current selected featureLayer
//build a query against it, using the objectId
//zoom to position: https://developers.arcgis.com/javascript/3/jssamples/fl_zoomgrid.html
var layerUrl = dijit.byId("LayerSelectBox").get("value");
var url = lang.replace(_baseUrl, { layer: layerUrl });
var fl = new FeatureLayer(url, {
mode: FeatureLayer.MODE_SELECTION,
outFields: ["ObjectID"]
});
//clear selection
fl.clearSelection();
query.objectIds = [data.OBJECTID];
fl.selectFeatures(query, FeatureLayer.SELECTION_NEW, function(features) {
//zoom to the selected feature
var geometryExtent = features[0].geometry.getExtent().expand(5.0);
_map.setExtent(geometryExtent);
});
});
虽然查询及其周围的一切工作正常,但缩放到结果让我很头疼。该地图的空间参考 Wkid 为 102100,返回的几何图形为 102362。
尝试将地图的范围设置为几何的范围或以点为中心会导致以下错误:
Map: Geometry (wkid: 102362) cannot be converted to spatial reference
of the map (wkid: 102100)
.selectFeatures
(https://developers.arcgis.com/javascript/3/jsapi/featurelayer-amd.html#selectfeatures) 的文档只提供了这一点有用的信息:
The input query. The query object has the following restrictions to
avoid conflicts between layer and map properties.
- outFields specified by the query object are overridden by the outFields specified in the FeatureLayer constructor.
- The returnGeometry value specified by the query object is ignored and true is used.
- The outSpatialReference set by the query object is ignored and the map's spatial reference is used.
老实说,这让我有点困惑。如何将结果 sr 转换/转换为地图 sr 并将地图居中?通过在地图上单击它来查询 FeatureLayer,会出现一个小对话框 Window,带有开箱即用的“缩放到”按钮,因此该功能可供使用。我似乎只是在做一些根本性的错误。
最后,GeometryService 拯救了我:
fl.selectFeatures(query, FeatureLayer.SELECTION_NEW, function(features) {
var gmsvc = new GeometryService("url//to/Geometryservice");
var fooExtent = graphicUtils.graphicsExtent(features);
var projectParams = new ProjectParameters();
projectParams.geometries = [fooExtent];
projectParams.outSR = map.spatialReference;
gmsvc.project(projectParams).then(function(x) {
map.setExtent(x[0]);
})
});
我正在使用 graphicUtils 从一组几何图形计算范围。这不是绝对必要的,但由于我的结果可以是任何东西,从一个点到一个多边形,这是一个很好的接触并节省了一些开关语句。
接下来,我将使用我的几何服务将范围投影到地图的空间参考中,完成后,我将设置该范围。
我有一个使用 Esri ArcGis Js Api v3.11 的地图服务。
在该地图上,用户可以查询每个 FeatureLayer 并返回一个简单的网格。行单击事件附加了以下处理程序:
_grid.on('dgrid-select', function(event) {
var data = event.rows[0].data;
//get the current selected featureLayer
//build a query against it, using the objectId
//zoom to position: https://developers.arcgis.com/javascript/3/jssamples/fl_zoomgrid.html
var layerUrl = dijit.byId("LayerSelectBox").get("value");
var url = lang.replace(_baseUrl, { layer: layerUrl });
var fl = new FeatureLayer(url, {
mode: FeatureLayer.MODE_SELECTION,
outFields: ["ObjectID"]
});
//clear selection
fl.clearSelection();
query.objectIds = [data.OBJECTID];
fl.selectFeatures(query, FeatureLayer.SELECTION_NEW, function(features) {
//zoom to the selected feature
var geometryExtent = features[0].geometry.getExtent().expand(5.0);
_map.setExtent(geometryExtent);
});
});
虽然查询及其周围的一切工作正常,但缩放到结果让我很头疼。该地图的空间参考 Wkid 为 102100,返回的几何图形为 102362。
尝试将地图的范围设置为几何的范围或以点为中心会导致以下错误:
Map: Geometry (wkid: 102362) cannot be converted to spatial reference of the map (wkid: 102100)
.selectFeatures
(https://developers.arcgis.com/javascript/3/jsapi/featurelayer-amd.html#selectfeatures) 的文档只提供了这一点有用的信息:
The input query. The query object has the following restrictions to avoid conflicts between layer and map properties.
- outFields specified by the query object are overridden by the outFields specified in the FeatureLayer constructor.
- The returnGeometry value specified by the query object is ignored and true is used.
- The outSpatialReference set by the query object is ignored and the map's spatial reference is used.
老实说,这让我有点困惑。如何将结果 sr 转换/转换为地图 sr 并将地图居中?通过在地图上单击它来查询 FeatureLayer,会出现一个小对话框 Window,带有开箱即用的“缩放到”按钮,因此该功能可供使用。我似乎只是在做一些根本性的错误。
最后,GeometryService 拯救了我:
fl.selectFeatures(query, FeatureLayer.SELECTION_NEW, function(features) {
var gmsvc = new GeometryService("url//to/Geometryservice");
var fooExtent = graphicUtils.graphicsExtent(features);
var projectParams = new ProjectParameters();
projectParams.geometries = [fooExtent];
projectParams.outSR = map.spatialReference;
gmsvc.project(projectParams).then(function(x) {
map.setExtent(x[0]);
})
});
我正在使用 graphicUtils 从一组几何图形计算范围。这不是绝对必要的,但由于我的结果可以是任何东西,从一个点到一个多边形,这是一个很好的接触并节省了一些开关语句。
接下来,我将使用我的几何服务将范围投影到地图的空间参考中,完成后,我将设置该范围。