Welcome to Ray's Blog

Stay Hungry Stay Foolish - Steve Jobs

0%

百度地图 -Qiao


所有的百度地图 SDK 在使用时,必须先调用SDKInitializer.initialize(Context context)方法进行初始化。

  • MapStaus地图状态。可以设置地图的中心点、缩放级别、仰望角、旋转角。该类是 final 类,要用MapStaus.Builder构造
  • Overlay覆盖物。该类为抽象类。其子类有Arc弧线、Circle圆、 Dot圆点、GroundOverlay矩形区域、Marker标记、Polygon多边形、Polyline折线、Text文本

地图常用类

BaiduMap

用于操作 MapView 的类。

BaiduMap 常用方法

  • animateMapStatus(MapStatusUpdate update)更新地图
  • addOverlay(OverlayOptions options)添加覆盖物
  • clear()清空地图所有的 Overlay 覆盖物以及 InfoWindow
  • setMyLocationEnabled(boolean enabled)是否允许定位图层
  • setMapStatus(MapStatusUpdate update)改变地图状态
  • setMyLocationData(MyLocationData data)设置定位数据
  • setMyLocationConfigeration(MyLocationConfiguration configeration)设置定位图层配置信息

注意事项

  • 在使用定位图层时,要先设置setMyLocationEnabled(true)
  • 添加覆盖物,返回的是Overlay这个超类,形参是OverlayOptions的子类

MapView

  • 用于显示地图视图的控件,生命周期最好与 Activity 一致
  • 在 com.baidu.mapapi.map 包下,在 xml 调用时要使用包名加控件名

MapView 常用方法

  • getMap() 获得地图控制器,返回一个 BaiduMap 类
  • showZoomControls(boolean show) 设置是否显示缩放控件
  • showScaleControl(boolean show) 设置是否显示比例尺控件
  • setLogoPosition(LogoPosition position) 用于多地图显示
  • removeView(View view) 从 MapView 中移除一个子 View
  • getMapLevel() 获取当前地图级别对应比例尺大小
  • setCustomMapStylePath(String customMapStylePath) 设置自定义地图样式

MapView 的使用

  1. 若从 XML 中加载,直接用 findViewById 即可,需要注意的是若不进行任何的设置,默认的中心点是北京,并不是定位的当前点。下面的代码只是简单的演示了如何在加载地图时手动设置定位中心点。
1
2
3
4
5
6
7
      LatLng p = new LatLng(31.22548,121.48326);
mBaiduMap = mMapView.getMap();
mBaiduMap.setMyLocationEnabled(true);
MapStatus.Builder builder = new MapStatus.Builder();
builder.target(p).zoom(18.0f);
mBaiduMap.animateMapStatus(MapStatusUpdateFactory
.newMapStatus(builder.build()));
  1. 如果某个 Activit 只有一个 MapView 用于显示地图。可以直接setContentView(mMapView)mMapViewMapView的实例。在调用时必须先实例化MapView代码演示:
1
2
3
4
5
6
7
8
9
10
if (intent.hasExtra("x") && intent.hasExtra("y")) {
Bundle b = intent.getExtras();
LatLng p = new LatLng(b.getDouble("y"), b.getDouble("x"));
mMapView = new MapView(this,
new BaiduMapOptions()
.mapStatus(new MapStatus.Builder().target(p).zoom(18.0f)
.build()));
}else
mMapView = new MapView(this, new BaiduMapOptions());
setContentView(mMapView);
  1. 使用fragment填充MapView时,需要借助SupportMapFragment类去实现。代码演示:
1
2
3
4
5
6
7
8
9
10
11
12
      MapStatus ms = new MapStatus.Builder()
