我有一条从directions request获得的折线,我希望它显示所有可见点的google地图,但我不知道如何使照相机显示所有点。我这样添加折线:
fun MapFragment.addPolyline(line: String) {
val decodedPath: List<LatLng> = PolyUtil.decode(line)
getMap()?.addPolyline(PolylineOptions().addAll(decodedPath))
}
然后,我将相机对准这样的点之一:
fun MapFragment.setCamera(latLng: LatLng) {
getMap()?.moveCamera(
CameraUpdateFactory.newLatLngZoom(
latLng, 20f
)
)
}
但是,这仅显示折线的一部分。我需要更改缩放比例,以便地图覆盖整个折线。我该如何实现?
您必须进行latlngbound。可以使用此方法绘制整个点的折线。
fun drawPolyline(listlatlng: ArrayList<LatLng>) {
var latlngHistory: ArrayList<LatLng> = ArrayList()
val bc = LatLngBounds.Builder()
for (item in listlatlng) {
latlngHistory.add(item)
bc.include(item)
}
mMap?.clear()
var path: Polyline = mMap!!.addPolyline(
PolylineOptions().addAll(latlngHistory)
)
path.width = 10F
path.color = Color.parseColor("#A3258F")
if (latlngHistory != null && latlngHistory.size != 0) {
if (latlngHistory.size < 2) {
bc.include(latlngHistory.get(0))
bc.include(latlngHistory.get(latlngHistory.size - 1))
}
//cardll is a layout refernce on which map is displaying
val width = cardll?.width
val height = cardll?.height
if (width != null && height != null) {
mMap!!.moveCamera(
CameraUpdateFactory.newLatLngBounds(
bc.build(),
width!!,
height!!,
60
)
)
}
}
}