添加监听器时未加载Google Maps V3地图
Google Maps V3 Map not loaded when adding listener
我正在尝试添加一个idle
监听器到谷歌地图。
问题:当我如下所示添加侦听器时,我得到错误Cannot read property '__e3_' of undefined
google.maps.event.addListener(map, 'idle', function() {
console.log('hello');
}
我通过添加setTimeout(..., 1000)
来解决它,以确保地图在一秒钟后加载。
:
- 这个错误是由于地图没有被加载吗?
- 这是最好的解决方法吗?
- 这个问题应该发生吗?我猜,如果我添加这个相同的监听器映射没有其他代码,这个错误不会弹出。
编辑
Map初始化
<script type="text/javascript">
var map;
var geocoder;
var directionsService = new google.maps.DirectionsService();
function initialize() {
var center_latlng = new google.maps.LatLng(42.354183,-71.065063);
var options = {
zoom: 15,
minZoom: 11,
maxZoom: 19,
center: center_latlng,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
map = new google.maps.Map(document.getElementById("map_canvas"), options);
var Style = [
{
featureType: "poi",
elementType: "labels",
stylers: [
{ visibility: "off" }
]
},{
featureType: "landscape",
elementType: "labels",
stylers: [
{ visibility: "off" }
]
}
]
map.setOptions({styles: Style});
geocoder = new google.maps.Geocoder();
// Marker Clusterer
var styles = {styles: [{
height: 34,
url: "images/template/markers/cluster.png",
width: 34,
textColor: '#FFF',
textSize: 12
},
{
height: 56,
url: "http://google-maps-utility-library-v3.googlecode.com/svn/trunk/markerclusterer/images/m2.png",
width: 56
},
{
height: 66,
url: "http://google-maps-utility-library-v3.googlecode.com/svn/trunk/markerclusterer/images/m3.png",
width: 66
},
{
height: 78,
url: "http://google-maps-utility-library-v3.googlecode.com/svn/trunk/markerclusterer/images/m4.png",
width: 78
},
{
height: 90,
url: "http://google-maps-utility-library-v3.googlecode.com/svn/trunk/markerclusterer/images/m5.png",
width: 90
}]
};
var mcOptions = {gridSize: 50, maxZoom: 15, styles: styles['styles']};
mc = new MarkerClusterer(map, [], mcOptions);
}
</script>
1。发生这种类型的错误是因为您试图访问一个在尝试附加侦听器时不存在的对象。监听器代码必须附加在包含Google地图对象的map变量之后。你什么时候尝试连接听众?我在初始化代码中没有看到它。
2)。不,超时不可靠。如果初始化存在延迟,则映射对象仍可能无法在指定的时间间隔内初始化。
3)。您不能访问不存在的对象的属性。在map对象实例化后在init方法中添加侦听器可以解决这个问题。
var map;
function initialize() {
var center_latlng = new google.maps.LatLng(42.354183, -71.065063);
var options = {
zoom: 15,
minZoom: 11,
maxZoom: 19,
center: center_latlng,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
//instantiate map object
map = new google.maps.Map(document.getElementById("map_canvas"), options);
//attach listener to the map object.
google.maps.event.addListener(map, 'idle', function() {
console.log('hello');
});
}
下面是上面代码的一个工作示例:http://jsfiddle.net/R7d6L/
相关文章:
- RichMarker for Google Maps v3 - 点击跌倒标记
- 获取从iFrame到父页面的Google Maps v3角坐标
- Google Maps v3 API-在地图上绘制圆圈
- Google Maps V3 fitBounds 在第二次调用具有相同边界时会缩放到更近的视图
- Jquery UI + Google Maps v3 API 仅在刷新后加载
- Google Maps v3 DistanceMatrix/Directions ignore avoidTolls:f
- 将参数向下传递到嵌套函数 Google Maps v3
- 在 Google Maps V3 中获取 GeoJSON 数据层的属性
- Mouseout for infoWindow.close 在 javascript 中的 Google Maps v3
- Google Maps v3 infoWindow addDomListener for HTML Button
- 在 Rails 应用程序中显示 Google Maps v3 上的标记 - 使用 Javascript 和 JSON
- 在 InfoWindow 中添加 alt 标签,用于 Google Maps V3 JavaScript
- 使用React为Google Maps v3自定义套印格式
- 在标记Google Maps v3上添加onclick事件
- jQuery Mobile interferes with Google Maps v3 API
- 所有Google Maps v3标记的通用事件处理程序
- 移动Safari中的Google Maps v3 InfoBox事件传播
- 在Maps V3中拖动Pegman会触发不需要的点击事件
- 瓦片上的事件开始在Google Maps v3中加载
- 如何将事件处理程序绑定到Google Maps V3 API默认PanControl's单击事件