可以根据两个坐标智能缩放地图(代码如下)
LatLngBounds latLngBounds = createBounds("纬度1", "经度1", "纬度2", "经度2");
//newLatLngBoundsRect()方法参数注释
//latlngbounds - 地图显示经纬度范围。
//paddingLeft - 设置经纬度范围和mapView左边缘的空隙。
//paddingRight - 设置经纬度范围和mapView右边缘的空隙。
//paddingTop - 设置经纬度范围和mapView上边缘的空隙。
//paddingBottom - 设置经纬度范围和mapView下边缘的空隙。 aMap.animateCamera(CameraUpdateFactory.newLatLngBoundsRect(), 1000L, null);
createBounds()方法 如下:
/**
* 根据2个坐标返回一个矩形Bounds
* 以此来智能缩放地图显示
*/
public static LatLngBounds createBounds(Double latA, Double lngA, Double latB, Double lngB) {
LatLng northeastLatLng;
LatLng southwestLatLng;
Double topLat, topLng;
Double bottomLat, bottomLng;
if (latA >= latB) {
topLat = latA;
bottomLat = latB;
} else {
topLat = latB;
bottomLat = latA;
}
if (lngA >= lngB) {
topLng = lngA;
bottomLng = lngB;
} else {
topLng = lngB;
bottomLng = lngA;
}
northeastLatLng = new LatLng(topLat, topLng);
southwestLatLng = new LatLng(bottomLat, bottomLng);
return new LatLngBounds(southwestLatLng, northeastLatLng);
}
以上是一种实现方法,但是不是很完美 如果你有多个点的话上面的方法难免有些不足存在缺陷。
还有一种方法就是用LatLngBounds对象去include()LatLngd对象 也就是经纬度.方法如下:
LatLngBounds.Builder b = LatLngBounds.builder();
//tempList就是我们存储(LatLng )经纬度的集合,需要小伙伴们自己add
for (LatLng latLng : tempList) {
b.include(latLng);
}
LatLngBounds bounds = b.build();
//newLatLngBoundsRect()方法参数注释
//latlngbounds - 地图显示经纬度范围。
//paddingLeft - 设置经纬度范围和mapView左边缘的空隙。
//paddingRight - 设置经纬度范围和mapView右边缘的空隙。
//paddingTop - 设置经纬度范围和mapView上边缘的空隙。
//paddingBottom - 设置经纬度范围和mapView下边缘的空隙。
aMap.animateCamera(CameraUpdateFactory.newLatLngBoundsRect(), 1000L, null);
建议大家采用第二种方法。
希望能帮助到各位。