Flutter amap_flutter_map 缩放地图使两点都在地图上显示

CameraUpdate update = CameraUpdate.newLatLngBounds(bound, 10);
_mapController?.moveCamera(update);

上面这个不好用,实际测试,并不会缩放地图。

最后只能是先算出两点的中心点后,以中心点为基点,计算两点间的横向距离,再由距离大体估算出zoom的值来:

_configWorkDayMap() async {
    //116
    LatLng latLng1 = LatLng(39.91,116.65);
    LatLng latLng2 = LatLng(39.93,116.70);
    double radius = 1000;

    Marker marker1 =  Marker(
      position: latLng1,
      icon: BitmapDescriptor.fromIconPath('assets/start.png'),
    );

    Marker marker2 =  Marker(
      position: latLng2,
      icon: BitmapDescriptor.fromIconPath('assets/end.png'),
    );

    _markers.addAll([marker1, marker2]);

    final Polygon polygon = Polygon(
      strokeColor: Colors.blue.withAlpha(8),
      fillColor: Colors.blue.withAlpha(2),
      strokeWidth: 1,
      points: createCirclePoints(latLng1,radius),
    );
    _polygons.add(polygon);

    {
      double minLat = min(latLng1.latitude, latLng2.latitude);
      double minLng = min(latLng1.longitude, latLng2.longitude);
      double maxLat = max(latLng1.latitude, latLng2.latitude);
      double maxLng = max(latLng1.longitude, latLng2.longitude);

      LatLng centerLatLng = LatLng((maxLat+minLat)/2, (maxLng+minLng)/2);

      double diffLng = maxLng - minLng;

      //地球周长
      double perimeter = 2 * pi * 6371000;
      //纬度latitude所在圆的周长
      double perimeterLatitude = perimeter * cos(pi * centerLatLng.latitude / 180);
      //一米对应的经度(东西方向)1M实际度
      double longitudePerMi = 360 / perimeterLatitude;

      double distance =  diffLng / longitudePerMi;




      // zoom 19
      // 13 /  2.5 *  20 = 104

      // zoom 18
      // 13 / 3  * 50  = 217

      // zoom  17
      // 13 / 1.5  * 50   = 434

      // zoom 16
      // 13 / 1.5  * 50  = 834



      // zoom 15
      // 13 / 1.5 * 200  =  1668



      // zoom 14
      // 13 /2  * 500 = 3250

      // zoom 13
      // 13 / 1.8  * 1000  = 7222



      // zoom 12
      // 13 / 1.8  * 2000  = 14444



      // zoom 11
      // 13 / 1.8  * 5000  = 43333



      // zoom 10
      // 13 / 1.3  * 10000  = 10000

      // zoom 9
      // 13 / 1.8  *  20000 = 144444



      // zoom 8
      // 13 / 3  * 50 000    = 216666

      // zoom  7
      // 13 / 2  * 50 000  = 325000

      // zoom  6
      // 13 /   *   = 650000


      double zoom = 2;
      if(distance <650000){
        zoom = 6;
      }
      if(distance <200000){
        zoom = 9;
      }
      if(distance <50000){
        zoom = 11;
      }
      if(distance <7500){
        zoom = 13;
      }
      if(distance <1700){
        zoom = 15;
      }

      print('==== zoom:$zoom distance:$distance');

      CameraUpdate update = CameraUpdate.newLatLngZoom(centerLatLng, zoom);



      _mapController?.moveCamera(update,animated: true);
    }

    setState(() {});
  }

最后再吐槽一下高德的Flutter官方组件,就是个半成品,只有基础功能,很多原生有的功能都是不支持的。垃圾,真心不如原来三方的amap_all_fluttify,要不是amap_all_fluttify不支持null safe,真是毫无必要换用官方组件。不知道这些大厂的管理层除了天天让程序员996外,作啥正经事了。

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值