【问题标题】:Arrow Mark over Polyline in Android Google Route MapAndroid Google Route Map 中折线上的箭头标记
【发布时间】:2013-08-18 22:55:00
【问题描述】:

如何在 Android Google 地图 v2 中在折线上显示箭头

我浏览了几个链接,其中大部分都提供了 JS https://google-developers.appspot.com/maps/documentation/javascript/examples/overlay-symbol-arrow 的链接,但我在 Android 中需要而不是 JS,我无法在 Android 中使用这些代码

有些人正在使用 MapActivity 我也按照教程创建地图活动 https://developers.google.com/maps/documentation/android/v1/hello-mapview 但它不起作用我无法生成地图活动,因此我无法使用 locationoverlays

我也有一些评论的帖子,因为它在 v3 中可用,但现在我的要求是在 v2 中

这个问题已经被问过很多次了,我知道但我仍然找不到任何合适的答案

如何在折线上显示箭头以显示我应该去的方向

任何示例或教程都会很有帮助。

谢谢

【问题讨论】:

标签: android google-maps google-maps-android-api-2 polyline


【解决方案1】:

Google Maps API v2 Demo 中有一个 MarkerDemoActivity 类,您可以在其中看到自定义图像如何设置为 GoogleMap。您可以使用标记来显示这样的箭头:

// 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,您可以通过以下方式获取它们:

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

经典的旋转游戏算法:D

确保您的箭头图像指向上方。请注意,如果您不想使用图像来显示箭头,而您只想使用折线,则可以通过获取最后一个 LatLng 点(您将显示箭头的位置)并使用translation 算法以 45º 画出构成箭头的两条线。

【讨论】:

  • 这几乎可以完美运行。问题是位图的底部从您指定的位置开始,因此箭头出现在略高于线的位置。它看起来几乎是 <____>
  • 添加标记时可以标记位移。标记.setAnchor(0.5f, 0f);只玩变量
【解决方案2】:

最近,Google 在 Google Maps Android API v2 中为折线实现了此功能。

他们添加了使用自定义位图自定义多段线起点和终点的功能。使用它,您将能够向多段线添加箭头。

请参阅Shapes Guide 中有关线帽的信息。请参阅折线和多边形教程中的example

您也可以在这里阅读相应的博文:

https://maps-apis.googleblog.com/2017/02/styling-and-custom-data-for-polylines.html

【讨论】: