Rotideporc
Rotideporc

Reputation: 5

MarkerCluster Google map not grouping my marker

First i get the position of stations with JSON // Work

JSON structure :

[{"number":31705,"name":"31705 - CHAMPEAUX (BAGNOLET)","address":"RUE DES CHAMPEAUX (PRES DE LA GARE ROUTIERE) - 93170 BAGNOLET","position":{"lat":48.8645278209514,"lng":2.416170724425901},"banking":true,"bonus":true,"status":"OPEN","contrat _name":"Paris","bike_stands":50,"available_bike_stands":45,"available_bikes":5,"last_update":1416677110000}]

Second I want grouping marker with MarkerCluster but the MarkerCluster are in all my marker
Why it's not grouping ?

function initialize() {

  var mapOptions = {
    center: new google.maps.LatLng(48.859875, 2.342253),
    zoom: 13,
    mapTypeId: google.maps.MapTypeId.ROADMAP,
    disableDefaultUI: true
  };

  var infoWindow = new google.maps.InfoWindow();
  var mcOptions = {gridSize: 50, maxZoom: 15, minimumClusterSize:10}

  map = new google.maps.Map(document.getElementById("map_canvas"), mapOptions);
  $.getJSON("https://api.jcdecaux.com/vls/v1/stations?contract=Paris&apiKey=d98a9b59f1d1999cf1ab909051cc63bd46c66a46", function (data) {
    $.each(data, function (key, data) {

      var markers = [];

      for (var i = 0, length = data.address.length; i < length; i++) {
        var myLatlng = new google.maps.LatLng(data.position.lat, data.position.lng);
        var marker = new google.maps.Marker({
          position: myLatlng
        });

        markers.push(marker);
      }

      var markerCluster = new MarkerClusterer(map, markers, mcOptions);

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

Upvotes: 0

Views: 468

Answers (1)

geocodezip
geocodezip

Reputation: 161384

You are creating a new MarkerClusterer for each marker (if you indent your code it is easier to see)

  • Move the creation of the MarkerCluster out of the $.each.

also:

  • move the google.maps.event.addDomListener(window,'load',initialize); out of the initialize function
  • remove the inner loop on data.address.length (unless it is required by your data, didn't look at all your data)
function initialize() {

  var mapOptions = {
    center: new google.maps.LatLng(48.859875, 2.342253),
    zoom: 13,
    mapTypeId: google.maps.MapTypeId.ROADMAP,
    disableDefaultUI: true
  };

  var infoWindow = new google.maps.InfoWindow();
  var mcOptions = {
    gridSize: 50,
    maxZoom: 15,
    minimumClusterSize: 10
  };

  map = new google.maps.Map(document.getElementById("map_canvas"), mapOptions);
  $.getJSON("https://api.jcdecaux.com/vls/v1/stations?contract=Paris&apiKey=d98a9b59f1d1999cf1ab909051cc63bd46c66a46", function (data) {
    $.each(data, function (key, data) {
      var myLatlng = new google.maps.LatLng(data.position.lat, data.position.lng);
      var marker = new google.maps.Marker({
        position: myLatlng
      });
      markers.push(marker);
    });
    var markerCluster = new MarkerClusterer(map, markers, mcOptions);
  });
}
google.maps.event.addDomListener(window, 'load', initialize);

working fiddle (with a modified subset of your data)

Upvotes: 1

Related Questions