问题
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