2012-04-30 42 views

回答

1

您无法侦听默认UI控件集上的事件。但是,如果你严格地专注于对mapTypeControlmap.setMapTypeId()点击之间的区别,你可以使用的事实,你控制可能调用setMapTypeId(),并添加一些状态管理代码的代码:

// First, add a new state var: 
var typeIdChangedInCode = false; 

// Then, anywhere in your code where you call setMapTypeId(), add this: 
typeIdChangedInCode = true; 
map.setMapTypeId(newTypeId); 

// Finally, include state checking code in the map event listener: 
google.maps.event.addListener(map, "maptypeid_changed", function(evnt) { 
    if (typeIdChangedInCode) { 
     //handle the scenario in the non-click way, but REMEMBER TO: 
     typeIdChangedInCode = false; 
    } 
    else { 
     //handle the scenario in the map click way 
    } 
}); 

这应该设置你以您需要的方式处理两个不同的事件。

0
google.maps.event.addListener(map, 'maptypeid_changed', function(e){ 
      alert(map.getMapTypeId()); 
    }); 
+0

如何判断这是编程式maptype更改的结果还是用户单击控件?这似乎在两种情况下都会触发(这就是我问这个问题的原因)。 –

+0

对不起,我误解了。 –