当用户点击 mapTypeControl
时,我需要以不同于通过 map.setMapTypeId()
设置时的方式处理这种情况。如何监听 map 类型控件上的点击?
最佳答案
您无法监听默认 UI 控件集上的事件。但是,如果您严格关注区分 mapTypeControl
和 map.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
}
});
这应该让您能够以您需要的方式处理两种不同的情况。
关于google-maps-api-3 - 谷歌地图 API v3 : handle user click on map type control?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10384597/