从世界坐标转换为图块位置 [英] Converting from world coordinates to tile location

查看:88
本文介绍了从世界坐标转换为图块位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我和一个朋友正在尝试为使用Google Maps的类构建一个android应用,而我们已经花了几天时间解决这个错误.

me and a friend are trying to build an android app for class that uses google maps and we have been spending days on this one error.

理想情况下,应用程序会接收用户位置的更新,进行存储并在任何存储的坐标上绘制.事实证明,实现它是一场噩梦,因此,如果该图块包含用户访问过的坐标,那么我们现在就尝试在整个图块上绘制.但是,我们认为从坐标到图块位置的转换有些问题,因为缩放比例越高,绘制的图块就越远离实际坐标向北偏西移动.

Ideally the app receives updates of the user's location, stores them, and paints over any stored coordinate. That turned out to be a nightmare to implement so right now we're just trying to paint over the entire tile if that tile contains a coordinate that the user has visited. However we think something's wrong with our conversion from coordinate to tile location because the higher the zoom the more our painted tile moves north and a little west from the actual coordinate.

private boolean isCoordInTileForZoom(int tileX, int tileY, int zoom)
{
    //coordinate to check if they are in the current tile
    float lon = WILF_TEST_COOR.x;
    float lat = WILF_TEST_COOR.y;

    //coordinates in terms of map length and map height
    float mapX = lon + 180;
    float mapY = (lat * -1) + 150;

    //number of tiles in a row or column (2^zoom)
    int tiles = (int) Math.pow(2,zoom);

    //the height and width of a single tile
    double tileWidth = 360.0/tiles;
    double tileHeight = 180.0/tiles;

    int mapCol = (int) (mapX/tileWidth);
    int mapRow = (int) (mapY/tileHeight);

    Log.d("Minimap.mapGridDetails","    \n" + "Zoom level: " + zoom + "\nMap Rows: " + tiles + "\nTile Width: " + tileWidth + "\nTile Height: " + tileHeight);

    if (mapCol == tileX && mapRow == tileY) 
    {
        return true;
    }
    else
    {
        return false;
    }
}

现在,我们假设世界为360度,高度为180度,这意味着在缩放级别2下,每个图块应为90x45(4x4网格).而且它可以在缩放2和我认为3的情况下工作,但在缩放4时以及超出彩绘瓷砖的位置会跳到预期位置的北部.

Now we're assuming the world is 360 degrees across and 180 degrees tall which means, at zoom level 2, each tile should be 90x45 (a 4x4 grid). And it works at zoom 2 and I think 3 but at zoom 4 and beyond the painted tile jumps north of the expected spot.

我的感觉是问题出在我们假设坐标转换是如何工作的(我们假设Google的世界地图是一个布局精美的平面,也许对我们来说是天真幼稚的),或者google地图实际上比180度无论哪种方式,这件事都将在几天后到期,因此我们提前感谢您的任何建议.

My feeling is that the problem lies in our assumption of how coordinate conversion works (we're assuming Google's world map is a nicely laid out flat surface which is perhaps exceptionally naive of us) or maybe the google map is actually taller than 180 degrees. Either way, this thing is due in a few days so we thank you in advance for any advice.

推荐答案

您看到了以下内容: https://developers.google.com/maps/documentation/javascript/examples/map-coordinates 吗?尽管使用了JavaScript,但它显示了正确的坐标转换原理.

Have you seen this: https://developers.google.com/maps/documentation/javascript/examples/map-coordinates ? Although its JavaScript, it shows the principle of the correct coordinate conversion.

您也可以检查我对此

You may also check my answer to this SO question. The class TileProjection I provided there should answer your question. You may e.g. use its method getTileBounds, to check whether your coordinates are inside the tile, or you may use the method latLngToPoint and check, whether x and y of the point are both in the range 0 - TILE_SIZE.

这篇关于从世界坐标转换为图块位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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