(Android)呼叫百度地圖api之新增覆蓋物
阿新 • • 發佈:2019-02-18
一、自定義類Info
public class Info implements Serializable { private double latitude; private double longitude; private int imgId; private String name; public static List<Info> infos = new ArrayList<Info>(); //新增兩個靜態地圖覆蓋物 static { infos.add(new Info(32.120072, 118.922755, R.drawable.bookbox, "box1202")); infos.add(new Info(32.120267, 118.920518, R.drawable.bookbox, "box1201")); } public Info(double latitude, double longitude, int imgId, String name) { this.latitude = latitude; this.longitude = longitude; this.imgId = imgId; //點選覆蓋物,顯示圖片this.name = name; //點選覆蓋物,顯示文字 } public double getLatitude() { return latitude; } public void setLatitude(double latitude) { this.latitude = latitude; } public double getLongitude() { return longitude; } public void setLongitude(double longitude) { this.longitude = longitude; } public int getImgId() { return imgId; } public void setImgId(int imgId) { this.imgId = imgId; } public String getName() { return name; } public void setName(String name) { this.name = name; } }
二、核心程式碼
- 宣告所需變數
// 覆蓋物相關
private BitmapDescriptor mMarker;
public RelativeLayout mMarkerLy;
- 核心程式碼
private void initMarker() //初始化 { mMarker = BitmapDescriptorFactory.fromResource(R.drawable.bookbox); //設定地圖覆蓋物圖示 mMarkerLy = findViewById(R.id.id_maker_ly); //佈局檔案中所對應的RelativeLayout }
// 新增覆蓋物private void addOverlays(List<Info> infos) { mBaiduMap.clear(); LatLng latLng = null; Marker marker = null; OverlayOptions options; for (Info info : infos) { // 經緯度 latLng = new LatLng(info.getLatitude(), info.getLongitude()); // 圖示 options = new MarkerOptions().position(latLng).icon(mMarker) .zIndex(5); marker = (Marker) mBaiduMap.addOverlay(options); Bundle arg0 = new Bundle(); arg0.putSerializable("info", info); marker.setExtraInfo(arg0); } MapStatusUpdate msu = MapStatusUpdateFactory.newLatLng(latLng); mBaiduMap.setMapStatus(msu); }
mBaiduMap.setOnMarkerClickListener(new BaiduMap.OnMarkerClickListener() //設定監聽 { @Override public boolean onMarkerClick(Marker marker) { Bundle extraInfo = marker.getExtraInfo(); Info info = (Info) extraInfo.getSerializable("info"); TextView name = mMarkerLy.findViewById(R.id.id_info_name); boxname=info.getName(); //從info類中獲取資料 /*connect();*/ name.setText(info.getName()); mMarkerLy.setVisibility(View.VISIBLE); //佈局檔案中的RelativeLayout設定為可見 return true; } }); mBaiduMap.setOnMapClickListener(new BaiduMap.OnMapClickListener() { @Override public boolean onMapPoiClick(MapPoi arg0) { return false; } @Override public void onMapClick(LatLng arg0) { mMarkerLy.setVisibility(View.GONE); mBaiduMap.hideInfoWindow(); /*mConnect.disconnect();*/ /*adapter=null;*/ } });
三、其它
<RelativeLayout android:id="@+id/id_maker_ly" android:layout_width="match_parent" android:layout_height="240dp" android:layout_alignParentBottom="true" android:background="#cc4e5a6b" android:clickable="true" android:visibility="gone"> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <TextView android:id="@+id/id_info_name" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="書箱1201" android:textColor="#fff5eb" /> <android.support.v7.widget.RecyclerView android:id="@+id/recycler_view6" android:layout_width="match_parent" android:layout_height="match_parent"> </android.support.v7.widget.RecyclerView> </LinearLayout> </RelativeLayout>