Android集成百度地图点击Mark弹出PopWindow

项目中集成了百度地图,点击Mark弹出PopWindow,效果图如下:


代码如下:

BdMapFragment.java

package com.wdkj.housemanager.fragment;

import android.Manifest;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.ActivityCompat;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.baidu.location.BDLocation;
import com.baidu.location.BDLocationListener;
import com.baidu.location.LocationClient;
import com.baidu.location.LocationClientOption;
import com.baidu.mapapi.map.BaiduMap;
import com.baidu.mapapi.map.BitmapDescriptor;
import com.baidu.mapapi.map.BitmapDescriptorFactory;
import com.baidu.mapapi.map.InfoWindow;
import com.baidu.mapapi.map.LogoPosition;
import com.baidu.mapapi.map.MapPoi;
import com.baidu.mapapi.map.MapStatus;
import com.baidu.mapapi.map.MapStatusUpdateFactory;
import com.baidu.mapapi.map.MapView;
import com.baidu.mapapi.map.Marker;
import com.baidu.mapapi.map.MarkerOptions;
import com.baidu.mapapi.map.MyLocationData;
import com.baidu.mapapi.map.Overlay;
import com.baidu.mapapi.map.OverlayOptions;
import com.baidu.mapapi.map.Polyline;
import com.baidu.mapapi.map.PolylineOptions;
import com.baidu.mapapi.model.LatLng;
import com.wdkj.housemanager.R;
import com.wdkj.housemanager.activity.AddpointActivity;
import com.wdkj.housemanager.activity.CreatetaskActivity;
import com.wdkj.housemanager.activity.DisposeActivity;
import com.wdkj.housemanager.activity.HousedetailActivity;
import com.wdkj.housemanager.activity.HousejdActivity;
import com.wdkj.housemanager.activity.HuinfoActivity;
import com.wdkj.housemanager.activity.LoginActivity;
import com.wdkj.housemanager.activity.PatrolRecordActivity;
import com.wdkj.housemanager.activity.UpdateHouseActivity;
import com.wdkj.housemanager.api.AddhouseApi;
import com.wdkj.housemanager.api.AddressApi;
import com.wdkj.housemanager.api.HouseconditionApi;
import com.wdkj.housemanager.api.MarkApi;
import com.wdkj.housemanager.api.PolygonApi;
import com.wdkj.housemanager.api.VillageApi;
import com.wdkj.housemanager.bean.BaseBean;
import com.wdkj.housemanager.bean.Constant;
import com.wdkj.housemanager.bean.HousemarkBean;
import com.wdkj.housemanager.bean.JsonResult;
import com.wdkj.housemanager.bean.RegionPoints;
import com.wdkj.housemanager.bean.RegionpointBean;
import com.wdkj.housemanager.bean.SuccessdEvent;
import com.wdkj.housemanager.bean.VillageBean;
import com.wdkj.housemanager.bean.position;
import com.wdkj.housemanager.util.PointUtils;
import com.wdkj.housemanager.util.PositionUtil;
import com.wdkj.housemanager.util.ProgressDialog;
import com.wdkj.housemanager.util.RetrofitManager;
import com.wdkj.housemanager.util.SPUtils;
import com.wdkj.housemanager.view.Cluster;
import com.wdkj.housemanager.view.ClusterItem;
import com.wdkj.housemanager.view.ClusterManager;
import com.wdkj.housemanager.view.PolyOverlyInfo;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class BdMapFragment extends Fragment {

    @BindView(R.id.iv_addhouse)
    ImageView ivAddhouse;
    @BindView(R.id.ll_addhouse)
    LinearLayout llAddhouse;
    @BindView(R.id.iv_position)
    ImageView ivPosition;
    @BindView(R.id.et_search)
    EditText etSearch;
    @BindView(R.id.ll_search)
    LinearLayout llSearch;
    @BindView(R.id.start)
    Button start;
    @BindView(R.id.end)
    Button end;
    @BindView(R.id.route)
    Button route;
    @BindView(R.id.reset)
    Button reset;
    @BindView(R.id.ll_guidelayout)
    LinearLayout llGuidelayout;
    private boolean addHouse = false;
    private LocationClient mLocationClient;
    public BDLocationListener myListener;
    private ArrayList<PolyOverlyInfo> polyInfoList = new ArrayList<>();

    private View view;
    public static MapView mMapView = null;
    private List<HousemarkBean> houseMarkList;
    private static final String TAG = "BdMapFragment";
    private boolean isFrist = true;
    private BaiduMap mBaiduMap;
    private Polyline mPolyline;
    private double mCurrentLat = 0.0;
    private double mCurrentLon = 0.0;
    private int mCurrentDirection = 0;
    private float mCurrentAccracy;
    boolean isFirstLoc = true; // 是否首次定位
    private MyLocationData locData;
    private InfoWindow mInfoWindow;
    private LinearLayout baidumap_infowindow;
    private Marker marker,housemarker;
    private BitmapDescriptor bitmapred, bitmapblue;
    private static final int BAIDU_READ_PHONE_STATE =100;
    private ClusterManager<MyItem> mClusterManager;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        //在使用SDK各组件之前初始化context信息,传入ApplicationContext
        //注意该方法要再setContentView方法之前实现
        view = LayoutInflater.from(getActivity()).inflate(R.layout.bdfragment_map, null);
        baidumap_infowindow = (LinearLayout) LayoutInflater.from(getActivity()).inflate(R.layout.baidumap_infowindow, null);
        ButterKnife.bind(this, view);
        mMapView = (MapView) view.findViewById(R.id.mapview);
        bitmapred = BitmapDescriptorFactory.fromResource(R.drawable.position);
        mMapView.setLogoPosition(LogoPosition.logoPostionleftTop);
        mMapView.showZoomControls(false);
        mBaiduMap = mMapView.getMap();
        //普通地图
        mBaiduMap.setMapType(BaiduMap.MAP_TYPE_NORMAL);
        // 定义点聚合管理类ClusterManager
        mClusterManager = new ClusterManager<MyItem>(getActivity(), mBaiduMap);
        
        mBaiduMap.setOnMapStatusChangeListener(mClusterManager);
        // 设置maker点击时的响应
        mBaiduMap.setOnMarkerClickListener(mClusterManager);

        mClusterManager.setOnClusterItemClickListener(new ClusterManager.OnClusterItemClickListener<MyItem>() {
            @Override
            public boolean onClusterItemClick(MyItem item) {//item数据
               HousemarkBean houseInfo = item.mhousemarkBean;
                createInfoWindow(baidumap_infowindow, houseInfo);
                LatLng ll = PositionUtil.gpstoBd(new LatLng(houseInfo.getY(), houseInfo.getX()));
                mInfoWindow = new InfoWindow(baidumap_infowindow, ll, -47);
                //显示InfoWindow
                mBaiduMap.showInfoWindow(mInfoWindow);
                return false;
            }
        });

        mBaiduMap.setOnMapClickListener(new BaiduMap.OnMapClickListener() {
            @Override
            public void onMapClick(LatLng latLng) {
                //隐藏InfoWindow
                if (mInfoWindow != null) {
                    mBaiduMap.hideInfoWindow();
                    mMapView.postInvalidate();
                }
                if(addHouse&&getPolyInfo(latLng)!=null){
                    PolyOverlyInfo polyInfo = getPolyInfo(latLng);
                    LatLng downlatlng=PositionUtil.convertBaiduToGPS(latLng);
                    Intent intent = new Intent(getContext(), AddpointActivity.class);
                    intent.putExtra("streetName", polyInfo.getStreetName());
                    intent.putExtra("streetCode", polyInfo.getVillageName());
                    intent.putExtra("streetID", polyInfo.getVillageId());
                    intent.putExtra("lon", downlatlng.longitude);
                    intent.putExtra("lat", downlatlng.latitude);
                    startActivity(intent);
                }else{
                    return;
                }
            }
            @Override
            public boolean onMapPoiClick(MapPoi mapPoi) {
                return false;
            }
        });

        return view;
    }




    /**
     * @param baidumap_infowindow
     * @param
     * @Description: 创建 弹出窗口
     * @Author:王松山
     * @Since: 2017年5月22日
     */
    private void createInfoWindow(LinearLayout baidumap_infowindow, final HousemarkBean bean) {

        InfoWindowHolder holder = null;
        if (baidumap_infowindow.getTag() == null) {
            holder = new InfoWindowHolder();
            holder.tv_entname = (TextView) baidumap_infowindow.findViewById(R.id.tv_entname);
            holder.tv_checkdept = (TextView) baidumap_infowindow.findViewById(R.id.tv_checkdept);
            holder.tv_checkuser = (TextView) baidumap_infowindow.findViewById(R.id.tv_checkuser);
            holder.tv_checktime = (TextView) baidumap_infowindow.findViewById(R.id.tv_checktime);
            holder.tv_dispose = (TextView) baidumap_infowindow.findViewById(R.id.tv_dispose);
            holder.tv_xcjl = (TextView) baidumap_infowindow.findViewById(R.id.tv_xcjl);
            holder.tv_huinfo=(TextView) baidumap_infowindow.findViewById(R.id.tv_huinfo);
            holder.tv_delete=(TextView) baidumap_infowindow.findViewById(R.id.tv_delete);
            baidumap_infowindow.setTag(holder);
        }
        holder = (InfoWindowHolder) baidumap_infowindow.getTag();

        holder.tv_entname.setOnClickListener(new View.OnClickListener() {//创建任务
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getContext(), CreatetaskActivity.class);
                intent.putExtra("houseNum", bean.getBh());
                startActivity(intent);
                //隐藏InfoWindow
                mBaiduMap.hideInfoWindow();
            }
        });


    }
    public class InfoWindowHolder {
        public TextView tv_entname;
        public TextView tv_checkdept;
        public TextView tv_checkuser,tv_huinfo;
        public TextView tv_checktime,tv_xcjl,tv_dispose,tv_delete;
    }





    @Override
    public void onDestroyView() {
        // 退出时销毁定位
        mLocationClient.stop();
        // 关闭定位图层
        mBaiduMap.setMyLocationEnabled(false);
        mMapView.onDestroy();
        mMapView = null;
        super.onDestroyView();
    }

    @Override
    public void onResume() {
        // MapView的生命周期与Activity同步,当activity恢复时需调用MapView.onResume()
        mMapView.onResume();
        super.onResume();
    }
    @Override
    public void onPause() {
        mMapView.onPause();
        super.onPause();
    }
    /**
     * @paramgeoPoint当前点击的点
     * @return当前点击位置所在街道信息
     */
    private PolyOverlyInfo getPolyInfo(LatLng point) {
        for (PolyOverlyInfo polygonInfo : polyInfoList) {
            if (PointUtils.IsPointInPolygon(polygonInfo.getPolyline().getPoints(), point)) {
                return polygonInfo;
            }
        }
        return null;
    }

    /**
     * 每个Marker点,包含Marker点坐标以及图标 将需要的数据通过构造加到Item中
     */
    public class MyItem implements ClusterItem {
        private  LatLng mPosition;
        private HousemarkBean mhousemarkBean;

        public MyItem(LatLng latLng,HousemarkBean housemarkBean) {
            mPosition = latLng;
            mhousemarkBean=housemarkBean;
        }

        @Override
        public LatLng getPosition() {
            return mPosition;
        }

        @Override
        public BitmapDescriptor getBitmapDescriptor() {
            return BitmapDescriptorFactory
                    .fromResource(R.drawable.position);
        }

    }


    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }



}

