user1809790
user1809790

Reputation: 1369

Google Maps - Clustering Markers

I have a Google Map (using API V3) where I need to set marker clustering to it. Basically the marker locations are loaded through JSON and it returns the Issue (either 1 or 2), the latitude and the longitude.

I have tried to set the clustering however nothing is being displayed on the map even though the JSON call actually returns points. Here is something I came up with:

var map;

function myMap(){   
  var latlng = new google.maps.LatLng(35.94, 14.37);

var mapOptions = {
zoom: 10,
center: latlng,
panControl: false,
mapTypeControl: false,
mapTypeId: google.maps.MapTypeId.ROADMAP,
scaleControl: false,
streetViewControl: false,
overviewMapControl: false,
zoomControl: true};

map = new google.maps.Map(document.getElementById("map"), mapOptions);  
loadMarkers();
}

function loadMarkers() {    

var mcOptions = {gridSize: 50, maxZoom: 15};
var issueSevereMarkers = [];
var issueSlightMarkers = [];

 markerIssueHigh = new google.maps.MarkerImage("img/icn_issue_high.png",
new google.maps.Size(10.0, 10.0),
new google.maps.Point(0, 0),
new google.maps.Point(5.0, 5.0)
);

markerIssueMedium = new google.maps.MarkerImage("img/icn_issue_medium.png",
new google.maps.Size(10.0, 10.0),
new google.maps.Point(0, 0),
new google.maps.Point(5.0, 5.0)
);

var issuePoints = $.ajax({
  type: 'GET',
  url: 'http://www.mydomain.com/php/markers.php?&jsoncallback=?',
  dataType: 'JSONp',
  timeout: 5000,
  success: function(data) {         
        $.each(data, function(i,item){

        if (item.Issue == 1) {  
            // If severely congested                
              var latLng = new google.maps.LatLng(item.Latitude, item.Longitude);
              var issueSevereMarker = new google.maps.Marker({'position': latLng});
              issueSevereMarkers.push(issueSevereMarker);

        } else if (item.Issue == 2) {
            // If slightly congested
              var latLng = new google.maps.LatLng(item.Latitude, item.Longitude);
              var issueSlightMarker = new google.maps.Marker({'position': latLng});
              issueSlightMarkers.push(issueSlightMarker);
        }   

    });

    var markerClusterSevere = new MarkerClusterer(map, issueSevereMarkers);
    var issueSlightMarkerCluster = new MarkerClusterer(map, issueSlightMarkers);
  },
  error: function(data) {

  }
});     
}

Basically what I am doing is get the result from JSON, and if the issue is of type 1, I add a pin to the issueSevereMarkers array, otherwise I add it to the issueSlightMarkers array. This should load the arrays with the points of the markers. I am not sure if I am doing anything wrong there.

Also another issue that I have is that the clusterer basically returns a custom icon I suppose when the markers are grouped. When I zoom in and see the individual markers, how can I set the markers to use the markerIssueHigh and markerIssueMedium images?

I am using the Clustere for the V3 (http://google-maps-utility-library-v3.googlecode.com/svn/trunk/markerclusterer/docs/reference.html)

Upvotes: 2

Views: 2784

Answers (1)

geocodezip
geocodezip

Reputation: 161334

To actually use your custom icons you need to reference them in the google.maps.Marker constructor

    if (item.Issue == 1) {  
        // If severely congested                
          var latLng = new google.maps.LatLng(item.Latitude, item.Longitude);
          var issueSevereMarker = new google.maps.Marker({position: latLng, 
                                                          icon:markerIssueHigh});
          issueSevereMarkers.push(issueSevereMarker);

    } else if (item.Issue == 2) {
        // If slightly congested
          var latLng = new google.maps.LatLng(item.Latitude, item.Longitude);
          var issueSlightMarker = new google.maps.Marker({position: latLng,
                                                          icon:markerIssueMedium});

          issueSlightMarkers.push(issueSlightMarker);
    }   

Don't see anything obvious as to why nothing would appear, here is a link to a working map with the clusterer (gets its data from XML rather than JSON).

This similar question adds a custom marker to the cluster.

Upvotes: 1

Related Questions