服务器端群集谷歌地图标记(serverside clustering google maps markers)

编程入门 行业动态 更新时间:2024-10-26 05:25:27
服务器端群集谷歌地图标记(serverside clustering google maps markers)

我想知道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. }

更多推荐

本文发布于:2023-08-03 09:29:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1385238.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:服务器端   标记   地图   serverside   markers

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!