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

需求:

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

实现:

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

限制地图的显示范围

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

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

调用示例如下:

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中的经纬度,谁的大谁就在右上角,谁小谁就在左下角,为此封装了一个方法:

[java]  view plain  copy
  1. /** 
  2.      * 根据2个坐标返回一个矩形Bounds 
  3.      * 以此来智能缩放地图显示 
  4.      */  
  5.     public static LatLngBounds createBounds(Double latA,Double lngA,Double latB,Double lngB){  
  6.         LatLng northeastLatLng;  
  7.         LatLng southwestLatLng;  
  8.   
  9.         Double topLat,topLng;  
  10.         Double bottomLat,bottomLng;  
  11.         if(latA>=latB){  
  12.             topLat=latA;  
  13.             bottomLat=latB;  
  14.         }else{  
  15.             topLat=latB;  
  16.             bottomLat=latA;  
  17.         }  
  18.         if(lngA>=lngB){  
  19.             topLng=lngA;  
  20.             bottomLng=lngB;  
  21.         }else{  
  22.             topLng=lngB;  
  23.             bottomLng=lngA;  
  24.         }  
  25.         northeastLatLng=new LatLng(topLat,topLng);  
  26.         southwestLatLng=new LatLng(bottomLat,bottomLng);  
  27.         return new LatLngBounds(southwestLatLng, northeastLatLng);  
  28.     }  

最后调用cameraAPI即可,这里使用的动画的方式,10代表的是padding内边距

[java]  view plain  copy
  1. mAMap.animateCamera(CameraUpdateFactory.newLatLngBounds(latLngBounds, 10),1000L,null);  

4方位种效果




  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值