问题描述
我正在使用Gooogle Maps API v2开发Android应用程序.我的地图上有标记,我想圈出其中一个.通过使用Circle和Circle Options类,我设法轻松地做到了这一点.但是我也希望我的圆圈在缩放或缩放时在屏幕上保持相同的大小,就像标记一样.这意味着圆必须以像素为单位具有恒定的半径.遗憾的是,我们无法在API v2中设置半径(以像素为单位).
I'm working on an Android Application using Gooogle Maps API v2. I have markers on my map, and I'd like to circle one of them. I managed to do that easily, by using the Circle and Circle Options classes. But I'd also like my circle to keep the same size on the screen when zooming or unzooming, just like the markers do. It means that the circle must have a constant radius in terms of pixels. Sadly, we cannot set a radius in pixels in the API v2.
我尝试了几种解决方案,但不满意.
I have tried several solutions, but I'm not satisfied.
在第一个中,我只是乘以或除以半径:
In the first one, I just multiply or divide the radius :
@Override
public void onCameraChange(CameraPosition position)
{
if(previousZoom > position.zoom) {
mSelectionCircle.setRadius(Math.abs(position.zoom - previousZoom)*2*mSelectionCircle.getRadius());
}
else if(previousZoom < position.zoom) {
mSelectionCircle.setRadius(Math.abs(position.zoom - previousZoom)*mSelectionCircle.getRadius()/2);
}
previousZoom = position.zoom;
}
起初它似乎起作用,但是在快速缩放或用手指缩放时会产生错误的结果.此外,缩放比例在屏幕上清晰可见.
It seemed to work at first, but produces wrong results when zooming quickly or zooming with fingers. Moreover, the scaling is clearly visible on the screen.
我的第二个解决方案使用像素-米转换.想法是在缩放时重新计算以米为单位的半径,因此圆在屏幕上具有恒定的大小.为此,我在屏幕上获得了圆"的当前位置:
My second solution uses pixel-meter conversions. The idea is to recalculate the radius in meters when zooming/unzooming, so the circle has a constant size on the screen. To do that, I get the current position of the Circle on the screen:
Point p1 = mMap.getProjection().toScreenLocation(mSelectionCircle.getCenter());
然后我在圆的边缘上创建另一个点:
Then I create another point which is on the edge of the circle:
Point p2 = new Point(p1.x + radiusInPixels, p1.y);
哪里
int radiusInPixels = 40;
然后,我使用一个函数返回这两个点之间的距离(以米为单位).
After that, I use a function which returns the distance between these two points in meters.
private double convertPixelsToMeters(Point point1, Point point2) {
double angle = Math.acos(Math.sin(point1.x) * Math.sin(point2.x)
+ Math.cos(point1.x) * Math.cos(point2.x) * Math.cos(point1.y- point2.y));
return angle * Math.PI * 6378100.0; // distance in meters
}
6378100是平均地球半径.最后,我设置了新的圆半径:
6378100 is average Earth radius. Finally, I set the new radius of the Circle :
mSelectionCircle.setRadius(convertPixelsToMeters(p1, p2));
从理论上讲应该可以,但是我得到的半径值很可笑(10 ^ 7 m!).转换功能可能不正确?
It should work in theory but I get ridiculous radius values (10^7 m!). The conversion function may be wrong?
那么有没有一种更简单的方法可以做到这一点,或者如果没有,您是否可以帮助我理解为什么我的第二个解决方案不起作用?
So is there a simpler method to do that, or if not, may you help me to understand why my second soluton doesn't work?
谢谢!
推荐答案
请改用Marker
的自定义图标.您可以创建Bitmap
和Canvas
,在后者上绘制并将其用作Marker
图标:
Use a custom icon for Marker
instead. You can create Bitmap
and Canvas
, draw on the latter and use it as a Marker
icon:
new MarkerOptions().icon(BitmapDescriptorFactory.fromBitmap(bitmap))...
这篇关于在Google Maps API v2中在屏幕上绘制恒定大小的圆的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!