开发者

serverside clustering google maps markers

开发者 https://www.devze.com 2023-02-07 17:33 出处:网络
I would like to know what is the fastest algorithm for clustering markers in PHP? Only thing I need from the cluster function is an output with a cluster obj, that has properties: lat,lng and size.

I would like to know what is the fastest algorithm for clustering markers in PHP?

Only thing I need from the cluster function is an output with a cluster obj, that has properties: lat,lng and size.

and then the markers that weren't clustered of course, but I can't seem to find php code for this, and there must be some?

I'm looking for the code-behind that would produce this kind of result? (Or perhaps perform better). http://maps.forum.nu/server_side_clusterer/

So far I've tried with:

function ClusterMarkers($markers,$ZOOM)
  {
  $this->load->library('firephp');    
  $singleMarkers = array();
  $clusterMarkers = array();

  // Minimum distance between markers to be included in a cluster, at diff. zoom levels
  $DISTANCE = (10000000 >> $ZOOM);
  // Loop until all markers have been compared.
  while (count($mar开发者_JAVA百科kers)) {
      $marker  = array_pop($markers);
      $cluster = array();

      // Compare against all markers which are left.
      foreach ($markers as $key => $target) {
          $pixels = abs($marker['lat']-$target['lat']) + abs($marker['lng']-$target['lng']);
          $this->firephp->log('pix :'.$pixels);
          if ($pixels < $DISTANCE) {
              unset($markers[$key]);
              $cluster[] = $target;
          }
      }

      // If a marker has been added to cluster, add also the one we were comparing to.
      if (count($cluster) > 0) {
          $cluster[] = $marker;
          $clusterMarkers[] = $cluster;
      } else {
          $singleMarkers[] = $marker;
      }
  } 
  return array('singlemarkers' => $singleMarkers, 'clustermarkers' => $clusterMarkers); 
  }

My data is then jsonized, but the clustermarkers array contains all the markerdata, and I'm wondering how I would effectively simply set a lat,lng and a size without having to recalculate to resourcedemanding everytime a new marker is added.


Basically the $clusterMarkers array is a group of clustered markers, so you could simply take the centroid of the containing markers instead of returning all markers. Before returning the result, do:

foreach($clusterMarkers as $key => $cluster) {
    $centroid = array('lat' => 0, 'lng' => 0, 'count' => 0);
    foreach($cluster as $marker) {    
        $centroid['lat'] += $marker['lat']; // Sum up the Lats
        $centroid['lng'] += $marker['lng']; // Sum up the Lngs
        $centroid['count']++;
    }
    $centroid['lat'] /= $centroid['count']; // Average Lat
    $centroid['lng'] /= $centroid['count']; // Average Lng
    $clusterMarkers[$key] = $centroid; // Overwrite the cluster with the single point.
}


I use the MarkerClusterer JavaScript class from Xiaoxi Wu.

You may want to take a look and adapt it to PHP:

http://googlegeodevelopers.blogspot.com/2009/04/markerclusterer-solution-to-too-many.html

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号