且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在谷歌Android路线图箭头标记过折线

更新时间:2023-12-05 20:27:16

在的谷歌地图API V2演示有一个MarkerDemoActivity类,你可以在其中看到的自定义图像是如何设置为GoogleMap的。你可以使用一个标记,以表明这样的箭头:

In the Google Maps API v2 Demo there is a MarkerDemoActivity class in which you can see how a custom Image is set to a GoogleMap. You could use a marker to show the arrowhead like this:

// First you need rotate the bitmap of the arrowhead somewhere in your code
Matrix matrix = new Matrix();
matrix.postRotate(rotationDegrees);
// Create the rotated arrowhead bitmap
Bitmap arrowheadBitmap = Bitmap.createBitmap(originalBitmap, 0, 0,
                      width, height, matrix, true);
// Now we are gonna to add a marker
mArrowhead = mMap.addMarker(new MarkerOptions()
.position(POSITION)
.icon(arrowheadBitmap));

一些澄清: 当你绘制的路线,或者如果你只是得到了两个点来绘制,以获得 rotationDegrees 则可以通过这样做让他们:

Some clarifications: When you are drawing a route, or if you simply got two points to draw, to get the rotationDegrees you can get them by doing this:

rotationDegrees = Math.toDegrees(Math.atan2(originLat-destinyLat, originLon-destinyLon));

经典的旋转游戏算法:D

The classic rotation game algorithm :D

要确保,你箭头图像朝上。需要注意的是,如果你不想使用图像显示箭头和替代那种,你想只使用多段线,就可以实现,通过采取最后的经纬度点(你要去哪里,以显示箭头),并使用通过45°翻译算法,绘制两条线,使箭头。

Be sure of that you arrowhead image is pointing up. Note that if you don't want to use an Image to show the arrowhead and instead of that, you want use only Polylines, you can achieve that by taking the last LatLng point (where you are going to display the arrowhead) and use a translation algorithm by 45º to draw the two lines that make the arrowhead.