点击百度地图marker跳出弹出框,若点重合弹出框里可左右滑动切换

被这个问题困扰了好几天,其实后来发现很简单,主要还是思路不对。首先,弹出框里的信息展示必然是ViewPager里塞入View,如果只是一个点的话很简单直接setAdapter,如果这个位置的点重合,只需要往ViewPagerAdapter里动态添加View就可以了。一开始的想法方向错了,一直想着点击一个marker跳出一个dialog,怎么会两个dialog合并到一起.....

注:在DialogFragment产生之前,我们创建对话框:一般采用AlertDialog和Dialog。注:官方不推荐直接使用Dialog创建对话框。DialogFragment里塞入的是Fragment,AlertDialog塞入的是View布局。(Activity继承FragmentActivity才有getSupportFragmentManager方法)
Android 官方推荐 : DialogFragment 创建对话框

ViewPagerAdapter

package com.example.hasee.baidutest.newtest.suzhou;

import android.support.v4.view.PagerAdapter;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewParent;

import java.util.ArrayList;
import java.util.List;

/**
 * 通用ViewPager适配器
 *
 * @author Liao_Yang
 * @Description
 * @data2015年12月30日 下午3:55:00
 */
public class ViewPagerAdapter extends PagerAdapter {
    private List views = new ArrayList();

    public ViewPagerAdapter() {
    }

    // 动态往适配器里添加Views
    public void setViews(List views) {
        this.views = views;
    }

    @Override
    public int getCount() {
        return views.size();
    }

    @Override
    public boolean isViewFromObject(View v, Object obj) {
        return v == obj;
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        View view = views.get(position);
        ViewParent parent = view.getParent();
        if (null != parent) {
            ((ViewGroup) parent).removeView(view);
        }
        container.addView(view);
        return view;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        container.removeView(views.get(position));
    }
}

PixelInfoDialog主要代码

private void initData() {
        mAdapter = new ViewPagerAdapter();
        mViews = new ArrayList();
        int size = 0;
        if (null != mItems && (size = mItems.size()) > 0) {
            for (int i = 0; i < size; i++) {
                View convertView = mInflater.inflate(R.layout.pixel_info_list,
                        null);
                ListView listView = (ListView) convertView
                        .findViewById(R.id.list_view);
                Object item = mItems.get(i);
                List desc = null;

                mViews.add(convertView);
            }
        }
        mAdapter.setViews(mViews);
        mViewpager.setAdapter(mAdapter);

        onPageSelected(0);
    }

MainActivity中主要代码

BaiduMap.OnMapStatusChangeListener listener = new BaiduMap.OnMapStatusChangeListener() {
        /**
         * 手势操作地图,设置地图状态等操作导致地图状态开始改变。
         * @param status 地图状态改变开始时的地图状态
         */
        public void onMapStatusChangeStart(MapStatus status) {
        }

        /**
         * 地图状态变化中
         * @param status 当前地图状态
         */
        public void onMapStatusChange(MapStatus status) {
        }

        /**
         * 地图状态改变结束
         */
        @Override
        public void onMapStatusChangeFinish(MapStatus mapStatus) {
            //左上角经纬度
            Point pt = new Point();
            pt.x = 0;
            pt.y = 0;
            LatLng ll = mBaiduMap.getProjection().fromScreenLocation(pt);
            //右下角经纬度
            DisplayMetrics dm = new DisplayMetrics();
            getWindowManager().getDefaultDisplay().getMetrics(dm);
            Point pty = new Point();
            pty.x = dm.widthPixels;
            pty.y = dm.heightPixels;
            LatLng lly = mBaiduMap.getProjection().fromScreenLocation(pty);

            //这里能正确显示坐标
            LatLng[] pts = MapUtils.getMapLTandRB(mBaiduMap);
//            pts[0] = MapUtils.fromBaiduToGPS(pts[0]); //gps坐标
//            pts[1] = MapUtils.fromBaiduToGPS(pts[1]); //gps坐标
            Log.e("MainActivity", "左上角坐标:" + pts[0]+"\n右下角坐标:"+pts[1]);

            pointByWindow = DrawPointByWindow.getInstance(BaiduMapUse.getInstance(MainActivity.this
                    , mBaiduMap), mBaiduMap);
            pointByWindow.getPoint(pts);


            double latitude = 31.99601;
            double longitude = 118.738368;
            LatLng latLng = new LatLng(latitude, longitude);
            BitmapDescriptor descriptor = BitmapDescriptorFactory.fromResource(R.mipmap.ic_launcher);
            OverlayOptions options = new MarkerOptions().position(latLng).icon(descriptor);
            Marker marker = (Marker) mBaiduMap.addOverlay(options);

            mBaiduMap.setOnMarkerClickListener(new BaiduMap.OnMarkerClickListener() {
                @Override
                public boolean onMarkerClick(Marker marker) {
                    Bundle b = MapUtils.getDataWithOverlay(marker);
                    if (null != b) {
                        Serializable data = b.getSerializable("data");

                        BasePointInfo basePointInfo = (BasePointInfo) data;
                        String layerId = basePointInfo.getLayerId();
                        //查询已勾选图层下对应坐标的基站
                        List pois = new ArrayList<>();
                        pois.add(basePointInfo);
                        pois.add(basePointInfo);

                        PixelInfoDialog dialog = new PixelInfoDialog(mContext, pois, mDialogUtil,mBaiduMap);
                        dialog.show();

//                        if (layerId.equals("layerId")) {  //不同图层的点(每个图层的点信息结构应该相同)
//                            map = new HashMap<>();
//                            map.put("第一", new ElementaryFragment());
//                            map.put("第二", new MapFragment());
//                            map.put("第三", new ZipFragment());
//                            DialogFragmentWindow_Modity dialog = new DialogFragmentWindow_Modity(map,pois,basePointInfo);
//                            dialog.show(getSupportFragmentManager(), "");
//
//                        }else{                           //另一个图层的点
//                            map = new HashMap<>();
//                            map.put("第一", new MapFragment());
//                            DialogFragmentWindow_Modity dialog = new DialogFragmentWindow_Modity(map,pois,basePointInfo);
//                            dialog.show(getSupportFragmentManager(), "");
//                        }

                    }
                    return true;
                }
            });

        }
    };

效果图显示

点击百度地图marker跳出弹出框,若点重合弹出框里可左右滑动切换_第1张图片
TIM图片20170623180850.jpg

点击百度地图marker跳出弹出框,若点重合弹出框里可左右滑动切换_第2张图片
TIM图片20170623180843.jpg

你可能感兴趣的:(点击百度地图marker跳出弹出框,若点重合弹出框里可左右滑动切换)