深入学习百度地图Android SDK v4.0.0【第五关】瓦片图层

时间:2021-12-12 01:37:46
官方说是可以使用自己的图片,但是图片的大小和规格都没有明说,主要用于添加自己的图层信息,有在线和离线两种!估计使用率很低,也就不多说了!
package cq.cake.chwl.ui;

import android.content.res.AssetManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;

import com.baidu.mapapi.map.BaiduMap;
import com.baidu.mapapi.map.FileTileProvider;
import com.baidu.mapapi.map.MapStatus;
import com.baidu.mapapi.map.MapStatusUpdate;
import com.baidu.mapapi.map.MapStatusUpdateFactory;
import com.baidu.mapapi.map.Tile;
import com.baidu.mapapi.map.TileOverlay;
import com.baidu.mapapi.map.TileOverlayOptions;
import com.baidu.mapapi.map.TileProvider;
import com.baidu.mapapi.map.UrlTileProvider;
import com.baidu.mapapi.model.LatLng;
import com.baidu.mapapi.model.LatLngBounds;

import java.io.InputStream;
import java.nio.ByteBuffer;

import cq.cake.base.BaseToolbarMapActivity;
import cq.cake.chwl.R;

/**
* 地图SDK自v3.6.0起,新增瓦片图层(tileOverlay), 该图层支持开发者添加自有瓦片数据,
* 包括本地加载和在线下载两种方式。该图层可随地图的平移、缩放、旋转等操作做相应的变换,
* 它仅位于底图之上(即瓦片图层将会遮挡底图,不遮挡其他图层),
* 瓦片图层的添加顺序不会影响其他图层(例如:POI搜索图层、我的位置图层等)的叠加关系,
* 适用于开发者拥有某一区域的地图,并希望使用此区域地图覆盖相应位置的百度地图。
*/
public class TileActivity extends BaseToolbarMapActivity implements BaiduMap.OnMapLoadedCallback{
private TileOverlay tileOverlay;//瓦片图对象
// 设置瓦片图的在线缓存大小,默认为20 M
private static final int TILE_TMP = 20 * 1024 * 1024;
private static final int MAX_LEVEL = 21;
private static final int MIN_LEVEL = 3;
Tile offlineTile;
private boolean mapLoaded = false;
MapStatusUpdate mMapStatusUpdate;

private final String onlineUrl = "http://api0.map.bdimg.com/customimage/tile"
+ "?&x={x}&y={y}&z={z}&udt=20150601&customid=light";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tile);
MapStatus.Builder builder = new MapStatus.Builder();
builder.zoom(16.0f);
builder.target(new LatLng(39.914935D, 116.403119D));
mMapStatusUpdate = MapStatusUpdateFactory.newMapStatus(builder.build());
mBaiduMap.setMapStatus(mMapStatusUpdate);
initOffTile();

}

private void initOffTile() {
TileOverlayOptions options = new TileOverlayOptions();
// 构造显示瓦片图范围,当前为世界范围
LatLng northeast = new LatLng(80, 180);
LatLng southwest = new LatLng(-80, -180);
// 设置离线瓦片图属性option
options.tileProvider(tileProvider)
.setPositionFromBounds(new LatLngBounds.Builder().include(northeast).include(southwest).build());
// 通过option指定相关属性,向地图添加离线瓦片图对象
tileOverlay = mBaiduMap.addTileLayer(options);
if (mapLoaded) {
setMapStatusLimits();
}
}
private void setMapStatusLimits() {
LatLngBounds.Builder builder = new LatLngBounds.Builder();
builder.include(new LatLng(39.94001804746338, 116.41224644234747))
.include(new LatLng(39.90299859954822, 116.38359947963427));
mBaiduMap.setMapStatusLimits(builder.build());
mBaiduMap.setMaxAndMinZoomLevel(17.0f, 16.0f);
mBaiduMap.setMapType(BaiduMap.MAP_TYPE_NONE);
}
@Override
public void onMapLoaded() {
mapLoaded = true;
}

/**
* 定义瓦片图的离线Provider,并实现相关接口
* MAX_LEVEL、MIN_LEVEL 表示地图显示瓦片图的最大、最小级别
* Tile 对象表示地图每个x、y、z状态下的瓦片对象
*/
FileTileProvider tileProvider = new FileTileProvider() {
@Override
public Tile getTile(int x, int y, int z) {
// 根据地图某一状态下x、y、z加载指定的瓦片图
String filedir = "LocalTileImage/" + z + "/" + z + "_" + x + "_" + y + ".jpg";
Bitmap bm = getFromAssets(filedir);
if (bm == null) {
return null;
}
// 瓦片图尺寸必须满足256 * 256
offlineTile = new Tile(bm.getWidth(), bm.getHeight(), toRawData(bm));
bm.recycle();
return offlineTile;
}
@Override
public int getMaxDisLevel() {
return MAX_LEVEL;
}
@Override
public int getMinDisLevel() {
return MIN_LEVEL;
}
};

/**
* 瓦片文件解析为Bitmap
* @param fileName
* @return 瓦片文件的Bitmap
*/
public Bitmap getFromAssets(String fileName) {
AssetManager am = this.getAssets();
InputStream is = null;
Bitmap bm;

try {
is = am.open(fileName);
bm = BitmapFactory.decodeStream(is);
return bm;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
/**
* 解析Bitmap
* @param bitmap
* @return
*/
byte[] toRawData(Bitmap bitmap) {
ByteBuffer buffer = ByteBuffer.allocate(bitmap.getWidth()
* bitmap.getHeight() * 4);
bitmap.copyPixelsToBuffer(buffer);
byte[] data = buffer.array();
buffer.clear();
return data;
}


/**
* 使用瓦片图的在线方式
*/
private void onlineTile() {

mBaiduMap.setMapType(BaiduMap.MAP_TYPE_NORMAL);
if (tileOverlay != null && mBaiduMap != null) {
tileOverlay.removeTileOverlay();
}
// final String urlString = mEditText.getText().toString();
/**
* 定义瓦片图的在线Provider,并实现相关接口
* MAX_LEVEL、MIN_LEVEL 表示地图显示瓦片图的最大、最小级别
* urlString 表示在线瓦片图的URL地址
*/
TileProvider tileProvider = new UrlTileProvider() {
@Override
public int getMaxDisLevel() {
return MAX_LEVEL;
}

@Override
public int getMinDisLevel() {
return MIN_LEVEL;
}

@Override
public String getTileUrl() {
return onlineUrl;
}

};
TileOverlayOptions options = new TileOverlayOptions();
// 构造显示瓦片图范围,当前为世界范围
LatLng northeast = new LatLng(80, 180);
LatLng southwest = new LatLng(-80, -180);
// 通过option指定相关属性,向地图添加在线瓦片图对象
tileOverlay = mBaiduMap.addTileLayer(options.tileProvider(tileProvider).setMaxTileTmp(TILE_TMP)
.setPositionFromBounds(new LatLngBounds.Builder().include(northeast).include(southwest).build()));
if (mapLoaded) {
mBaiduMap.setMaxAndMinZoomLevel(21.0f, 3.0f);
mBaiduMap.setMapStatusLimits(new LatLngBounds.Builder().include(northeast).include(southwest).build());
mBaiduMap.setMapStatus(mMapStatusUpdate);
}
}

}

最后的效果图:

深入学习百度地图Android SDK v4.0.0【第五关】瓦片图层