android - 如何在谷歌地图中的折线上方添加文字?

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

我有 2 个标记,我想在它们之间画一条线,并有一个文本显示线上方的距离,就像在网络版的谷歌地图中一样。

maps

我已经找到了 this关于在 2 个标记之间画一条线的示例,但如何在线上方添加文本?​​

最佳答案

我有一个情况要显示多段线上方两个标记之间的距离。

以下是我的要求:

enter image description here

我设法以其他方式做到了,这是一种非常有趣的方式,通过这种方式您可以自定义它以实现任何目标。

步骤:

  1. 使用 Location.distanceBetween() 计算标记之间的距离方法。
  2. 创建一个新的 Layout XML 文件并创建您想要为文本显示的任何类型的 UI。在我的例子中,它只包含一个 TextView
  3. TextView 中设置计算的距离。
  4. XML 布局 转换为位图
  5. 从该 Bitmap 对象创建一个 BitmapDescriptor
  6. 找出所需标记之间的中点,并使用在上一步中创建的 BitmapDescriptor 添加自定义标记,您就完成了。

代码:

To calculate distance between two markers

float[] distance1 = new float[1];
Location.distanceBetween(userMarker.getPosition().latitude, userMarker.getPosition().longitude, positionMarker.getPosition().latitude, positionMarker.getPosition().longitude, distance1);

Create Bitmap from XML Layout file

LinearLayout distanceMarkerLayout = (LinearLayout) getLayoutInflater().inflate(R.layout.distance_marker_layout, null);

distanceMarkerLayout.setDrawingCacheEnabled(true);
distanceMarkerLayout.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED), MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
distanceMarkerLayout.layout(0, 0, distanceMarkerLayout.getMeasuredWidth(), distanceMarkerLayout.getMeasuredHeight()); 
distanceMarkerLayout.buildDrawingCache(true);

TextView positionDistance = (TextView) distanceMarkerLayout.findViewById(R.id.positionDistance);

positionDistance.setText(distance1[0]+" meters");           

Bitmap flagBitmap = Bitmap.createBitmap(distanceMarkerLayout.getDrawingCache());
                distanceMarkerLayout.setDrawingCacheEnabled(false);
                BitmapDescriptor flagBitmapDescriptor = BitmapDescriptorFactory.fromBitmap(flagBitmap);

To find out mid point between two markers do something like the following:

double dLon = Math.toRadians(flagMarker.getPosition().longitude - positionMarker.getPosition().longitude);

double lat1 = Math.toRadians(positionMarker.getPosition().latitude);
double lat2 = Math.toRadians(flagMarker.getPosition().latitude);
double lon1 = Math.toRadians(positionMarker.getPosition().longitude);

double Bx = Math.cos(lat2) * Math.cos(dLon);
double By = Math.cos(lat2) * Math.sin(dLon);
double lat3 = Math.atan2(Math.sin(lat1) + Math.sin(lat2), Math.sqrt((Math.cos(lat1) + Bx) * (Math.cos(lat1) + Bx) + By * By));
double lon3 = lon1 + Math.atan2(By, Math.cos(lat1) + Bx);

lat3 = Math.toDegrees(lat3);
lon3 = Math.toDegrees(lon3);

Add a new cutom marker by using the bitmap descriptor created by us in previous steps

Marker centerOneMarker = mMap.addMarker(new MarkerOptions()
                .position(new LatLng(lat3, lon3))
                .icon(flagBitmapDescriptor));

希望对你有帮助。

关于android - 如何在谷歌地图中的折线上方添加文字?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29921570/

相关文章:

android - Retrofit 2 Spinner 获取数据失败

android - 自动显示谷歌地图标记的工具栏

android - 从后台恢复后,Flutter GoogleMap 为空白

javascript - 使用 Bootstrap Glyphicon 作为谷歌地图标记

android - 从 Play 商店下载 apk 后,谷歌地图无法在 Android 中运行

Android 谷歌地图最小缩放

android - 如何获取所有wifi接入点的BSSID?

javascript - 渲染函数中 this.state 为 null

java - 订阅按钮点击而不内联实现

android - 谷歌地图 API ( 9757 ) : Authorization failure