Android 程序开发——百度地图的使用(四) 添加有实体类的marker 标记图层

现实开发中,我们需要添加标注物的时候,总会带有各种信息,为了方便点击标记物的时候,显示相应的信息。

(我曾经做过:利用marker点击事件里边的经纬度 和服务器依次遍历获取到相应的值。这种做法简直愚蠢,一来没有用到面线对象,二来经纬度不可能那么精确到小数点n位)

下面便是介绍 如何添加   带有实体类的marker标记图层,以及点击后的显示




第一步:实体类

MarkerInfo.java(里面包含了marker  携带的各种信息)

package com.sw.jiuawan.suiwan.bean;

import com.baidu.mapapi.model.LatLng;

import java.io.Serializable;
import java.util.List;

/**
 * Created by treasure on 2017.02.11.
 */

public class MarkerInfo implements Serializable {
    private double lat, lon;
    private LatLng mLatLng;
    private MarkerInfo.Map_Active_Data mMapActiveData;

    public MarkerInfo() {
    }

    public MarkerInfo(double lat, double lon, MarkerInfo.Map_Active_Data mMapActiveData) {
        this.lat = lat;
        this.lon = lon;
        this.mMapActiveData = mMapActiveData;
        mLatLng = new LatLng(lat, lon);
    }

    public double getLat() {
        return lat;
    }

    public void setLat(double lat) {
        this.lat = lat;
    }

    public double getLon() {
        return lon;
    }

    public void setLon(double lon) {
        this.lon = lon;
    }

    public LatLng getLatLng() {
        return mLatLng;
    }

    public void setLatLng(LatLng latLng) {
        mLatLng = latLng;
    }

    public MarkerInfo.Map_Active_Data getMapActiveData() {
        return mMapActiveData;
    }

    public void setMapActiveData(MarkerInfo.Map_Active_Data mapActiveData) {
        mMapActiveData = mapActiveData;
    }

    public class Map_Active_Data implements Serializable {
        private String active_id;
        private String position;
        private String active_name;
        private String active_desc;
        private String city;
        private String area;
        private String date;
        private String price;
        //0 已购买, 1未购买
        private int status;
        private String imageurl;
        private String activeaddr;
        private List<UserMapResultBean.MapDataInfo.Map_Active_Data.StartPositionArray> start_position_array;
        private int type;// 0,室外  1,室内

        public String getActiveid() {
            return active_id;
        }

        public void setActiveid(String active_id) {
            this.active_id = active_id;
        }

        public String getPosition() {
            return position;
        }

        public void setPosition(String position) {
            this.position = position;
        }

        public String getActive_name() {
            return active_name;
        }

        public void setActive_name(String active_name) {
            this.active_name = active_name;
        }

        public String getCity() {
            return city;
        }

        public void setCity(String city) {
            this.city = city;
        }

        public String getArea() {
            return area;
        }

        public void setArea(String area) {
            this.area = area;
        }

        public String getDate() {
            return date;
        }

        public void setDate(String date) {
            this.date = date;
        }

        public String getPrice() {
            return price;
        }

        public void setPrice(String price) {
            this.price = price;
        }

        public int getStatus() {
            return status;
        }

        public void setStatus(int status) {
            this.status = status;
        }

        public String getActive_id() {
            return active_id;
        }

        public void setActive_id(String active_id) {
            this.active_id = active_id;
        }

        public String getImageurl() {
            return imageurl;
        }

        public void setImageurl(String imageurl) {
            this.imageurl = imageurl;
        }

        public String getActive_desc() {
            return active_desc;
        }

        public void setActive_desc(String active_desc) {
            this.active_desc = active_desc;
        }

        public List<UserMapResultBean.MapDataInfo.Map_Active_Data.StartPositionArray> getStart_position_array() {
            return start_position_array;
        }

        public void setStart_position_array(List<UserMapResultBean.MapDataInfo.Map_Active_Data.StartPositionArray> start_position_array) {
            this.start_position_array = start_position_array;
        }

        public String getActiveaddr() {
            return activeaddr;
        }

        public void setActiveaddr(String activeaddr) {
            this.activeaddr = activeaddr;
        }

        public int getType() {
            return type;
        }

        public void setType(int type) {
            this.type = type;
        }

        public class StartPositionArray implements Serializable {
            private float position_x;
            private float position_y;

            public float getPosition_x() {
                return position_x;
            }

            public void setPosition_x(float position_x) {
                this.position_x = position_x;
            }

            public float getPosition_y() {
                return position_y;
            }

            public void setPosition_y(float position_y) {
                this.position_y = position_y;
            }
        }
    }
}

第二部:添加Marker标记(请求服务器,从服务器拿到信息,然后依次添加 marker标记图层)

