当使用Google Maps API中的loadGeoJson()方法完成所有特性的绘制时,是否会触发一个事件?
我读到你可以监听地图的“空闲”状态,但似乎在加载完成后,但在绘制要素之前,地图被认为是空闲的。请看下面的小提琴:https://jsfiddle.net/z3tu0epb/
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()。
谢谢,
发布于 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>))的回调函数
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");
});
}#map {
height: 100%;
}
/* Optional: Makes the sample page fill the window. */
html,
body {
height: 100%;
margin: 0;
padding: 0;
}<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中为此类事件提交功能请求
https://stackoverflow.com/questions/46546201
复制相似问题