bdfragment_map.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">


    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@+id/rl_header">


        <com.baidu.mapapi.map.MapView
            android:id="@+id/mapview"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />



    </RelativeLayout>


</RelativeLayout>

baidumap_infowindow.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@drawable/pop"
    android:paddingTop="10dp"
    android:paddingBottom="25dp"
    android:paddingLeft="5dp"
    android:paddingRight="5dp"
    android:orientation="vertical"
    >

    <TextView
        android:id="@+id/tv_entname"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="创建任务"
        android:gravity="center"
        android:background="@drawable/resource_update_press"/>

    <TextView
        android:id="@+id/tv_delete"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="删除房屋"
        android:gravity="center"
        android:background="@drawable/resource_update_press"/>


    <TextView
        android:id="@+id/tv_checkdept"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="查看房屋"
        android:layout_marginTop="3dp"
        android:gravity="center"
        android:background="@drawable/resource_update_press"/>

    <TextView
        android:id="@+id/tv_checkuser"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="更新房屋"
        android:layout_marginTop="3dp"
        android:gravity="center"
        android:background="@drawable/resource_update_press"/>

    <TextView
        android:id="@+id/tv_checktime"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="鉴定房屋"
        android:layout_marginTop="3dp"
        android:gravity="center"
        android:background="@drawable/resource_update_press"/>
    <TextView
        android:id="@+id/tv_xcjl"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="巡检记录"
        android:layout_marginTop="3dp"
        android:gravity="center"
        android:background="@drawable/resource_update_press"/>

    <TextView
        android:id="@+id/tv_dispose"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="房屋处置"
        android:layout_marginTop="3dp"
        android:gravity="center"
        android:background="@drawable/resource_update_press"/>

    <TextView
        android:id="@+id/tv_huinfo"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="户信息"
        android:layout_marginTop="3dp"
        android:gravity="center"
        android:background="@drawable/resource_update_press"/>



</LinearLayout>
完成了




阅读更多 登录后自动展开
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页