高德地图自定义Marker点击时出现的InfoWindow

来源:互联网 发布:tensorflow不好用 编辑:程序博客网 时间:2024/05/01 09:02

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();               }            }        }
1 0