Android 高德地图在地图上面添加背景图和设置滑动范围

高德地图:在地图上面添加背景图和设置滑动范围

因为我这边开发的是景区,所以在地图上面景区的位置就添加了一个景区的背景图,代码如下:

if (!mMapBgImg.contains("http")) {
            AsyncUtil.async(new Function<String, Bitmap>() {
                @Override
                public Bitmap apply(String o) throws Exception {
                    Bitmap bitmap = null;
                    try {
                        bitmap = Glide.with(mContext)
                                .asBitmap()
                                .load(mMapBgImg)
                                .into(mMapBgWidth, mMapBgHeight)
                                .get();
                    } catch (ExecutionException e) {
                        e.printStackTrace();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    return bitmap;
                }
            }, new Consumer<Bitmap>() {
                @Override
                public void accept(Bitmap bitmap) throws Exception {
                    if (bitmap != null) {
                        groundoverlay = mAmap.addGroundOverlay(new GroundOverlayOptions()
                                .anchor(0.5f, 0.5f)
                                .transparency(0.1f)
                                .image(BitmapDescriptorFactory.fromBitmap(bitmap))
                                .positionFromBounds(mBounds));
                    }
                }
            });
        } else {
            AsyncUtil.async(new Function<String, Bitmap>() {
                @Override
                public Bitmap apply(String o) throws Exception {
                    Bitmap bitmap = null;
                    try {
                        bitmap = Glide.with(mContext)
                                .asBitmap()
                                .load(mMapBgImg)
                                .into(mMapBgWidth, mMapBgHeight)
                                .get();
                    } catch (ExecutionException e) {
                        e.printStackTrace();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    return bitmap;
                }
            }, new Consumer<Bitmap>() {
                @Override
                public void accept(Bitmap bitmap) throws Exception {
                    if (bitmap != null) {
                        groundoverlay = mAmap.addGroundOverlay(new GroundOverlayOptions()
                                .anchor(0.5f, 0.5f)
                                .transparency(0.1f)
                                .image(BitmapDescriptorFactory.fromBitmap(bitmap))
                                .positionFromBounds(mBounds));
                        // 保存到本地
                        saveBitmap(bitmap);
                    }
                }
            });
        }

因为我的代码中做了缓存到本地的操作,但是还是可能出现没有缓存到本地的情况,没有缓存到本地的情况就属于有http的链接,因为将图片地址转为bitmap,所以我这边用的是rxjava异步加载的方式进行加载。

groundoverlay = mAmap.addGroundOverlay(new GroundOverlayOptions()
                                .anchor(0.5f, 0.5f)
                                .transparency(0.1f)
                                .image(BitmapDescriptorFactory.fromBitmap(bitmap))
                                .positionFromBounds(mBounds));

这个代码是最主要的,将背景的图片添加到地图的界面上。
相当于添加了一个GroundOverlay,
anchor:是设置图片的对齐方式,[0,0]是左上角,[1,1]是右下角 。我这边设置的是居中对齐
transparency:是设置ground 覆盖物的透明度。
image:是将我们异步加载的图片传给地图。
positionFromBounds:是根据矩形区域设置ground 覆盖物的位置。当设置时忽略旋转的角度,但是画此ground 覆盖物时还是会使用之前的旋转角度。
mBounds:是设置图片覆盖的范围

设置滑动范围

        ScenicSpotGuideBean.MapBgCoordinateListBean northEast = mInfo.getMapBgCoordinateList().get(0);
        ScenicSpotGuideBean.MapBgCoordinateListBean southWest = mInfo.getMapBgCoordinateList().get(1);
        // 设置背景所在的区域
        mBounds = new LatLngBounds.Builder()
                .include(new LatLng(northEast.getLatitude(), northEast.getLongitude()))
                .include(new LatLng(southWest.getLatitude(), southWest.getLongitude())).build();
        //   需要设置滑动范围
        mAmap.setMapStatusLimits(mBounds);

上面代码给的northEast 和southWest 是我这边后台给的景区范围,后台给了两个经纬度的点。

 mAmap.setMapStatusLimits(mBounds);

这句代码是将景区的背景大小设置为滑动范围。
在这里插入图片描述
这个大概是实现之后的界面覆盖的效果,基本上看不到地图的背景。

AsyncUtil的源码:


import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.functions.Consumer;
import io.reactivex.functions.Function;
import io.reactivex.schedulers.Schedulers;

/**
 * 异步操作
 */
public class AsyncUtil {
    /**
     * 执行异步任务,在主线程回调
     * @param function
     * @param action
     */
    public static void async(Function function, Consumer action) {
        Observable observable = Observable.just("").observeOn(Schedulers.io()).map(function);
        observable.observeOn(AndroidSchedulers.mainThread()).subscribe(action);
    }
}

很简单的一个工具类。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值