将纬度/经度转换为像素并返回

发布于 2024-09-13 06:53:58 字数 1159 浏览 2 评论 0原文

我在我的应用程序中使用谷歌地图,并且我有一个网络服务器,其中的数据库填充了纬度/经度值。我想在地图上标记它们,但如果它们彼此之间在一定的像素距离内,我也想将它们聚集在一起。

我想如果我从数据库中检索所有点,我应该能够执行类似这样的操作(伪代码):

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);
}

我的 PixelsToBB 函数需要对缩放级别做什么?或者更确切地说,我的convertToXY、convertToLng 和convertToLat 需要做什么?我是否以正确的方式思考这个问题,或者是否有更好的方法来做到这一点? 我什至不知道要搜索什么,所以如果之前有人问过我,我很抱歉。

I'm using google maps in my application, and I have a webserver with a databse filled with lat/lon values. I want to mark them on the map, but I also want to cluster them together if they are within a certain pixel-distance of eachother.

I figure if I retrieve all my points from the database, I should be able to do something like this (pseudocode):

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);
}

What would my pixelsToBB function need to do with the zoomlevel? OR rather what would my convertToXY, convertToLng and convertToLat need to do? Am I thinking about this the right way, or are there any better ways to do it?
I'm not even sure what to search for, so if it's been asked before I'm sorry.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

回眸一笑 2024-09-20 06:53:59

使用 Google 地图 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)];

Using 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)];
迎风吟唱 2024-09-20 06:53:59

There is a JavaScript example to do this on this page as part of the documentation for the Google Maps API. Bear in mind you need to look at the page source to see it. It's not an actual documentation page but rather an example.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文