2012-10-08 97 views
4

我目前使用MarkerClustererPlus來聚集我的標記。 (歡迎任何其他建議)我想知道是否有一種方法可以通過大陸或國家進行聚類,而不是靠近。謝謝有沒有一種方法來按國家分類谷歌地圖標記?

+0

在哪裏你的標記是從哪裏來的?如果它們來自數據庫,那麼根據服務器端所需的任何參數對其進行羣集應該非常簡單(group by)。 – Marcelo

+0

http://stackoverflow.com/questions/12592079/google-maps-api-v3-utilize-markercluster-but-have-the-clusters-themselves-be的可能重複 – geocodezip

回答

3

有,它叫Marker Manager,實際上和你當前使用的庫在同一個服務器上。我認爲這裏沒有必要提供任何示例,因爲它們都是right here

3

嘗試基於區域集羣這個代碼..

var keys = []; 
var markerCluster = []; 
var markers = new Object(); 
var map; 

function initialize(){ 
var mapProp = { 
    center:center, 
    zoom:5, 
    mapTypeId:google.maps.MapTypeId.ROADMAP 
    }; 
    map=new google.maps.Map(document.getElementById("googleMap") 
    ,mapProp); 

//styling cluster image.. 
    var clusterStyles = [ 
    { 
    opt_textColor: 'black', 
    url: 'images/cluster.png', 
    height: 56, 
    width: 55 
    }, 
    { 
    opt_textColor: 'black', 
    url: 'images/cluster2.png', 
    height: 53, 
    width: 52 
    } 
    ]; 

    //cluster marker options.. 
    var mcOptions = { 
      // gridSize: 16, 
      styles: clusterStyles, 
      maxZoom: 15 
       }; 
function initialize(){ 
var mapProp = { 
    center:center, 
    zoom:5, 
    mapTypeId:google.maps.MapTypeId.ROADMAP 
}; 
    map=new google.maps.Map(document.getElementById("googleMap") 
    ,mapProp); 

//styling cluster image.. 
    var clusterStyles = [ 
    { 
    opt_textColor: 'black', 
    url: 'images/cluster.png', 
    height: 56, 
    width: 55 
    }, 
    { 
    opt_textColor: 'black', 
    url: 'images/cluster2.png', 
    height: 53, 
    width: 52 
    } 
    ]; 

    //cluster marker options.. 
    var mcOptions = { 
      // gridSize: 16, 
      styles: clusterStyles, 
      maxZoom: 15 
      }; 
    //fetching lat long from data base 
    <?php echo "addmarker(lat,lng); ?>" 
    for(var k in markers) keys.push(k); 
      for(var i = 0; i < keys.length; i++) 
      { 
       markerCluster[i] = new MarkerClusterer(map,  markers[keys[i]],mcOptions); 
      } 
} 
function addmarker(lat, lng) 
{ 
    var provnce; 
    var mycenter = new google.maps.LatLng(lat,lng); 
    var marker = new google.maps.Marker({ 
     position:mycenter, 
     title:infoName, 
     id: count++ 
     // animation:google.maps.Animation.BOUNCE 
     }); 
    //clustering markers based on region.. 
    $.ajax({ url:'https://maps.googleapis.com/maps/api/geocode/json? latlng='+lat+','+lng+'&sensor=true', 
     async: false, 
     success: function(data){ 
         // console.log(data.results[0]); 
         // return; 
      for (var i=0; i<data.results[0].address_components.length; i++) 
      { 
       if (data.results[0].address_components[i].types[0] == "administrative_area_level_1") { 
         //this is the object for province 
         provnce = data.results[0].address_components[i]['long_name']; 
        } 
      } 
      provnce = provnce.split(" ",1); 
      if(markers.hasOwnProperty(provnce)) 
      { 
       markers[provnce].push(marker); 
      } 
      else 
      { 
       markers[provnce] = new Array(); 
       markers[provnce].push(marker); 
      } 
      // console.log(markers); 
      } 
     }); 
}