简体   繁体   English

Google Maps API JS - MarkerClusterer - 无法读取未定义的属性“maxZoom”

[英]Google Maps API JS - MarkerClusterer - Cannot read property 'maxZoom' of undefined

I have google map object with options:我有带有选项的谷歌地图对象:

$(document).ready(function () {
    var mapOptions = {
        zoom: 4,
        minZoom: 3,
        maxZoom: 12,
        center: new google.maps.LatLng(39.484973, -0.364126),
        mapTypeControl: false,
        streetViewControl: false
    };

    var mapElement = document.getElementById('#map');

    map = new google.maps.Map(mapElement, mapOptions);

    markers = [...];
    markerCluster = new MarkerClusterer(map, markers, {
        averageCenter: true,
        styles: [{
            url: '/cluster.png',
            width: 64,
            height: 64,
            textColor: 'white',
            backgroundPosition: 'center'
        }]
    });

    ...
}

Now after initializing a map I want to change a zoom level by running:现在初始化地图后,我想通过运行更改缩放级别:

var location = ...

map.setCenter(location); // works fine
map.setZoom(7);`, 

but I get an error in console:但我在控制台中收到错误消息:

Uncaught TypeError: Cannot read property 'maxZoom' of undefined
at Ag.<anonymous> (markerclusterer.js:163)
at Object._.z.trigger (js?key=AIzaSyBpuEnTxOHtdmlMllM_Qd2SL_Q2t45o3_0:99)
at Hb (js?key=AIzaSyBpuEnTxOHtdmlMllM_Qd2SL_Q2t45o3_0:38)
at Ag._.k.set (js?key=AIzaSyBpuEnTxOHtdmlMllM_Qd2SL_Q2t45o3_0:101)
at Ag.setZoom (js?key=AIzaSyBpuEnTxOHtdmlMllM_Qd2SL_Q2t45o3_0:56)
at OfferMap.setBounds (offers_offer-map_1.js:1)
at HTMLDocument.<anonymous> (offers_trainee-offers_3.js:1)
at i (jquery.min.js:2)
at Object.fireWith [as resolveWith] (jquery.min.js:2)
at Function.ready (jquery.min.js:2)

Anyone has any ideas what is going on?任何人都知道发生了什么事?

UPDATE更新

Since problem is because of MarkerClusterer as @geocodezip mentioned in comment below, here is the script I load in:由于问题是因为 MarkerClusterer,正如下面评论中提到的 @geocodezip,这里是我加载的脚本:

<script type="text/javascript" src="https://developers.google.com/maps/documentation/javascript/examples/markerclusterer/markerclusterer.js"></script>

Okay, I resolved it, Apparently, as @geocodezip mentioned under the question it was masterclusterer problem and my imported library was out of date.好的,我解决了它,显然,正如@geocodezip 在问题下提到的那样,这是masterclusterer问题,我导入的库已过时。 Had to download newer .必须下载更新的。 Works perfectly.完美运行。

Also do not forget to set maxZoom in MarkerClusterer options:也不要忘记在 MarkerClusterer 选项中设置 maxZoom:

this.markerCluster = new MarkerClusterer(this.map, this.markers, {
    maxZoom: 12,
    averageCenter: true,
    styles: [{
        url: this.clusterIcon,
        width: 64,
        height: 64,
        textColor: 'white',
        backgroundPosition: 'center'
    }]
});

During clear problems, updated title and question for the future visitors.在明确问题期间,为未来的访问者更新标题和问题。

For me issue was only solved after defining mapTypes collection for google map object: this.googleMapOptions = { zoom: 5, center: {lat: -28.643387, lng: 153.612224}, disableDefaultUI: true, styles: MapStyle.json, maxZoom: 18, mapTypeId: google.maps.MapTypeId.ROADMAP, mapTypeControl: false, zoomControl: false }对我来说,问题仅在为谷歌地图对象定义mapTypes集合后得到解决: this.googleMapOptions = { zoom: 5, center: {lat: -28.643387, lng: 153.612224}, disableDefaultUI: true, styles: MapStyle.json, maxZoom: 18, mapTypeId: google.maps.MapTypeId.ROADMAP, mapTypeControl: false, zoomControl: false }

Initialize Google map:初始化谷歌地图:

        this.gmap = new google.maps.Map(jqueryElement[0],
            this.googleMapOptions);

        this.googleMapType = new google.maps.MapTypeRegistry();

        this.roadMapType = new google.maps.StyledMapType(MapStyle.json, { alt: "roadmap", maxZoom: 18, name : "roadmap" })

        this.googleMapType.set("roadmap", this.roadMapType);

        this.gmap.mapTypes = this.googleMapType;
        this.gmap.setMapTypeId("roadmap");

MapStyle json object can be generated at https://mapstyle.withgoogle.com/可以在https://mapstyle.withgoogle.com/生成 MapStyle json 对象

You should wrap this function in window.onload to make sure that the dom is loaded.您应该将此函数包装在window.onload中以确保加载了 dom。

window.onload = function () {

    var mapOptions = {
        zoom: 4,
        minZoom: 3,
        maxZoom: 12,
        center: new google.maps.LatLng(39.484973, -0.364126),
        mapTypeControl: false,
        streetViewControl: false
    };

    var mapElement = document.getElementById('#map');

    map = new google.maps.Map(mapElement, mapOptions);
}

I had the same issue in Angular 8 project.我在 Angular 8 项目中遇到了同样的问题。 Adding the following to my index.html file fix the issue for me:将以下内容添加到我的 index.html 文件中可以解决我的问题:

<script type="text/javascript" src="https://developers.google.com/maps/documentation/javascript/examples/markerclusterer/markerclusterer.js"></script>

Try to set map globally and the call update function for updating zoom with map.setZoom(7);尝试全局设置map并调用update函数更新zoom map.setZoom(7);

Fiddle demo小提琴演示

JS JS

var map;
function initMap() {
var mapOptions = {
    zoom: 4,
    minZoom: 3,
    maxZoom: 12,
    center: new google.maps.LatLng(39.484973, -0.364126),
    mapTypeControl: false,
    streetViewControl: false
};

var mapElement = document.getElementById('map');

map = new google.maps.Map(mapElement, mapOptions);
}
function updateMap(){
map.setZoom(7);
}

HTML HTML

<button onclick="updateMap()">
set zoom
</button>
<div id="map"></div>

Updated Fiddle:更新小提琴:

Fiddle小提琴

Similarly to @Taras, I was able to solve this problem by tweaking the markerclusterer.js file slightly.与@Taras 类似,我能够通过稍微调整 markerclusterer.js 文件来解决这个问题。 Replace the 'zoom_changed' listener with the following...用以下内容替换“zoom_changed”侦听器......

// Add the map event listeners
  var that = this;
  google.maps.event.addListener(this.map_, 'zoom_changed', function() {
    // Determines map type and prevent illegal zoom levels
        var zoom = that.map_.getZoom();
        var minZoom = that.map_.minZoom || 0;
        var mapType = that.map_.mapTypes[that.map_.getMapTypeId()];
        var maxZoom = null;
        if (mapType !== null && mapType !== undefined) {
            maxZoom = Math.min(that.map_.maxZoom || 100, mapType.maxZoom);
        } else {
            maxZoom = that.map_.maxZoom || 100;
        }

        zoom = Math.min(Math.max(zoom,minZoom),maxZoom);

        if (that.prevZoom_ != zoom) {
            that.prevZoom_ = zoom;
            that.resetViewport();
        }
  });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 谷歌地图无法读取未定义的属性“地图” - google maps Cannot read property 'maps' of undefined Google Maps和Angular js:TypeError:无法读取未定义的属性“触发” - Google Maps and Angular js: TypeError: Cannot read property 'trigger' of undefined Google maps API:无法读取未定义的属性“__e3_” - Google maps API: Cannot read property '__e3_' of undefined Google Maps / Places API JavaScript - 无法读取未定义的属性“setContent” - Google Maps/Places API JavaScript - Cannot read property 'setContent' of undefined Google地图:无法读取未定义的属性“几何” - Google Maps: Cannot read property 'geometry' of undefined “无法读取未定义的属性‘zindex’”谷歌地图 - “Cannot read property 'zindex' of undefined” Google maps Google Maps MapType界面中的maxZoom属性的用途? - Purpose of maxZoom property in Google Maps MapType interface? Google Maps API v3 Heatmaps 错误:“无法读取未定义的属性‘HeatmapLayer’” - Google Maps API v3 Heatmaps Error: "Cannot read property 'HeatmapLayer' of undefined" 谷歌地图 API 返回类型错误:无法读取未定义的属性“重量”[暂停] - Google Maps API returns TypeError: Cannot read property 'weight' of undefined [on hold] 无法使用带有 load-google-maps-api-2 的 webpack 读取未定义的属性“自动完成” - Cannot read property 'Autocomplete' of undefined using webpack with load-google-maps-api-2
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM