首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >确定在Google Maps API中完成Geojson绘制的时间

确定在Google Maps API中完成Geojson绘制的时间
EN

Stack Overflow用户
提问于 2017-10-03 21:48:35
回答 1查看 485关注 0票数 1

当使用Google Maps API中的loadGeoJson()方法完成所有特性的绘制时,是否会触发一个事件?

我读到你可以监听地图的“空闲”状态,但似乎在加载完成后,但在绘制要素之前,地图被认为是空闲的。请看下面的小提琴:https://jsfiddle.net/z3tu0epb/

代码语言:javascript
复制
var map;

function initMap() {
  map = new google.maps.Map(document.getElementById('map'), {
    zoom: 12,
    center: new google.maps.LatLng(40.755690, -73.975938)
  });

  // Load GeoJSON.
  map.data.loadGeoJson(
    'https://services5.arcgis.com/GfwWNkhOj9bNBqoJ/arcgis/rest/services/nybb/FeatureServer/0/query?where=1=1&outFields=*&geometryPrecision=8&outSR=4326&f=geojson');

  google.maps.event.addListenerOnce(map, 'idle', function() {
    alert("map is idle");
  });

}

我还知道在向地图添加任何要素时触发的addFeature()侦听器,但我需要在所有要素添加到地图后运行alert()

谢谢,

EN

回答 1

Stack Overflow用户

发布于 2017-10-05 20:47:24

恐怕没有可行的方法来捕获数据层的“绘制所有功能后”事件。如果数据层公开了内部使用的绘图管理器实例,则可能会发生这种情况。在这种情况下,您可以侦听overlaycomplete事件

https://developers.google.com/maps/documentation/javascript/reference#OverlayCompleteEvent

但是数据层并不公开它的绘图管理器实例,所以你不能添加一个监听器。

https://developers.google.com/maps/documentation/javascript/reference#Data

您可以做的唯一一件事是弄清楚所有功能何时加载(添加到集合中)。在这种情况下,您可以使用loadGeoJson(url:string, options?:Data.GeoJsonOptions, callback?:function(Array<Data.Feature>))的回调函数

代码语言:javascript
复制
var map;

function initMap() {
  map = new google.maps.Map(document.getElementById('map'), {
    zoom: 12,
    center: new google.maps.LatLng(40.755690, -73.975938)
  });

  // Load GeoJSON.
  map.data.loadGeoJson(
    'https://services5.arcgis.com/GfwWNkhOj9bNBqoJ/arcgis/rest/services/nybb/FeatureServer/0/query?where=1=1&outFields=*&geometryPrecision=8&outSR=4326&f=geojson',
    {},
    function(features) {
        alert("Loaded " + features.length + " features");
    });

  google.maps.event.addListenerOnce(map, 'idle', function() {
    alert("map is idle");
  });
  
  

}
代码语言:javascript
复制
#map {
  height: 100%;
}


/* Optional: Makes the sample page fill the window. */

html,
body {
  height: 100%;
  margin: 0;
  padding: 0;
}
代码语言:javascript
复制
<div id="map"></div>
<!-- Replace the value of the key parameter with your own API key. -->
<script async defer src="https://maps.googleapis.com/maps/api/js?key=AIzaSyDztlrk_3CnzGHo7CFvLFqE_2bUKEq1JEU&callback=initMap">


</script>

此外,您也可以在Google issue tracker中为此类事件提交功能请求

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/46546201

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档