1. 程式人生 > >Android 高德地圖新增Marker,Marker點選事件

Android 高德地圖新增Marker,Marker點選事件

新增圖片圖示

BitmapDescriptor bitmapDescriptor = BitmapDescriptorFactory.fromBitmap(BitmapFactory.decodeResource(getResources(), R.mipmap.qidian));
final Marker marker = aMap.addMarker(new MarkerOptions().position(latLng).icon(bitmapDescriptor));

 

1、新增Marker點選事件——根據markerID區分各個Marker的點選事件

mapView = (MapView) findViewById(R.id.map);
//在activity執行onCreate時執行mMapView.onCreate(savedInstanceState),實現地圖生命週期管理
mapView.onCreate(savedInstanceState);
if (aMap == null) {
    Log.i("lgq","sssssfa====aMap == null");
    aMap = mapView.getMap();
    //設定顯示定位按鈕 並且可以點選
    UiSettings settings = aMap.getUiSettings();
    aMap.setLocationSource(this);//設定了定位的監聽
    // 是否顯示定位按鈕
    settings.setMyLocationButtonEnabled(true);
    aMap.setMyLocationEnabled(true);//顯示定位層並且可以觸發定位,預設是flase


    aMap.setOnMarkerClickListener(new AMap.OnMarkerClickListener() {
        @Override
        public boolean onMarkerClick(Marker marker) {
            Log.i("lgq","dianjiddd===="+marker.getPeriod());//獲取markerID
            bottomli.setVisibility(View.VISIBLE);
            bottomte.setText("地址是:"+marker.getPeriod());
            return false;
        }
    });

2、給地圖新增自定義Marker

新增多個Marker

setMarker("大象網咖",23.025845,113.752532,1);
setMarker("小猴網咖",23.025845,113.772532,2);
setMarker("大英超網咖",23.024845,113.782532,3);
setMarker("京山市場網咖",23.086634,113.849915,4);
setMarker("東城億嘉網咖",23.036034,113.816161,5);
public void setMarker(String barname,double wd,double jd,int mid){
    LayoutInflater mInflater = LayoutInflater.from(this);
    View view = mInflater.inflate(R.layout.zdyview, null);
    TextView textView = view.findViewById(R.id.tete);
    textView.setText(barname);

    Bitmap bitmap =convertViewToBitmap(view);

    //繪製marker
    Marker marker2 = aMap.addMarker(new MarkerOptions()
            .position(new LatLng(wd,jd)).period(mid)//新增markerID
            .icon(BitmapDescriptorFactory.fromBitmap(bitmap))
            .draggable(false));
}

 zdyview檔案

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    android:orientation="vertical">

    <TextView
        android:id="@+id/tete"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="13dp"
        android:textColor="@color/colorPrimary"
        android:background="@mipmap/custom_info_bubble"
        android:paddingRight="3dp"
        android:paddingLeft="3dp"
        android:textStyle="bold"
        android:text="速度"
       />

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@mipmap/dw64"/>

</LinearLayout>