google maps api v3: handle user click on map type control?

十年热恋 提交于 2021-01-28 01:11:21

问题


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?


回答1:


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.




回答2:


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


来源:https://stackoverflow.com/questions/10384597/google-maps-api-v3-handle-user-click-on-map-type-control

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!