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

很簡單的一個工具類。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章