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

Tam*_*mas 6 android google-maps google-maps-android-api-2

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

地图

我已经发现这个样品约2个绘制标记之间的线路,但我怎么添加一行上面的文字?

gpr*_*our 5

我有一种情况来显示折线上方两个标记之间的距离。

以下是我的要求:

在此处输入图片说明

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

脚步:

  1. 使用Location.distanceBetween()方法计算标记之间的距离。
  2. 创建一个新Layout XML文件并创建UI要显示的文本类型。就我而言,它只包含一个TextView
  3. 在中设置计算出的距离TextView
  4. 将转换XML LayoutBitmap
  5. BitmapDescriptor从该Bitmap对象创建一个。
  6. 找出所需标记之间的中点,并使用BitmapDescriptor最后一步中创建的添加自定义标记,即可完成操作。

码:

计算两个标记之间的距离

float[] distance1 = new float[1];
Location.distanceBetween(userMarker.getPosition().latitude, userMarker.getPosition().longitude, positionMarker.getPosition().latitude, positionMarker.getPosition().longitude, distance1);
Run Code Online (Sandbox Code Playgroud)

从XML布局文件创建位图

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);
Run Code Online (Sandbox Code Playgroud)

要找出两个标记之间的中点,请执行以下操作:

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);
Run Code Online (Sandbox Code Playgroud)

使用我们在先前步骤中创建的位图描述符添加新的皮肤标记

Marker centerOneMarker = mMap.addMarker(new MarkerOptions()
                .position(new LatLng(lat3, lon3))
                .icon(flagBitmapDescriptor));
Run Code Online (Sandbox Code Playgroud)

希望对您有帮助。


小智 3

一种想法是使用两个标记的纬度和经度坐标计算它们之间的中点,然后放置一个包含您想要显示的任何信息的信息窗口。或者像下面这样的东西可能会有所帮助:链接