解决百度地图android SDK旋转时缩放边界的问题

        最近在用百度地图android SDK。使用过程中发现在设置好指定的坐标范围内显示是没有问题的。但是地图一旋转,原来设置好的范围就显示不出来了。原因是百度地图显示的边界是以地图未作旋转时,根据边界东北角的坐标和边界西南角的坐标来显示的。它会根据屏幕的宽度和高度画一个框,刚好包住边界东北角和边界西南角。但是地图旋转以后,它的边界计算方式不会改变,这样会导致计算的缩放级别也不变,但是地图却旋转了,这样会导致原来可见的范围变的不可见了。

        解决思路,以旋转90度为例,当前屏幕为竖屏,取靠近屏幕顶端的一点为点A,取靠近屏幕低端的一点为B。当前屏幕正好将AB显示出来,如果将地图旋转90度,这样AB就显示不出来的。想象一下,我们先不让地图旋转,先让地图缩放成我们旋转后的级别,然后再进行旋转。怎么能才保证让地图缩放成我们想要的级别呢,我们需要重新设置地图的边界,就是让A和B沿着AB的中点旋转90度,产生出新的坐标,这就是需要重新设置的边界,这样地图就要自动缩放到我们想要的缩放级别。然后我们再进行旋转。

        具体的实现代码:

//这里我已经将百度地图坐标默认改为了gcj02格式,所以代码里面往地图上设置的都是gcj02格式的。百度地图默认坐标格式是bd09格式的 。                       
                        List<LatLng> markerPositions = new ArrayList<>();
                        LatLng latLng1_gcj02 = new LatLng(39.93, 116.43);
                        markerPositions.add(latLng1);
                        LatLng latLng2_gcj02 = new LatLng(39.91,116.42);
                        markerPositions.add(latLng2);
                        LatLng latlng=new LatLng(bdLocation.getLatitude(),bdLocation.getLongitude());
                        markerPositions.add(latlng);//这里再增加一个百度地图当前位置

                        LatLngBounds.Builder builder = new LatLngBounds.Builder();
                        for (LatLng position : markerPositions) {
                            builder.include(position);
                        }
                        LatLngBounds bounds = builder.build();
                        //获取边界,获取后转成WGS84坐标再进行旋转,bd09和gcj02都是加密坐标,旋转后不准
                        double[] pp_ne=CoordinateTransformUtil.gcj02towgs84(bounds.northeast.longitude,bounds.northeast.latitude);
                        double[] pp_ew=CoordinateTransformUtil.gcj02towgs84(bounds.southwest.longitude,bounds.southwest.latitude);
                        double[] pp_center=CoordinateTransformUtil.gcj02towgs84(bounds.getCenter().longitude,bounds.getCenter().latitude);
                        //旋转后的边界
                        LatLng latlngNorthWest=rotateLatLng(new LatLng(pp_center[1],pp_center[0]),new LatLng(pp_ne[1],pp_ne[0]),bdLocation.getDirection());
                        LatLng latlngSouhtEast=rotateLatLng(new LatLng(pp_center[1],pp_center[0]),new LatLng(pp_ew[1],pp_ew[0]),bdLocation.getDirection());
                        //再转成gcj02格式
                        double[] pp_ner=CoordinateTransformUtil.wgs84togcj02(latlngNorthWest.longitude,latlngNorthWest.latitude);
                        double[] pp_ewr=CoordinateTransformUtil.wgs84togcj02(latlngSouhtEast.longitude,latlngSouhtEast.latitude);
                        //创建新的builder,并将新的边界加进去
                        builder = new LatLngBounds.Builder();
                        builder.include(new LatLng(pp_ner[1],pp_ner[0]));
                        builder.include(new LatLng(pp_ewr[1],pp_ewr[0]));
                        bounds=builder.build();
                        MapStatusUpdate mapStatusUpdate1 = MapStatusUpdateFactory.newLatLngZoom(bounds,100,100,100,100);
                        mBaiduMap.setMapStatus(mapStatusUpdate1);
                        //旋转地图
                        MapStatus mapStatus = new MapStatus.Builder()
                                .target(bounds.getCenter())  //中心点就是bounds的中心点
                                .rotate(bdLocation.getDirection())
                                .build();
                        MapStatusUpdate mapStatusUpdate = MapStatusUpdateFactory.newMapStatus(mapStatus);
                        mBaiduMap.setMapStatus(mapStatusUpdate);

        以上代码会用到两个方法,一个是坐标格式转换方法,一个是坐标旋转方法。

