TileProvider方法getTile - 需要翻译x和y为纬度/长 [英] TileProvider method getTile - need to translate x and y to lat/long
问题描述
我移植iOS应用程序到Android,并使用谷歌地图Android的API V2。
应用程序需要绘制热图叠加到地图上。
I’m porting an iOS app to Android, and using Google Maps Android API v2.
The application needs to draw a heatmap overlay onto the map.
到目前为止,它看起来像最好的选择是使用 TileOverlay
,并实现自定义的 TileProvider
。在 getTile
,我的方法是给定的x,y和缩放,以及需要返回一个位图在瓷砖的形式方法
。到目前为止,一切都很好。
So far, it looks like the best option is to use a TileOverlay
and implement a custom TileProvider
. In the method getTile
, my method is given x, y, and zoom, and needs to return a bitmap in the form of a Tile
. So far, so good.
我有,我会用长绘制径向渐变到的位图,每一个纬度/热图项的数组。我有以下两个任务的麻烦:
I have an array of heatmap items that I will use to draw radial gradients onto the bitmap, each with a lat/long. I am having trouble with the following two tasks:
- 如何确定是否由X,Y,和变焦psented瓷砖重新$ P $包含/长热图项目的土地增值税?
- 如何翻译热图项目的纬度/长位图的x / y坐标。
感谢您的帮助!
更新
由于以下MaciejGórski的回答,和马辛的实现我能得到我的问题回答了上半场,但我仍然需要与第二部分的帮助。为了澄清,我需要一个函数长返回tile的x / y坐标为指定纬度/。我试图扭转MaciejGórski的和马辛的回答计算没有运气。
Thanks to MaciejGórski's answer below, and marcin's implementation I was able to get the 1st half of my question answered, but I still need help with the 2nd part. To clarify, I need a function to return the x/y coordinates of the tile for a specified lat/long. I've tried reversing the calculations of MaciejGórski's and marcin's answer with no luck.
public static Point fromLatLng(LatLng latlng, int zoom){
int noTiles = (1 << zoom);
double longitudeSpan = 360.0 / noTiles;
double mercator = fromLatitude(latlng.latitude);
int y = ((int)(mercator / 360 * noTiles)) + 180;
int x = (int)(latlng.longitude / longitudeSpan) + 180;
return new Point(x, y);
}
任何帮助是AP preciated!
Any help is appreciated!
推荐答案
在缩放级别0,只有一个区域(X = 0,Y = 0)。瓷砖的下一个缩放级别数量的四倍(在X和Y一倍)。
On zoom level 0, there is only one tile (x=0,y=0). On next zoom level number of tiles are quadrupled (doubled on x and y).
这意味着在缩放级别是W
,X可以是范围℃的值为0,1&LT;&LT; W)。
This means on zoom level W
, x may be a value in range <0, 1 << W).
从技术文档:
的瓦片的坐标被从图的左上角(西北)角测量。在缩放级别N,瓦坐标x值范围从0到2N - 1,增加从西部到东部,而y值的范围从0到2N - 1,增加从北到南。
The coordinates of the tiles are measured from the top left (northwest) corner of the map. At zoom level N, the x values of the tile coordinates range from 0 to 2N - 1 and increase from west to east and the y values range from 0 to 2N - 1 and increase from north to south.
您可以用这个简单的计算实现的。
You can achieve this using simple calculations.
有关经度,这是简单的:
For longitude this is straightforward :
double longitudeMin = (((double) x) / (1 << zoom)) * 360 - 180;
double longitudeMax = (((double) x + 1) / (1 << zoom)) * 360 - 180;
longitudeMax = Double.longBitsToDouble(Double.doubleToLongBits(longitudeMax) - 1); // adjust
下面x被第一比例为&LT; 0,1),然后进入&LT; -180,180)
Here x is first scaled into <0,1), then into <-180,180).
的最大值被调节,因此它不会与下一个区域重叠。你可以跳过这一点。
The max value is adjusted, so it doesn't overlap with the next area. You may skip this.
有关纬度,这将是一个有点困难,因为谷歌地图使用墨卡托投影。
For latitude this will be a bit harder, because Google Maps use Mercator projection.
首先,你Y比例,就像它在范围和LT; -180,180)。需要注意的是价值需要得到扭转。
First you scale y just like it was in range <-180,180). Note that the values need to be reversed.
double mercatorMax = 180 - (((double) y) / (1 << zoom)) * 360;
double mercatorMin = 180 - (((double) y + 1) / (1 << zoom)) * 360;
现在你用一个神奇的功能,做墨卡托投影(从<一个href="https://github.com/mg6maciej/android-maps-extensions/blob/develop/android-maps-extensions/src/main/java/com/androidmapsextensions/utils/SphericalMercator.java"相对=nofollow> SphericalMercator.java ):
Now you use a magical function that does Mercator projection (from SphericalMercator.java):
public static double toLatitude(double mercator) {
double radians = Math.atan(Math.exp(Math.toRadians(mercator)));
return Math.toDegrees(2 * radians) - 90;
}
latitudeMax = SphericalMercator.toLatitude(mercatorMax);
latitudeMin = SphericalMercator.toLatitude(mercatorMin);
latitudeMin = Double.longBitsToDouble(Double.doubleToLongBits(latitudeMin) + 1);
这是从内存类型和以任何方式未经测试,所以如果有错误,请把一个评论,我会解决它。
This was was typed from memory and was not tested in any way, so if there is an error there, please put a comment and I will fix it.
这篇关于TileProvider方法getTile - 需要翻译x和y为纬度/长的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!