将纬度/经度转换为像素并返回 [英] Convert lat/lon to pixels and back

查看:119
本文介绍了将纬度/经度转换为像素并返回的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

我的pixelsToBB函数需要使用缩放级别来做什么?或者,而是我的convertToXY,convertToLng和convertToLat需要做什么?我是否正确地思考这个问题,还是有更好的方法来做到这一点?
我甚至不知道要搜索什么,所以如果在对不起之前询问它。

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.

推荐答案

使用Google Maps API v3:

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

这篇关于将纬度/经度转换为像素并返回的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