坐标旋转方法:这里因为都是近距离旋转,所以当作一个平面来处理,如果要远距离旋转的话需要考虑地球半径的问题。

 public static LatLng rotateLatLng(LatLng centerLatLng, LatLng originalLatLng, double rotationAngle) {
        double centerLat = centerLatLng.latitude;
        double centerLng = centerLatLng.longitude;
        double originalLat = originalLatLng.latitude;
        double originalLng = originalLatLng.longitude;
        double rotationAngleRadians = Math.toRadians(rotationAngle);

        // 将经纬度转换为平面坐标系
        double x = (originalLng - centerLng) * Math.cos(Math.toRadians(centerLat));
        double y = originalLat - centerLat;

        // 进行旋转计算
        double rotatedX = x * Math.cos(rotationAngleRadians) - y * Math.sin(rotationAngleRadians);
        double rotatedY = x * Math.sin(rotationAngleRadians) + y * Math.cos(rotationAngleRadians);

        // 将平面坐标系转换回经纬度
        double rotatedLng = rotatedX / Math.cos(Math.toRadians(centerLat)) + centerLng;
        double rotatedLat = rotatedY + centerLat;

        return new LatLng(rotatedLat, rotatedLng);
    }

坐标格式转换方法:

 static double x_pi = 3.14159265358979324 * 3000.0 / 180.0;
    // π
    static double pi = 3.1415926535897932384626;
    // 长半轴
    static double a = 6378245.0;
    // 扁率
    static double ee = 0.00669342162296594323;

    /**
     * 百度坐标系(BD-09)转WGS坐标
     *
     * @param lng 百度坐标纬度
     * @param lat 百度坐标经度
     * @return WGS84坐标数组
     */
    public static double[] bd09towgs84(double lng, double lat) {
        double[] gcj = bd09togcj02(lng, lat);
        double[] wgs84 = gcj02towgs84(gcj[0], gcj[1]);
        return wgs84;
    }

    /**
     * WGS坐标转百度坐标系(BD-09)
     *
     * @param lng WGS84坐标系的经度
     * @param lat WGS84坐标系的纬度
     * @return 百度坐标数组
     */
    public static double[] wgs84tobd09(double lng, double lat) {
        double[] gcj = wgs84togcj02(lng, lat);
        double[] bd09 = gcj02tobd09(gcj[0], gcj[1]);
        return bd09;
    }

    /**
     * 火星坐标系(GCJ-02)转百度坐标系(BD-09)
     *
     * @see 谷歌、高德——>百度
     * @param lng 火星坐标经度
     * @param lat 火星坐标纬度
     * @return 百度坐标数组
     */
    public static double[] gcj02tobd09(double lng, double lat) {
        double z = Math.sqrt(lng * lng + lat * lat) + 0.00002 * Math.sin(lat * x_pi);
        double theta = Math.atan2(lat, lng) + 0.000003 * Math.cos(lng * x_pi);
        double bd_lng = z * Math.cos(theta) + 0.0065;
        double bd_lat = z * Math.sin(theta) + 0.006;
        return new double[] { bd_lng, bd_lat };
    }

    /**
     * 百度坐标系(BD-09)转火星坐标系(GCJ-02)
     *
     * @see 百度——>谷歌、高德
     * @param lng 百度坐标纬度
     * @param lat 百度坐标经度
     * @return 火星坐标数组
     */
    public static double[] bd09togcj02(double bd_lon, double bd_lat) {
        double x = bd_lon - 0.0065;
        double y = bd_lat - 0.006;
        double z = Math.sqrt(x * x + y * y) - 0.00002 * Math.sin(y * x_pi);
        double theta = Math.atan2(y, x) - 0.000003 * Math.cos(x * x_pi);
        double gg_lng = z * Math.cos(theta);
        double gg_lat = z * Math.sin(theta);
        return new double[] { gg_lng, gg_lat };
    }

    /**
     * WGS84转GCJ02(火星坐标系)
     *
     * @param lng WGS84坐标系的经度
     * @param lat WGS84坐标系的纬度
     * @return 火星坐标数组
     */
    public static double[] wgs84togcj02(double lng, double lat) {
        if (out_of_china(lng, lat)) {
            return new double[] { lng, lat };
        }
        double dlat = transformlat(lng - 105.0, lat - 35.0);
        double dlng = transformlng(lng - 105.0, lat - 35.0);
        double radlat = lat / 180.0 * pi;
        double magic = Math.sin(radlat);
        magic = 1 - ee * magic * magic;
        double sqrtmagic = Math.sqrt(magic);
        dlat = (dlat * 180.0) / ((a * (1 - ee)) / (magic * sqrtmagic) * pi);
        dlng = (dlng * 180.0) / (a / sqrtmagic * Math.cos(radlat) * pi);
        double mglat = lat + dlat;
        double mglng = lng + dlng;
        return new double[] { mglng, mglat };
    }

    /**
     * GCJ02(火星坐标系)转GPS84
     *
     * @param lng 火星坐标系的经度
     * @param lat 火星坐标系纬度
     * @return WGS84坐标数组
     */
    public static double[] gcj02towgs84(double lng, double lat) {
        if (out_of_china(lng, lat)) {
            return new double[] { lng, lat };
        }
        double dlat = transformlat(lng - 105.0, lat - 35.0);
        double dlng = transformlng(lng - 105.0, lat - 35.0);
        double radlat = lat / 180.0 * pi;
        double magic = Math.sin(radlat);
        magic = 1 - ee * magic * magic;
        double sqrtmagic = Math.sqrt(magic);
        dlat = (dlat * 180.0) / ((a * (1 - ee)) / (magic * sqrtmagic) * pi);
        dlng = (dlng * 180.0) / (a / sqrtmagic * Math.cos(radlat) * pi);
        double mglat = lat + dlat;
        double mglng = lng + dlng;
        return new double[] { lng * 2 - mglng, lat * 2 - mglat };
    }

    /**
     * 纬度转换
     *
     * @param lng
     * @param lat
     * @return
     */
    public static double transformlat(double lng, double lat) {
        double ret = -100.0 + 2.0 * lng + 3.0 * lat + 0.2 * lat * lat + 0.1 * lng * lat + 0.2 * Math.sqrt(Math.abs(lng));
        ret += (20.0 * Math.sin(6.0 * lng * pi) + 20.0 * Math.sin(2.0 * lng * pi)) * 2.0 / 3.0;
        ret += (20.0 * Math.sin(lat * pi) + 40.0 * Math.sin(lat / 3.0 * pi)) * 2.0 / 3.0;
        ret += (160.0 * Math.sin(lat / 12.0 * pi) + 320 * Math.sin(lat * pi / 30.0)) * 2.0 / 3.0;
        return ret;
    }

    /**
     * 经度转换
     *
     * @param lng
     * @param lat
     * @return
     */
    public static double transformlng(double lng, double lat) {
        double ret = 300.0 + lng + 2.0 * lat + 0.1 * lng * lng + 0.1 * lng * lat + 0.1 * Math.sqrt(Math.abs(lng));
        ret += (20.0 * Math.sin(6.0 * lng * pi) + 20.0 * Math.sin(2.0 * lng * pi)) * 2.0 / 3.0;
        ret += (20.0 * Math.sin(lng * pi) + 40.0 * Math.sin(lng / 3.0 * pi)) * 2.0 / 3.0;
        ret += (150.0 * Math.sin(lng / 12.0 * pi) + 300.0 * Math.sin(lng / 30.0 * pi)) * 2.0 / 3.0;
        return ret;
    }

    /**
     * 判断是否在国内,不在国内不做偏移
     *
     * @param lng
     * @param lat
     * @return
     */
    public static boolean out_of_china(double lng, double lat) {
        if (lng < 72.004 || lng > 137.8347) {
            return true;
        } else if (lat < 0.8293 || lat > 55.8271) {
            return true;
        }
        return false;
    }

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值