2019獨角獸企業重金招聘Python工程師標準>>>
前言
上一篇講解并實踐了百度地圖基本顯示,地圖類型,實時交通圖,熱力圖,地圖控制和手勢控制,今天看下在地圖上添加view和覆蓋物。
地圖Logo不允許遮擋,可通過mBaiduMap.setPadding(paddingLeft, paddingTop, paddingRight, paddingBottom);方法可以設置地圖邊界區域,來避免UI遮擋。?
其中參數paddingLeft、paddingTop、paddingRight、paddingBottom參數表示距離屏幕邊框的左、上、右、下邊距的距離,單位為屏幕坐標的像素密度。
—————–>利用這個來添加自定義的內容
內容
地圖上添加View?
由于地圖的Logo不允許被遮擋,百度自然會想辦法針對一些開發者場景作出自己的調整,比如上面所說的setPadding()或者setViewPadding()一樣好使,具體的使用分為兩步:?
第一步,
mBaiduMap.setPadding(0, 0, 0, 200);
第二步,
private void addView() {TextView textView = new TextView(this);textView.setText("這是用戶自定義的View,這個時候logo和底部的一些內容會向上移動,因為MapView設置了底部Padding");textView.setBackgroundResource(R.color.colorPrimary); //創建一個TextView然后往底部放置,官方Demo如是操作MapViewLayoutParams.Builder builder = new MapViewLayoutParams.Builder();builder.layoutMode(MapViewLayoutParams.ELayoutMode.absoluteMode);builder.width(bmapView.getWidth());builder.height(200);builder.point(new Point(0, bmapView.getHeight()));builder.align(MapViewLayoutParams.ALIGN_LEFT, MapViewLayoutParams.ALIGN_BOTTOM);bmapView.addView(textView, builder.build());}
這樣子就在底部添加了一個TextView,當然想添加其他的View自然也是可行的,具體效果圖往下看。
地理編碼,標注覆蓋物和彈出窗覆蓋物?
利用標注覆蓋物在地圖上的特定位置添加標注,然后給標注覆蓋物設置點擊監聽展示彈出覆蓋物
//這個是“打點”按鈕的點擊事件,點擊事件發生后,地理編碼對象執行Address->location的解析,解析結果返回之后,拿到對應的點的經緯度,也就是LatLng實例和對應的地址信息,然后執行addMarker操作
private void addPointByEditText() {String placename = etPoint.getText().toString();final GeoCoder geo = GeoCoder.newInstance();geo.setOnGetGeoCodeResultListener(new OnGetGeoCoderResultListener() {@Overridepublic void onGetGeoCodeResult(GeoCodeResult geoCodeResult) {if (geoCodeResult.getLocation() != null) {addMarker(geoCodeResult.getLocation() , geoCodeResult.getAddress());//addText(geoCodeResult.getLocation(), "看過來 :" + geoCodeResult.getAddress());//這個是后面添加文字覆蓋物的時候使用到的} else {Toast.makeText(MainActivity.this, " No Result of GeoCoder! Sorry", Toast.LENGTH_SHORT).show();}}@Overridepublic void onGetReverseGeoCodeResult(ReverseGeoCodeResult reverseGeoCodeResult) {}});geo.geocode(new GeoCodeOption().address(placename).city(placename));
}
......//添加標注物
private void addMarker(LatLng point, String address) {//定義Maker坐標點//LatLng point = new LatLng(39.963175, 116.400244);//構建MarkerOption,用于在地圖上添加MarkerBundle bundle = new Bundle();bundle.putParcelable("LATLNG", point);//傳遞Bundle對象,方便后面讀取對應的info來創建pop windowbundle.putString("ADDRESS", address);OverlayOptions option = new MarkerOptions().position(point).icon(bitmap).extraInfo(bundle);//在地圖上添加Marker,并顯示mBaiduMap.addOverlay(option);
}//設置標注覆蓋物的點擊事件監聽
mBaiduMap.setOnMarkerClickListener(new BaiduMap.OnMarkerClickListener() {@Overridepublic boolean onMarkerClick(Marker marker) {//這里是MarkerOption創建的時候傳入的點坐標和對應的地址信息if (marker.getExtraInfo() != null) {LatLng point = marker.getExtraInfo().getParcelable("LATLNG");String address = marker.getExtraInfo().getString("ADDRESS");addPop(point, address);}return false;}
});
......//添加彈出窗覆蓋物
private void addPop(LatLng point, String message) {//創建InfoWindow展示的viewButton button = new Button(getApplicationContext());button.setBackgroundResource(R.drawable.marker_info_bg);button.setTextColor(Color.WHITE);button.setText(message);//創建InfoWindow , 傳入 view, 地理坐標, y 軸偏移量InfoWindow mInfoWindow = new InfoWindow(button, point, -47);//顯示InfoWindowmBaiduMap.showInfoWindow(mInfoWindow);
}
- 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
- 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
實際效果圖:?
文字覆蓋物?
文字覆蓋物主要就是在地圖上添加一些字串, 用上面的代碼,將標注覆蓋物改成文字覆蓋物
//設置文字覆蓋物private void addText(LatLng point, String message) {//構建文字Option對象,用于在地圖上添加文字OverlayOptions textOption = new TextOptions().bgColor(0xAAFFFF00).fontSize(35).fontColor(0xFFFF00FF).text(message).rotate(-30).position(point);//在地圖上添加該文字對象并顯示mBaiduMap.addOverlay(textOption);}
實際效果圖:?
行政區邊界搜索和多邊形覆蓋物?
利用DistrictSearch和PolyOverlay,實現圈出行政區域的效果
mDistrictSearch = DistrictSearch.newInstance();//創建行政區域查詢的實例
......//添加多邊形區域
private void addPoly(String cityname) {//建立搜索條件DistrictSearchOption option = new DistrictSearchOption().cityName(cityname).districtName(cityname);//設置搜索監聽事件mDistrictSearch.setOnDistrictSearchListener(new OnGetDistricSearchResultListener() {@Overridepublic void onGetDistrictResult(DistrictResult districtResult) {if (districtResult.error == SearchResult.ERRORNO.NO_ERROR) {List<List<LatLng>> pointsList = districtResult.getPolylines();if (pointsList == null) return;//地理邊界對象LatLngBounds.Builder builder = new LatLngBounds.Builder();for (List<LatLng> polyline : pointsList) {OverlayOptions ooPolyline11 = new PolylineOptions().width(10).points(polyline).dottedLine(true).color(Color.RED);mBaiduMap.addOverlay(ooPolyline11);//添加OverLayOverlayOptions ooPolygon = new PolygonOptions().points(polyline).stroke(new Stroke(5, 0xAA00FF88)).fillColor(0xAAFFFF00);mBaiduMap.addOverlay(ooPolygon);//添加OverLayfor (LatLng latLng : polyline) {builder.include(latLng);//包含這些點}}}}});mDistrictSearch.searchDistrict(option);//執行行政區域的搜索
}
- 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
- 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
實際效果圖:?
轉載于:https://my.oschina.net/u/1177694/blog/997633
總結
以上是生活随笔為你收集整理的Android 应用 之路 百度地图API使用(3)的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。