我想知道PHP中聚类标记的最快算法是什么?
我只需要集群函数需要的是带有集群obj的输出,它具有属性:lat,lng和size。
然后当然没有聚集的标记,但我似乎无法找到这个的PHP代码,并且必须有一些?
我正在寻找会产生这种结果的代码隐藏? (或者表现得更好)。 http://maps.forum.nu/server_side_clusterer/
到目前为止,我尝试过:
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($markers)) { $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); }然后我的数据被jsonized,但是clustermarkers数组包含所有的markerdata,我想知道如何有效地简单地设置lat,lng和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($markers)) { $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.
最满意答案
基本上$clusterMarkers数组是一组聚类标记,因此您可以简单地获取包含标记的质心而不是返回所有标记。 在返回结果之前,请执行:
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. }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. }更多推荐
发布评论