是否可以使用ArcGIS Online中的 KMLLayer({url:“我的文件”})方法来计算从Web加载的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 字段似乎设置不正确。
答案 0 :(得分:1)
我同意,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。