找出其他两个点之间点 x 和 y 的百分比
Find out precentage of point x and y between two other points
在我的 android 应用程序中,我有简单的 "map",这是基于卫星地图的图像。我在这张地图的两个角上有确切的 LatLng
位置:准确地说是左上角和右下角。现在,通过给定的 LatLng
点,我想创建一个小视图并将其添加到地图上(标记)。为此,我必须计算 "map".
上的 x 和 y 百分比(以像素为单位)
总结一下我的数据:
LatLng MAP_LEFT_TOP; //constant point
LatLng MAP_RIGHT_BOTTOM; //constant point
LatLng location; //point, which should be inside map rectangle
我要计算的内容:
float percentageX; //percentage x position of "location" on the map
float percentageY; //percentage y position of "location" on the map
在这里我找到了函数:
LatLng interpolate(LatLng from, LatLng to, double fraction)
我需要一些相反的东西。这是我的实现,但它没有给我正确的结果(y/longitude 相同):
double findFractionX(double latitudeLeftTop, double latitudeRightBottom, double latitude){
return (latitude - latitudeLeftTop) / (latitudeRightBottom - latitudeLeftTop);
}
然后我可以像这样得到标记的最终位置:
float imageViewWidth; //width of the imageView holding map
float imageViewHeight; //height of the imageView holding map
float x = percentageX * imageViewWidth;
float y = percentageY * imageViewHeight;
编辑:
我的错误是使用纬度获取 x,使用经度获取 y 而恰恰相反。
还是有问题,这是它在应用程序中的样子:
这是它应该在的地方(我用涂鸦来做这个例子):
为了清楚起见,我说的是名为“1.1”的点,请不要注意其他点,它们已硬编码在图像本身中。
因为我有另一个想法要写出来,所以我将把两者都放在一个完整的答案中以获得更多 space。
首先,检查您是否不小心切换了 long
/lat
或 x
/y
:
Long = x
和 lat = y
.
那么,你怎么看你加的样子?当您获得地图标记的确切位置时,简单地向该位置添加视图可能会看起来不合适。
视图本身可能有宽度和高度,您可能希望将它放在标记的顶部,就像在 Google 地图上单击商店时一样?如果你只是把它放在计算好的位置,它会把它的左上角放在那个位置。
在我的 android 应用程序中,我有简单的 "map",这是基于卫星地图的图像。我在这张地图的两个角上有确切的 LatLng
位置:准确地说是左上角和右下角。现在,通过给定的 LatLng
点,我想创建一个小视图并将其添加到地图上(标记)。为此,我必须计算 "map".
总结一下我的数据:
LatLng MAP_LEFT_TOP; //constant point
LatLng MAP_RIGHT_BOTTOM; //constant point
LatLng location; //point, which should be inside map rectangle
我要计算的内容:
float percentageX; //percentage x position of "location" on the map
float percentageY; //percentage y position of "location" on the map
在这里我找到了函数:
LatLng interpolate(LatLng from, LatLng to, double fraction)
我需要一些相反的东西。这是我的实现,但它没有给我正确的结果(y/longitude 相同):
double findFractionX(double latitudeLeftTop, double latitudeRightBottom, double latitude){
return (latitude - latitudeLeftTop) / (latitudeRightBottom - latitudeLeftTop);
}
然后我可以像这样得到标记的最终位置:
float imageViewWidth; //width of the imageView holding map
float imageViewHeight; //height of the imageView holding map
float x = percentageX * imageViewWidth;
float y = percentageY * imageViewHeight;
编辑: 我的错误是使用纬度获取 x,使用经度获取 y 而恰恰相反。
还是有问题,这是它在应用程序中的样子:
这是它应该在的地方(我用涂鸦来做这个例子):
为了清楚起见,我说的是名为“1.1”的点,请不要注意其他点,它们已硬编码在图像本身中。
因为我有另一个想法要写出来,所以我将把两者都放在一个完整的答案中以获得更多 space。
首先,检查您是否不小心切换了 long
/lat
或 x
/y
:
Long = x
和 lat = y
.
那么,你怎么看你加的样子?当您获得地图标记的确切位置时,简单地向该位置添加视图可能会看起来不合适。
视图本身可能有宽度和高度,您可能希望将它放在标记的顶部,就像在 Google 地图上单击商店时一样?如果你只是把它放在计算好的位置,它会把它的左上角放在那个位置。