高德地图 缩放地图比例把所绘制的坐标显示到可视区域

本文介绍两种智能缩放地图的方法,一种是通过两个坐标创建矩形Bounds,另一种是使用LatLngBounds对象包含多个LatLng对象,后者更适用于处理多点坐标场景。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

可以根据两个坐标智能缩放地图(代码如下)

 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);

建议大家采用第二种方法。

希望能帮助到各位。 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值