title: serverside clustering google maps markers
body: <p>I would like to know what is the fastest algorithm for clustering markers in PHP?</p>
  
  <p>Only thing I need from the cluster function is an output with a cluster obj, that has properties: lat,lng and size.</p>
  
  <p>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?</p>
  
  <p>I'm looking for the code-behind that would produce this kind of result? (Or perhaps perform better). <a href="http://maps.forum.nu/server_side_clusterer/" rel="nofollow">http://maps.forum.nu/server_side_clusterer/</a></p>
  
  <p><strong>So far I've tried with:</strong></p>
  
  <pre><code>function ClusterMarkers($markers,$ZOOM)
    {
    $this-&gt;load-&gt;library('firephp');    
    $singleMarkers = array();
    $clusterMarkers = array();
  
    // Minimum distance between markers to be included in a cluster, at diff. zoom levels
    $DISTANCE = (10000000 &gt;&gt; $ZOOM);
    // Loop until all markers have been compared.
    while (count($markers)) {
        $marker  = array_pop($markers);
        $cluster = array();
  
        // Compare against all markers which are left.
        foreach ($markers as $key =&gt; $target) {
            $pixels = abs($marker['lat']-$target['lat']) + abs($marker['lng']-$target['lng']);
            $this-&gt;firephp-&gt;log('pix :'.$pixels);
            if ($pixels &lt; $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) &gt; 0) {
            $cluster[] = $marker;
            $clusterMarkers[] = $cluster;
        } else {
            $singleMarkers[] = $marker;
        }
    } 
    return array('singlemarkers' =&gt; $singleMarkers, 'clustermarkers' =&gt; $clusterMarkers); 
    }
  </code></pre>
  
  <p>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.</p>