简体   繁体   中英

JS Function undefined when it is clearly a prototype of the object I am using

I am using GoogleMaps JS API V3 with ClusterMarker. So far so good, I have a working map with customised clusters.

I decided I needed a method to open the infoWindow with some information of the clicked cluster, including some anchored links.

I did some searching on SO and Google and came up with something along these lines from this StackOverflow answer .

var infoWindow = new google.maps.InfoWindow();

google.maps.event.addListener(markerCluster, 'clusterclick', function (clickedCluster) {


    console.log(clickedCluster);

    var center = clickedCluster.getCenter();

    var latLng = new google.maps.LatLng(center);

    // infowindow.setContent("Info Window");
    // infowindow.setPosition(latLng);
    // infowindow.open(map);

});

In the console.log(clickedCluster) I'm given a Cluster object that has 6 methods, and a load of properties. One of these functions is getCenter() that returns the 'Center of the cluster icon that has been clicked'.

When I uncomment this line: var center = clickedCluster.getCenter(); I get an error saying that getCenter() is not a function.

Can anyone help me shed some light on what I'm doing wrong here please?

Thanks in advance

So the solution to the problem turned out to be that clickedCluster was returned as an array. The below works great.

var infoWindow = new google.maps.InfoWindow();

google.maps.event.addListener(markerCluster, 'clusterclick', function (clickedCluster) {

    var center = clickedCluster[0].getCenter();
    var latLng = new google.maps.LatLng(center.lat(),center.lng());

    infoWindow.setContent(getDailyDeals(center.lat(),center.lng()));
    infoWindow.setPosition(latLng);
    infoWindow.open(map);
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM