android中使用百度地图绘制弹出框的覆盖物

本文介绍如何在项目中集成百度地图API实现类似美团的地图查看周边团购功能,包括创建地图覆盖物及复杂的弹出框布局实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

这几天在项目中引入了百度地图,实现的功能就是类似美团的地图查看周边团购那样的功能,实现的弹出框布局要比美团复杂一些。

下面直接上代码:

通过这个方法在指定的坐标创建一个覆盖物

mBaiduMap.setOnMarkerClickListener(new BaiduMap.OnMarkerClickListener() {
    @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
    @Override
    public boolean onMarkerClick(Marker marker) {
        LatLng latLng = marker.getPosition();
        showWindow(latLng.latitude,latLng.longitude);
        return true;
    }
});
然后通过showWindow方法实现弹出框
private void showWindow(double latitude,double longitude){
    try {
        for (int i = 0, n = list_data.size(); i < n; i++) {
            if (Double.parseDouble(list_data.get(i).get("lat").toString()) == latitude &&
                    Double.parseDouble(list_data.get(i).get("lng").toString()) == longitude) {
                NearListBean nearListBean=null;
                if(convertView ==null) {
                    convertView = LayoutInflater.from(getApplicationContext()).inflate(R.layout.near_map_item, null);
                    nearListBean = new NearListBean();
                    nearListBean.pic = (RoundRectImageView) convertView.findViewById(R.id.near_list_item_pic);
                    nearListBean.title = (TextView) convertView.findViewById(R.id.near_list_item_title);
                    nearListBean.time = (TextView) convertView.findViewById(R.id.near_list_item_time);
                    nearListBean.priceSymbol = (TextView) convertView.findViewById(R.id.near_list_item_price_symbol);
                    nearListBean.price = (TextView) convertView.findViewById(R.id.near_list_item_price);
                    nearListBean.endtime = (TextView) convertView.findViewById(R.id.near_list_item_endtime);
                    nearListBean.age = (TextView) convertView.findViewById(R.id.near_list_item_age);
                    nearListBean.distance = (TextView) convertView.findViewById(R.id.near_list_item_distance);
                    convertView.setTag(nearListBean);
                }else{
                    nearListBean= (NearListBean) convertView.getTag();
                }
                //homeListBean.img.setImageResource((Integer)mData.get(position).get("img"));
                AsynImageLoader asynImageLoader = new AsynImageLoader();
                asynImageLoader.showImageAsyn(nearListBean.pic, list_data.get(i).get("pic").toString(), R.drawable.message);
                //imageLoader.DisplayImage(list_data.get(i).get("pic").toString(), nearListBean.pic);
                nearListBean.title.setText(list_data.get(i).get("title").toString());
                nearListBean.time.setText(list_data.get(i).get("startTime").toString());
                String price= list_data.get(i).get("price").toString();
                if(price.indexOf("免费") ==-1) {
                    nearListBean.priceSymbol.setText("¥");
                    nearListBean.price.setText(price.substring(1, price.length()));
                }else{
                    nearListBean.priceSymbol.setText("");
                    nearListBean.price.setText(price);
                }
                nearListBean.distance.setText("<"+list_data.get(i).get("distance").toString());
                nearListBean.age.setText(list_data.get(i).get("age").toString());
                SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd HH:mm");
                nearListBean.endtime.setText(DateUtil.dateDiff(format.format(new Date()),list_data.get(i).get("applyend").toString(),"yyyy-MM-dd HH:mm"));
                //定义用于显示该InfoWindow的坐标点
                LatLng pt = new LatLng(latitude, longitude);
                //创建InfoWindow , 传入 view, 地理坐标, y 轴偏移量
                InfoWindow mInfoWindow = new InfoWindow(convertView, pt, -78);
                //显示InfoWindow
                mBaiduMap.showInfoWindow(mInfoWindow);
                index=i;
                convertView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Intent intent = new Intent(NearMapActivity.this, ActivityDetailActivity.class);

                        intent.putExtra("activityId",(int)Float.parseFloat(list_data.get(index).get("activityId").toString()));
                        startActivity(intent);
                    }
                });
                break;
            }

        }
    }catch (Exception e){
        e.printStackTrace();
    }
}
这里其实跟listview添加item基本一样的,这里的的布局文件的长宽写死了,不然会被压缩。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值