问题
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;
}
}
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.
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.
回答1:
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.
来源:https://stackoverflow.com/questions/23259288/converting-from-world-coordinates-to-tile-location