Javascript 修改谷歌地图类型的最大缩放级别

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10517848/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 01:40:18  来源:igfitidea点击:

Modify the max zoom level of a Google Map Type

javascriptgoogle-mapsgoogle-maps-api-3

提问by jbrtrnd


I need to set the maxZoomlevel of the google.maps.MapTypeId.HYBRIDto 21. Actually, he's set to 14 (inspected with the firebug console).


我需要将 的maxZoom级别设置google.maps.MapTypeId.HYBRID为 21。实际上,他设置为 14(用萤火虫控制台检查)。

Set the property of the google.mapsobject 'maxZoom' doesn't work in this case, and I already try to modify the google.maps.mapTypesobject, but without success.

google.maps在这种情况下,设置对象“maxZoom”的属性不起作用,我已经尝试修改google.maps.mapTypes对象,但没有成功。

var options = {
    center: new google.maps.LatLng(lat_centre,lng_centre),   
    zoom: 14, 
    maxZoom: 21,
    mapTypeId: google.maps.MapTypeId.TERRAIN,   
    panControl: true,
    zoomControl: true,
    mapTypeControl: false,
    scaleControl: false,
    streetViewControl: false,
    overviewMapControl: false,
    disableDoubleClickZoom: true,
    styles: [ 
        {featureType: "poi", stylers: [{visibility: "off"}]} 
        ]                   
    };

    map = new google.maps.Map(document.getElementById("map_container"),options);

AND

google.maps.event.addListener(map, 'tilesloaded', function(event){

    var mapTypeRegistry = map.mapTypes;

    var currentMapTypeId = map.getMapTypeId();

    var mapType = mapTypeRegistry.get(currentMapTypeId);

    mapType.maxZoom = 21;
  });

So, is there a way to increase the zoom level of the mapType HYBRID ?

那么,有没有办法增加 mapType HYBRID 的缩放级别?

Thanks.

谢谢。

回答by Engineer

If you changed mapTypeobject's property ( maxZoom, minZoom, etc... ), the changed effect will take a place ONLY AFTERthe map's type will be changed to that mapType. For example , if current mapTypeis TERRAIN, and you changed TERRAIN's maxZoom, maxZoomwill work only after changing mapTypeto another type (e.g. ROADMAP,HYBRID,etc..) and back to TERRAIN.

如果您更改了mapType对象的属性(maxZoomminZoom等... ),则更改后的效果在地图类型更改为该 之后才会发生mapType。例如,如果当前mapTypeTERRAIN的,你改变TERRAINmaxZoommaxZoom只会改变工作后mapType,以另一种类型(例如ROADMAPHYBRID等..),并返回TERRAIN

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

而不是设置mapTypemaxZoom选项,使用核心地图的maxZoom选项。添加"maptypeid_changed"事件监听器,当事件发生时,更改maxZoom选项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});
    }
});