您的位置:首页 > 移动开发 > Android开发

【Android】高德地图根据2个坐标智能缩放地图

2017-11-09 09:41 381 查看

需求:

在地图上给定2个坐标点,然后将2个坐标点通过缩放都能显示出来。

实现:

通过查阅高德地图接入文档和API能找到缩放的API高德地图文档,看到以下说明


限制地图的显示范围

从地图 SDK V4.1.0 起新增了设置地图显示范围的方法,手机屏幕仅显示设定的地图范围,例如:希望设置仅显示北京市区地图,可使用此功能。

注意:如果限制了地图显示范围,地图旋转手势将会失效。

调用示例如下:

Java

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方位种效果







内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  高德地图
相关文章推荐