最佳答案
我有一个情况要显示多段线上方两个标记之间的距离。
以下是我的要求:
我设法以其他方式做到了,这是一种非常有趣的方式,通过这种方式您可以自定义它以实现任何目标。
步骤:
- 使用 Location.distanceBetween() 计算标记之间的距离方法。
- 创建一个新的
Layout XML
文件并创建您想要为文本显示的任何类型的UI
。在我的例子中,它只包含一个TextView
。 - 在
TextView
中设置计算的距离。 - 将
XML 布局
转换为位图
。 - 从该
Bitmap
对象创建一个BitmapDescriptor
。 - 找出所需标记之间的中点,并使用在上一步中创建的
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/