private void requestActivePosition() {
        GameRequestBean gameRequestBean = new GameRequestBean();
        gameRequestBean.setAccount_id(account_id);
        gameRequestBean.setToken(token);
        gameRequestBean.setCity("010");
        gameRequestBean.setArea("");
        HttpHelper.doJsonPostCall(StringContents.DXYYACTIVELIST, gameRequestBean, this, new StringCallback() {
            @Override
            public void onError(Call call, Exception e, int id) {
                LogUtil.d("~~~~~~~~~~~~hello", "Exception:" + e.getMessage());
            }

            @Override
            public void onResponse(String response, int id) {
                LogUtil.d("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~response:", response);
                UserMapResultBean mapResultBean = ModelParseHelper.parseGameDataListBean(response);
                int code = mapResultBean.getCode();
                if (code == 0) {
                    int total_count = mapResultBean.getData().getTotal_count();

                    final List<UserMapResultBean.MapDataInfo.Map_Active_Data> active_data = mapResultBean.getData().getActive_data();
                    for (int i = 0; i < active_data.size(); i++) {
                        final UserMapResultBean.MapDataInfo.Map_Active_Data map_active_data = active_data.get(i);
                        String position = map_active_data.getPosition();
                        String[] split = position.split(",");
                        destination_latitude = Double.parseDouble(split[1]);
                        destination_longitude = Double.parseDouble(split[0]);
                        MarkerInfo markerInfo = new MarkerInfo(destination_latitude, destination_longitude, map_active_data);

                        //定义Maker坐标点
                        try {
                            addOverlay(markerInfo);
                        } catch (Exception e) {
                            Toast.makeText(UserMapActivity.this, "请重新登陆", Toast.LENGTH_SHORT).show();
                        }
                        
                    }

                } else {
                    Toast.makeText(UserMapActivity.this, "请先登录,谢谢", Toast.LENGTH_SHORT).show();
                }
            }
        });
    }

BitmapDescriptor bitmap_1 = BitmapDescriptorFactory.fromResource(R.mipmap.pic_chicken);


//添加标注
    public void addOverlay(MarkerInfo Info) {
        OverlayOptions overlayoptions = null;
        Marker marker = null;
                overlayoptions = new MarkerOptions()//
                        .position(Info.getLatLng())// 设置marker的位置
                        .icon(bitmap_1)// 设置marker的图标
                        .animateType(MarkerOptions.MarkerAnimateType.drop)
                        .zIndex(9);// 設置marker的所在層級


        marker = (Marker) map.addOverlay(overlayoptions);


        Bundle bundle = new Bundle();
        bundle.putSerializable("marker", Info);
        marker.setExtraInfo(bundle);
    }




第三步:点击marker  显示 具体的内容

注:1.marker的监听事件 放在onCreate()里就可以了 ,不要放在接收服务器的的函数里,否则点击多次相当于设置多次监听事件,再点一次的话就会很乱的

2.点击后可以放在主线程也可以放在子线程,反正地图定位的回调 要发个handler   毕竟更新view   我感觉地图定位回调的函数应该运行在子线程上,我试过直接更新view  有时候显示不出来,索性直接发个handler  毕竟官方也是这么做的

    //设置marker的点击事件
    private void setMarkerClickListener() {
        //Mark点击事件,,
        map.setOnMarkerClickListener(new BaiduMap.OnMarkerClickListener() {

            @Override
            public boolean onMarkerClick(Marker marker) {
                final MarkerInfo Info = (MarkerInfo) marker.getExtraInfo().get("marker");
//                            创建InfoWindow展示的view
                Message message = new Message();
                Bundle bundle = new Bundle();
                bundle.putSerializable("info", Info);
                message.setData(bundle);
                message.what = 211;
                mHandler.sendMessage(message);
                return false;
            }
        });
    }
这样的话你就可以获取你想要的数据了
case 211:
                    final MarkerInfo Info = (MarkerInfo) msg.getData().getSerializable("info");
                    LogUtil.d("~~~~~~~~~~~info:::", Info.getLat() + "");
                    markerStub.setVisibility(View.VISIBLE);
                    imageurl = StringContents.BASEURL + Info.getMapActiveData().getImageurl();

                    String active_desc = Info.getMapActiveData().getActive_desc();
                    active_desc = active_desc.replaceAll("\\[\\\\n\\]", "\n");
                    new Thread(new Runnable() {
                        @Override
                        public void run() {
                            if (!isPageDestroy) {
                                Message message = new Message();
                                message.what = 200;
                                mHandler.sendMessage(message);
                            }
                        }
                    }).start();
                    ac_title.setText(Info.getMapActiveData().getActive_name());
                    ac_location.setText(Info.getMapActiveData().getActiveaddr());
                    youhui.setText(Info.getMapActiveData().getPrice());
                    xuzhi.setText(active_desc);

                    status = Info.getMapActiveData().getStatus();
                    if (status == 0) {//未购买
                        ac_join.setText("参加");
                    } else if (status == 1) {
                        ac_join.setText("进入");
                    }
                    active_id = Info.getMapActiveData().getActiveid();
                    active_name = Info.getMapActiveData().getActive_name();
                    active_area = Info.getMapActiveData().getActiveaddr();
                    active_price = Info.getMapActiveData().getPrice();
                    active_stpos = Info.getMapActiveData().getStart_position_array();
                    type = Info.getMapActiveData().getType();
                    application.setActive_stpos(active_stpos);
                    break;




  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值