需求:
在地图上给定2个坐标点,然后将2个坐标点通过缩放都能显示出来。
实现:
通过查阅高德地图接入文档和API能找到缩放的API高德地图文档,看到以下说明
限制地图的显示范围
LatLng southwestLatLng = new LatLng(33.789925, 104.838326);
LatLng northeastLatLng = new LatLng(38.740688, 114.647472);
LatLngBounds latLngBounds = new LatLngBounds(southwestLatLng, northeastLatLng);
aMap.setMapStatusLimits(latLngBounds);
注意,我们并不需要限制地图的缩放功能,所以最终仅仅调用地图的缩放功能即可
给定左下的坐标和右上的坐标来确定一个显示范围,那么实际开发中我们拿到的坐标还需要进行比较后在创建该对象
对比坐标,使用坐标A和坐标B中的经纬度,谁的大谁就在右上角,谁小谁就在左下角,为此封装了一个方法:
/**
* 根据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);
}
最后调用cameraAPI即可,这里使用的动画的方式,10代表的是padding内边距
mAMap.animateCamera(CameraUpdateFactory.newLatLngBounds(latLngBounds, 10),1000L,null);
4方位种效果