当前位置: 动力学知识库 > 问答 > 编程问答 >

javascript - Purpose of maxZoom property in Google Maps MapType interface?

问题描述:

The Google maps API Guide detailing the MapTypes interface states the following with regard to "required" properties:

maxZoom (required) specifies the maximum zoom level at which to

display tiles of this map type.

However, in the example given by Google at:

https://developers.google.com/maps/documentation/javascript/examples/maptype-base

The maxZoom property is NOT even included.

If the code is modified to include the maxZoom property (as shown below), the property has no effect -- should it? Some clarification would be nice ...

<!DOCTYPE html>

<html>

<head>

<title>Overlay map types</title>

<style>

html, body, #map-canvas {

height: 100%;

margin: 0px;

padding: 0px;

}

</style>

<script src="https://maps.googleapis.com/maps/api/js?v=3.exp"></script>

<script>

/** @constructor */

function CoordMapType(tileSize) {

this.tileSize = tileSize;

this.maxZoom = 12; // <--- THIS HAS NO EFFECT ??

}

CoordMapType.prototype.getTile = function (coord, zoom, ownerDocument) {

var div = ownerDocument.createElement('div');

div.innerHTML = coord.toString() + '<br>zoom: ' + zoom.toString();

div.style.width = this.tileSize.width + 'px';

div.style.height = this.tileSize.height + 'px';

div.style.fontSize = '10';

div.style.borderStyle = 'solid';

div.style.borderWidth = '1px';

div.style.borderColor = '#AAAAAA';

return div;

};

var map;

var chicago = new google.maps.LatLng(41.850033, -87.6500523);

function initialize() {

var mapOptions = {

zoom: 10,

center: chicago

};

map = new google.maps.Map(document.getElementById('map-canvas'),

mapOptions);

// Insert this overlay map type as the first overlay map type at

// position 0. Note that all overlay map types appear on top of

// their parent base map.

map.overlayMapTypes.insertAt( 0, new CoordMapType(new google.maps.Size(256, 256)));

}

google.maps.event.addDomListener(window, 'load', initialize);

</script>

</head>

<body>

<div id="map-canvas"></div>

</body>

</html>

网友答案:

If you changed mapType object's property ( maxZoom, minZoom, etc... ), the changed effect will take a place ONLY AFTER the map's type will be changed to that mapType.

For example , if current mapType is TERRAIN, and you changed TERRAIN's maxZoom, maxZoom will work only after changing mapType to another type (e.g. ROADMAP,HYBRID,etc..) and back to TERRAIN.

Instead of setting mapType's maxZoom option,use core map's maxZoom option. Add listener for "maptypeid_changed" event, and when event occurs, change maxZoom option of map:

google.maps.event.addListener(map, 'maptypeid_changed', function(event){
if( map.getMapTypeId() === google.maps.MapTypeId.TERRAIN ){
    map.setOptions({maxZoom:/*For example*/5});
}else 
if( map.getMapTypeId() === google.maps.MapTypeId.ROADMAP ){
    map.setOptions({maxZoom:/*For example*/8});
}//Etc...
else{
    map.setOptions({maxZoom:/*For example*/21});
}
});
网友答案:

Found this buried in Google maps documentation in the section entitled "MapType object specification"...

maxZoom Type: number

The maximum zoom level for the map when displaying this MapType. Required for base MapTypes, ignored for overlay MapTypes.

So, a maxZoom property is NOT required for overlay MapTypes; which corresponds to the behavior in my original code example.

分享给朋友:
您可能感兴趣的文章:
随机阅读: