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

时间:2019-08-12 14:52:08

标签: javascript kml arcgis arcgis-js-api

是否可以使用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) });

现场直播:

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

控制台会打印出 KMLLayer 对象,并且 fullExtent 字段似乎设置不正确。

1 个答案:

答案 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