1. 程式人生 > >高德地圖自定義Marker點選時出現的InfoWindow

高德地圖自定義Marker點選時出現的InfoWindow

1.自定義InfoWindowAdapter:

package com.onetoo.www.onetoo.abapter.home;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.amap.api.maps2d.AMap;
import com.amap.api.maps2d.model.Marker
; import com.bumptech.glide.Glide; import com.onetoo.www.onetoo.R; import com.onetoo.www.onetoo.bean.home.HomeStore; import com.onetoo.www.onetoo.utils.StoreInfoUtil; /** * Created by longShun on 2016/11/3. * desc自定義地圖資訊框 */ public class CustomInfoWindowAdapter implements AMap.InfoWindowAdapter{ private Context context;
public CustomInfoWindowAdapter(Context context) { this.context = context; } @Override public View getInfoWindow(Marker marker) { View view = LayoutInflater.from(context).inflate(R.layout.map_info_window, null); setViewContent(marker,view); return view;
} //這個方法根據自己的實體資訊來進行相應控制元件的賦值 private void setViewContent(Marker marker,View view) { //例項: HomeStore.DataEntity storeInfo = (HomeStore.DataEntity) marker.getObject(); ImageView ivPic = (ImageView) view.findViewById(R.id.iv_info_store_pic); Glide.with(context).load(storeInfo.getStore_pic()).centerCrop().into(ivPic); TextView tvName = (TextView) view.findViewById(R.id.tv_info_store_name); tvName.setText(storeInfo.getStore_name()); TextView tvType = (TextView) view.findViewById(R.id.tv_info_store_type); String storeCategory = StoreInfoUtil.getStoreCategory(storeInfo.getFk_category_id()); tvType.setText(storeCategory); } //提供了一個給預設資訊視窗定製內容的方法。如果用自定義的佈局,不用管這個方法。 @Override public View getInfoContents(Marker marker) { return null; } }

2.拿到AMap物件,設定adapter:

AMap aMap = mapView.getMap();
aMap.setInfoWindowAdapter(new CustomInfoWindowAdapter(getActivity()));

3.Activity或則其他地方關鍵程式碼:


        MarkerOptions options = new MarkerOptions();
                options.title("");//title不設infowindow不顯示
                options.position(new LatLng(latitude, longitude)).icon(BitmapDescriptorFactory
                        .fromBitmap(BitmapFactory
                                .decodeResource(getResources(), R.drawable.xxx)));

        Marker marker = aMap.addMarker(options);
        marker.setObject(object);//把相應的物件賦給marker,adapter中通過這個物件給控制元件賦值

        aMap.setOnMarkerClickListener(this);
        @Override
        public boolean onMarkerClick(Marker marker) {
            curShowWindowMarker = marker;//儲存當前點選的Marker,以便點選地圖其他地方設定InfoWindow消失
            return false;//返回true,消費此事件。
        }

        aMap.setOnInfoWindowClickListener(this);
        @Override
        public void onInfoWindowClick(Marker marker) {
            // TODO: 2016/11/3
        }

        aMap.setOnMapTouchListener(this);
        @Override
        public void onTouch(MotionEvent motionEvent) {
             if (aMap != null && curShowWindowMarker != null) {
                    if (curShowWindowMarker.isInfoWindowShown()){
                        curShowWindowMarker.hideInfoWindow();
               }
            }
        }