项目近期开发加入百度地图的需求,初次使用遇到一些基础问题,总结如下:
百度地图的KEY申请地址http://lbsyun.baidu.com/,基础使用直接看百度地图API就可以,有Demo。
百度地图显示的时候全是网格没有地图,基本肯定就是KEY的问题。
1、BaiduMapOptions
1 | BaiduMapOptions compassEnabled(boolean enabled)//设置是否允许指南针,默认允许。 |
2、UiSettings
1 | MapView mMapView = (MapView)findViewById(R.id.mapview); |
3、根据坐标和级别缩放地图
1 | //地图级别3-2000km |
4、根据城市名称获取城市中心坐标或根据坐标反编码
1 | ** |
5、在地图上构建标志
1 | BitmapDescriptor bitmap = BitmapDescriptorFactory |
百度地图在合适视野范围内显示所有的点
通常地图会显示出多个点,如像行驶轨迹等,往往在屏幕上一次性就全部显示出来,那要怎么设置地图的显示宽高,才能将所有的点合理的显示出来呢。看看以下的代码实现:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43private void setMyLocation() {
list = new ArrayList<>();
LatLng ll = new LatLng(24.475982,
118.093132);
LatLng ll1 = new LatLng(24.477382,
118.093232);
LatLng ll2 = new LatLng(24.478382,
118.093832);
LatLng ll3 = new LatLng(24.487382,
118.094232);
LatLng ll4 = new LatLng(24.497982,
118.099562);
list.add(ll);
list.add(ll1);
list.add(ll2);
list.add(ll3);
list.add(ll4);
MapStatus.Builder builder = new MapStatus.Builder();
builder.target(ll).zoom(18.0f);
// mBaiduMap.animateMapStatus(MapStatusUpdateFactory.newMapStatus(builder.build()));
for (LatLng latLng : list) {
mBaiduMap.addOverlay(new BaiduMapUtil().setMarker(latLng));
}
mBaiduMap.addOverlay(new BaiduMapUtil().Polyline(list));
//将所有的坐标显示出来的合理视图
if (isFrist) {
mBaiduMap.setOnMapLoadedCallback(new BaiduMap.OnMapLoadedCallback() {
@Override
public void onMapLoaded() {
isFrist=false;
mBaiduMap.animateMapStatus(new BaiduMapUtil().setLatLngBounds(list, mMapView));
}
});
}else{
mBaiduMap.animateMapStatus(new BaiduMapUtil().setLatLngBounds(list, mMapView));
}
}以下为工具类BaiduMapUtil的具体实现
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50public class BaiduMapUtil {
/**
* 绘制Marker,地图上常见的类似气球形状的图层
*/
public MarkerOptions setMarker(LatLng latLng) {
return setMarker(latLng, R.drawable.icon_defult_marker);
}
public MarkerOptions setMarker(LatLng latLng, int drawableId) {
MarkerOptions markerOptions = new MarkerOptions();//参数设置类
markerOptions.position(latLng);//marker坐标位置
markerOptions.icon(BitmapDescriptorFactory
.fromResource(drawableId));//marker图标,可以自定义
markerOptions.draggable(false);//是否可拖拽,默认不可拖拽
markerOptions.anchor(0.5f, 1.0f);//设置 marker覆盖物与位置点的位置关系,默认(0.5f, 1.0f)水平居中,垂直下对齐
markerOptions.alpha(0.8f);//marker图标透明度,0~1.0,默认为1.0
markerOptions.animateType(MarkerOptions.MarkerAnimateType.none);//marker出现的方式,从天上掉下
markerOptions.flat(false);//marker突变是否平贴地面
markerOptions.zIndex(1);//index
return markerOptions;
}
/**
* 绘制折线
*/
public PolylineOptions Polyline(List<LatLng> points) {
PolylineOptions polylineOptions = new PolylineOptions();//参数设置类
polylineOptions.width(10);//宽度,单位:像素
polylineOptions.color(0xAAFF0000);//设置折线颜色
polylineOptions.points(points);//折线顶点坐标集
return polylineOptions;
}
/**
* 多个点,在Android里面显示合理的缩放级
*/
public MapStatusUpdate setLatLngBounds(List<LatLng> points, MapView mMapView) {
LatLngBounds.Builder builder2 = new LatLngBounds.Builder();
for (LatLng p : points) {
builder2 = builder2.include(p);
}
LatLngBounds latlngBounds = builder2.build();
MapStatusUpdate u = MapStatusUpdateFactory.newLatLngBounds(latlngBounds, mMapView.getWidth(), mMapView.getHeight());
return u;
}
}
1 | if (isFrist) { |
- 这段代码为显示合理范围的核心代码,加入是否为首次的判断,因为要在OnMapLoadedCallback中实现是关键,否则mapview的with和height取得是0,如果在其他视觉范围内,想恢复到这个合理的范围则经过首次加载后,就不会再执行OnMapLoadedCallback方法,所以要判断是否为第一次加载的状态。