高德地图:在地图上面添加背景图和设置滑动范围
因为我这边开发的是景区,所以在地图上面景区的位置就添加了一个景区的背景图,代码如下:
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);
}
}
很简单的一个工具类。