从 ArcGIS Online 中的 KML 图层获取范围

Get extent from KML layer in ArcGIS Online

有什么方法可以使用 KMLLayer({ url: "my file" }) 方法计算从网络加载的 KML 图层的范围在 ArcGIS Online 中?从 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) });

现场直播:

http://viseyes.org/visualeyes/test.htm?kml=https://www.arcgis.com/sharing/rest/content/items/a8efe6f4c12b462ebedc550de8c73e22/data

控制台打印出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.