有没有办法使用 ArcGIS Online 中的 KMLLayer({ url: "my file"}) 方法计算从网络加载的 KML 图层的范围?从 AGOL 加载的 KML 具有有效的 fullExtent 属性,但从其他来源加载的 KML 似乎默认为整个世界,这没有用。
这是一个例子:
app.kml=new KMLLayer({ url: "my file" });
app.map.add(app.kml);
app.kml.load().then(function() { app.mapView.extent=app.kml.fullExtent; console.log(app.kml) });
直播地址:
控制台打印出KMLLayer对象,并且fullExtent字段似乎没有设置正确。
最佳答案
我同意,fullExtent
属性似乎不是您所期望的。我认为有两种解决方法:
编写一些代码来查询layerView以获取范围:
view.whenLayerView(kmlLayer).then(function(layerView) {
watchUtils.whenFalseOnce(layerView, "updating", function() {
var kmlFullExtent = queryExtent(layerView);
view.goTo(kmlFullExtent);
});
});
function queryExtent(layerView) {
var polygons = layerView.allVisiblePolygons;
var lines = layerView.allVisiblePolylines;
var points = layerView.allVisiblePoints;
var images = layerView.allVisibleMapImages;
var kmlFullExtent = polygons
.concat(lines)
.concat(points)
.concat(images)
.map(
graphic => (graphic.extent ? graphic.extent : graphic.geometry.extent)
)
.reduce((previous, current) => previous.union(current));
return kmlFullExtent;
}
示例 here .
-- 或 --
再次调用实用程序服务并使用“lookAtExtent”属性:
view.whenLayerView(kmlLayer).then(function(layerView) {
watchUtils.whenFalseOnce(layerView, "updating", function() {
// Query the arcgis utility and use the "lookAtExtent" property -
esriRequest('https://utility.arcgis.com/sharing/kml?url=' + kmlLayer.url).then((response) => {
console.log('response', response.data.lookAtExtent);
view.goTo(new Extent(response.data.lookAtExtent));
});
});
});
示例 here .
关于javascript - 从 ArcGIS Online 中的 KML 图层获取范围,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57463495/