我在我的应用程序中使用谷歌地图,并且我有一个网络服务器,其中包含一个填充了纬度/经度值的数据库。我想在 map 上标记它们,但如果它们彼此相距一定像素距离,我也想将它们聚集在一起。
我想如果我从数据库中检索我所有的点,我应该能够做这样的事情(伪代码):
clusters[];
while(count(points)) {
cluster[];
point = points.pop();
boundingbox = pixelsToBB(point, pixeldistance, zoomlevel);
query = "select * from database where lat > boundingbox.minlat
and lat < boundingbox.maxlat and lng > boundingbox.minlng
and lng < boundingbox.maxlng";
for (result in executedquery) {
cluster[] += result;
points.remove(result);
}
clusters[] += cluster;
}
pixelsToBB(point, distance, zoomlevel) {
center = convertXY(point, zoomlevel);
maxlng = convertToLng(center.X, distance, zoomlevel);
minlng = convertToLng(center.X, -distance, zoomlevel);
minlat = convertToLat(center.Y, -distance, zoomlevel);
maxlat = convertToLat(center.Y, distance, zoomlevel);
return boundingbox(maxlng, maxlat, minlng, minlat);
}
我的 pixelToBB 函数需要对缩放级别做什么?或者更确切地说,我的 convertToXY、convertToLng 和 convertToLat 需要做什么?我是否以正确的方式思考这个问题,还是有更好的方法来做到这一点?
我什至不确定要搜索什么,所以如果在抱歉之前有人问过它。
最佳答案
使用谷歌地图 API v3:
var latLng = // your position object here
var projection = map.getProjection();
var bounds = map.getBounds();
var topRight = projection.fromLatLngToPoint(bounds.getNorthEast());
var bottomLeft = projection.fromLatLngToPoint(bounds.getSouthWest());
var scale = Math.pow(2, map.getZoom());
var worldPoint = projection.fromLatLngToPoint(latLng);
return [Math.floor((worldPoint.x - bottomLeft.x) * scale), Math.floor((worldPoint.y - topRight.y) * scale)];
关于google-maps - 将纬度/经度转换为像素并返回,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3410600/