标记聚类时,“marker.getPosition不是函数”

时间:2018-02-05 13:43:33

标签: api google-maps google-maps-api-3 google-maps-markers markerclusterer

结果是给我这个错误:“TypeError:marker.getPosition不是函数”。

我对markerClustering进行了多次搜索,但找不到任何错误说明!

我的变量myMarkers是MarkerClusterer文档中提到的数组。

如果有人可以帮助我,那就太过分了。)

function initMap(){

    var maDataBase = [];
    var myMarkers = [];

    $.getJSON( "https://api.jcdecaux.com/vls/v1/stations?contract=MY-CONTRACT&apiKey=MY-API-KEY", function( data ) {
        $.each( data, function( key, val ) {
        maDataBase.push(val);
        });
    }).done(function(){ 

        var bounds = new google.maps.LatLngBounds();
        var myMap = $('.main')[0];
        var mapOptions = {
            mapTypeId : google.maps.MapTypeId.ROADMAP
        };

        var googleMap = new google.maps.Map(myMap, mapOptions);

        for (var i = 0; i < 50; i++){

            myMarkers.push(new google.maps.LatLng(maDataBase[i].position.lat, maDataBase[i].position.lng));

            for (var x = 0, marker; x < myMarkers.length;x++){

                bounds.extend(myMarkers[i]);

                marker = new google.maps.Marker({
                    map : googleMap,
                    position: myMarkers[i],
                });
            }

        };

        googleMap.fitBounds(bounds);
        var markerCluster = new MarkerClusterer(googleMap, myMarkers,{imagePath: '../img/m'});

    });

};
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>JCDecaux</title>
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" type="text/css" media="screen" href="css/main.css" />
</head>
<body>
    <div class="main" id="main">

    </div>




    <script async defer src="https://maps.googleapis.com/maps/api/js?key=MY-API-KEY&callback=initMap"></script>
    <script src="js/markerclusterer.js"></script>

    <script src="js/jquery.min.js"></script>

    <script src="js/main.js"></script>
</body>
</html>

1 个答案:

答案 0 :(得分:0)

myMarkers不是google.maps.Markers的数组。条目没有getPosition方法。将您在数组中创建的google.maps.Marker个对象保存到MarkerClusterer