谷歌地图API v3:处理用户单击地图类型控件? [英] google maps api v3: handle user click on map type control?
问题描述
当用户单击 mapTypeControl
不同于通过 map.setMapTypeId()
进行设置时,我需要处理这种情况.如何监听对地图类型控件的点击?
I need to handle the case when a user clicks on a mapTypeControl
differently than when it's set through map.setMapTypeId()
. How do I listen for clicks on map type controls?
推荐答案
您不能在默认的UI控件集中监听事件.但是,如果您严格专注于区分 mapTypeControl
和 map.setMapTypeId()
的点击,则可以使用以下事实:您控制可能调用的代码setMapTypeId()
并添加一些状态管理代码:
You can't listen for events on the default UI control set. But if you are strictly focused on differentiating between clicks on the mapTypeControl
and map.setMapTypeId()
, you could use the fact that you control the code that may call setMapTypeId()
and add some state management code:
// 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
}
});
这应该使您能够按照需要的方式处理两种不同的情况.
This should set you up to handle the two different occurrences in the way you need.
这篇关于谷歌地图API v3:处理用户单击地图类型控件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!