将lat/lon转换为像素并返回

AmI*_*ord 15 google-maps zoom pixel coordinates

我在我的应用程序中使用谷歌地图,并且我有一个带有lat/lon值的数据库的网络服务器.我想在地图上标记它们,但如果它们在彼此的某个像素距离内,我也想将它们聚集在一起.

我想如果我从数据库中检索所有的点,我应该可以做这样的事情(伪代码):

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);
}
Run Code Online (Sandbox Code Playgroud)

我的pixelsToBB函数需要用zoomlevel做什么?或者更确切地说,我的convertToXY,convertToLng和convertToLat需要做什么?我是以正确的方式思考这个问题,还是有更好的方法来做到这一点?我甚至不确定要搜索什么,所以如果在对不起之前就被问到了.

小智 13

使用Google Maps 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)];
Run Code Online (Sandbox Code Playgroud)


Jhe*_*ico 8

有一个JavaScript示例可以在此页面上执行此操作,作为Google Maps API文档的一部分.请记住,您需要查看页面源以查看它.它不是一个实际的文档页面,而是一个例子.