.overlook(-20)
.zoom(15).build();
//mapView的初始化
BaiduMapOptions bo = new BaiduMapOptio()
.mapStatus(ms)
.compassEnabled(false)
.zoomControlsEnabled(false);
map = SupportMapFragment.newInstance(bo);
FragmentManager manager = getSupportFragmentManager();
manager.beginTransaction()
.add(R.id.map, map, "map_fragment").commit();`
  1. 实时定位。百度地图给的 Demo 中是用其 SDK 中的BDLocationListenerLocationClient类实现的。并没用 Android 自带的定位。代码比较多,就不具体给出了。其实也可以用自带的,使用MyLocationData进行设置.简化代码如下:
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
       LatLng p = new LatLng(31.22548,121.48326);
mBaiduMap = mMapView.getMap();
mBaiduMap.setMyLocationEnabled(true);
MyLocationData locData = new MyLocationData.Builder()
.accuracy(100)
.direction(100).latitude(p.latitude)
.longitude(p.longitude).build();
mBaiduMap.setMyLocationData(locData);

//初始化mapView
MapStatus.Builder builder = new MapStatus.Builder();
builder.target(p).zoom(18.0f);
mBaiduMap.animateMapStatus(MapStatusUpdateFactory
.newMapStatus(builder.build()));
mBaiduMap.setMyLocationConfigeration(new MyLocationConfiguration(
LocationMode.NORMAL, true, null));
代码中的经纬度是固定的,使用时采用实时接收当前点经纬度即可.获得的原始GPS坐标需进行转换。转换代码如下

private LatLng convert(LatLng sourceLatLng){
// 将google地图、soso地图、aliyun地图、mapabc地图和amap地图// 所用坐标转换成百度坐标
//CoordinateConverter converter = new CoordinateConverter();
//converter.from(CoordType.COMMON);
// sourceLatLng待转换坐标
//converter.coord(sourceLatLng);
//LatLng desLatLng = converter.convert();

// 将GPS设备采集的原始GPS坐标转换成百度坐标
CoordinateConverter converter = new CoordinateConverter();
converter.from(CoordType.GPS);
// sourceLatLng待转换坐标
converter.coord(sourceLatLng);
LatLng desLatLng = converter.convert();
return desLatLng;
}

Overlay

添加覆盖物

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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
   /**
* 添加点、线、多边形、圆、文字
*/
public void addCustomElementsDemo() {
// 添加普通折线绘制
LatLng p1 = new LatLng(39.97923, 116.357428);
LatLng p2 = new LatLng(39.94923, 116.397428);
LatLng p3 = new LatLng(39.97923, 116.437428);
List<LatLng> points = new ArrayList<LatLng>();
points.add(p1);
points.add(p2);
points.add(p3);
OverlayOptions ooPolyline = new PolylineOptions().width(10)
.color(0xAAFF0000).points(points);
mPolyline = (Polyline) mBaiduMap.addOverlay(ooPolyline);
mPolyline.setDottedLine(dottedLine.isChecked());

// 添加多颜色分段的折线绘制
LatLng p11 = new LatLng(39.965, 116.444);
LatLng p21 = new LatLng(39.925, 116.494);
LatLng p31 = new LatLng(39.955, 116.534);
LatLng p41 = new LatLng(39.905, 116.594);
LatLng p51 = new LatLng(39.965, 116.644);
List<LatLng> points1 = new ArrayList<LatLng>();
points1.add(p11);
points1.add(p21);
points1.add(p31);
points1.add(p41);
points1.add(p51);
List<Integer> colorValue = new ArrayList<Integer>();
colorValue.add(0xAAFF0000);
colorValue.add(0xAA00FF00);
colorValue.add(0xAA0000FF);
OverlayOptions ooPolyline1 = new PolylineOptions().width(10)
.color(0xAAFF0000).points(points1).colorsValues(colorValue);
mColorfulPolyline = (Polyline) mBaiduMap.addOverlay(ooPolyline1);

// 添加多纹理分段的折线绘制
LatLng p111 = new LatLng(39.865, 116.444);
LatLng p211 = new LatLng(39.825, 116.494);
LatLng p311 = new LatLng(39.855, 116.534);
LatLng p411 = new LatLng(39.805, 116.594);
List<LatLng> points11 = new ArrayList<LatLng>();
points11.add(p111);
points11.add(p211);
points11.add(p311);
points11.add(p411);
List<BitmapDescriptor> textureList = new ArrayList<BitmapDescriptor>();
textureList.add(mRedTexture);
textureList.add(mBlueTexture);
textureList.add(mGreenTexture);
List<Integer> textureIndexs = new ArrayList<Integer>();
textureIndexs.add(0);
textureIndexs.add(1);
textureIndexs.add(2);
OverlayOptions ooPolyline11 = new PolylineOptions().width(20)
.points(points11).dottedLine(true).customTextureList(textureList).textureIndex(textureIndexs);
mTexturePolyline = (Polyline) mBaiduMap.addOverlay(ooPolyline11);

// 添加弧线
OverlayOptions ooArc = new ArcOptions().color(0xAA00FF00).width(4)
.points(p1, p2, p3);
mBaiduMap.addOverlay(ooArc);
// 添加圆
LatLng llCircle = new LatLng(39.90923, 116.447428);
OverlayOptions ooCircle = new CircleOptions().fillColor(0x000000FF)
.center(llCircle).stroke(new Stroke(5, 0xAA000000))
.radius(1400);
mBaiduMap.addOverlay(ooCircle);

LatLng llDot = new LatLng(39.98923, 116.397428);
OverlayOptions ooDot = new DotOptions().center(llDot).radius(6)
.color(0xFF0000FF);
mBaiduMap.addOverlay(ooDot);
// 添加多边形
LatLng pt1 = new LatLng(39.93923, 116.357428);
LatLng pt2 = new LatLng(39.91923, 116.327428);
LatLng pt3 = new LatLng(39.89923, 116.347428);
LatLng pt4 = new LatLng(39.89923, 116.367428);
LatLng pt5 = new LatLng(39.91923, 116.387428);
List<LatLng> pts = new ArrayList<LatLng>();
pts.add(pt1);
pts.add(pt2);
pts.add(pt3);
pts.add(pt4);
pts.add(pt5);
OverlayOptions ooPolygon = new PolygonOptions().points(pts)
.stroke(new Stroke(5, 0xAA00FF00)).fillColor(0xAAFFFF00);
mBaiduMap.addOverlay(ooPolygon);
// 添加文字
LatLng llText = new LatLng(39.86923, 116.397428);
OverlayOptions ooText = new TextOptions().bgColor(0xAAFFFF00)
.fontSize(24).fontColor(0xFFFF00FF).text("百度地图SDK").rotate(-30)
.position(llText);
mBaiduMap.addOverlay(ooText);
// 添加矩形区域
LatLng southwest = new LatLng(39.92235, 116.380338);
LatLng northeast = new LatLng(39.947246, 116.414977);
LatLngBounds bounds = new LatLngBounds.Builder().include(northeast)
.include(southwest).build();

OverlayOptions ooGround = new GroundOverlayOptions()
.positionFromBounds(bounds).image(bdGround).transparency(0.8f);
mBaiduMap.addOverlay(ooGround);
//添加marker
LatLng llC = new LatLng(39.939723, 116.425541);
MarkerOptions ooC = new MarkerOptions().position(llC).icon(bdC)
.perspective(false).anchor(0.5f, 0.5f).rotate(30).zIndex(7);
//生长动画
ooC.animateType(MarkerAnimateType.grow);
mMarkerC = (Marker) (mBaiduMap.addOverlay(ooC));
}

POI 检索

百度给的教程:

第一步,创建 POI 检索实例

 mPoiSearch = PoiSearch.newInstance();

第二步,创建 POI 检索监听者;

1
2
3
4
5
6
7
8
 OnGetPoiSearchResultListener poiListener = new OnGetPoiSearchResultListener(){
public void onGetPoiResult(PoiResult result){
//获取POI检索结果,可以用添加多个overlay的方式,在地图上显示
}
public void onGetPoiDetailResult(PoiDetailResult result){
//获取Place详情页检索结果
}
};

第三步,设置 POI 检索监听者;

1
2
3
4
5
6
7
8
9
10
11
12
13
14
     mPoiSearch.setOnGetPoiSearchResultListener(poiListener);
~~~
第四步,发起检索请求;
~~~java
mPoiSearch.searchInCity((new PoiCitySearchOption())
.city(“北京”)
.keyword(“美食”)
.pageNum(10));
~~~
第五步,释放POI检索实例;
~~~java
mPoiSearch.destroy();
~~~