package com.zztrapp.zztrapp.ui.map;

import android.app.Dialog;
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.net.Uri;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.view.inputmethod.EditorInfo;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.appcompat.widget.LinearLayoutCompat;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.RecyclerView;

import com.amap.api.maps.AMap;
import com.amap.api.maps.CameraUpdateFactory;
import com.amap.api.maps.MapsInitializer;
import com.amap.api.maps.UiSettings;
import com.amap.api.maps.model.BitmapDescriptorFactory;
import com.amap.api.maps.model.LatLng;
import com.amap.api.maps.model.Marker;
import com.amap.api.maps.model.MarkerOptions;
import com.google.android.flexbox.FlexboxLayoutManager;
import com.zztrapp.zztrapp.R;
import com.zztrapp.zztrapp.bean.HttpResult;
import com.zztrapp.zztrapp.bean.MapBfjListBean;
import com.zztrapp.zztrapp.bean.MapHcstListBean;
import com.zztrapp.zztrapp.bean.MapJcjListBean;
import com.zztrapp.zztrapp.bean.MapKhdwListBean;
import com.zztrapp.zztrapp.bean.MapNcshwsclssListBean;
import com.zztrapp.zztrapp.bean.MapZdjgdwListBean;
import com.zztrapp.zztrapp.bean.SelectRvItemBean;
import com.zztrapp.zztrapp.databinding.FragmentMapBinding;
import com.zztrapp.zztrapp.http.HttpMethods;
import com.zztrapp.zztrapp.http.ISubscriberOnNextListener;
import com.zztrapp.zztrapp.http.ProgressSubscriber;
import com.zztrapp.zztrapp.http.RxApiManager;
import com.zztrapp.zztrapp.ui.currency.adapter.FlexBoxItemAdapter;
import com.zztrapp.zztrapp.ui.map.adapter.SelectRvItemAdapter;
import com.zztrapp.zztrapp.utilcode.klogs.KLog;
import com.zztrapp.zztrapp.utilcode.mUtil.MyUtil;
import com.zztrapp.zztrapp.utilcode.util.ToastUtils;

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

/**
 * Describe MapFragment
 * Created by Bby on 2022/6/21
 * Email bby0856@163.com
 */

public class MapFragment extends Fragment implements AMap.OnMarkerClickListener {

    private FragmentMapBinding binding;
    private List<SelectRvItemBean> listNav1;
    private List<SelectRvItemBean> listNav2;
    private List<SelectRvItemBean> listNav3;
    private SelectRvItemAdapter adapter;

    private String selectTypeName = "";
    private int selectType = 1;
    private int selectChildType = 1;
    private String searchName = "";


    private boolean isShowDialog = true;
    private AMap aMap; //初始化地图控制器对象
    private UiSettings uiSettings;
    private LatLng defaultLocation = new LatLng(39.865246, 116.378517);//北京
    private List<MapZdjgdwListBean> zdjgdwlist;//土壤污染-重点监管单位
    private List<MapJcjListBean> jcjList;//地下水-监测井
    private List<MapBfjListBean> bfjList;//地下水-废弃井
    private List<MapKhdwListBean> khdwList;//地下水-考核点位
    private List<MapHcstListBean> hcstList;//农业农村-黑臭水体
    private List<MapNcshwsclssListBean> ncshwsclssList;//农业农村-农村生活污水处理设施

    public MapFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        binding = FragmentMapBinding.inflate(inflater, container, false);
        MapsInitializer.updatePrivacyShow(getContext(), true, true);
        MapsInitializer.updatePrivacyAgree(getContext(), true);
        binding.map.onCreate(savedInstanceState);
        init();
        initClick();
        return binding.getRoot();
    }

    private void init() {
        aMap = null;
        binding.defaultLocation.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                aMap.moveCamera(CameraUpdateFactory.newLatLngZoom(defaultLocation, 12));
            }
        });

        if (aMap == null) {
            aMap = binding.map.getMap();
            uiSettings = aMap.getUiSettings();
            //设置地图属性
            setMapAttribute();
            aMap.setOnMarkerClickListener(this);
        }

        listNav1 = new ArrayList<>();
        listNav2 = new ArrayList<>();
        listNav3 = new ArrayList<>();

        selectTypeName = "重点监管单位";
        //土壤环境
        listNav1.add(new SelectRvItemBean(1, 1, "重点监管单位", true));
//        listNav1.add(new SelectRvItemBean(1, 2, "土壤污染地块", false));
        //地下水环境
        listNav2.add(new SelectRvItemBean(2, 1, "监测井", false));
        listNav2.add(new SelectRvItemBean(2, 2, "报废井", false));
        listNav2.add(new SelectRvItemBean(2, 3, "考核点位", false));
//        listNav2.add(new SelectRvItemBean(2, 3, "双源调查", false));
        //农业农村
        listNav3.add(new SelectRvItemBean(3, 1, "黑臭水体", false));
        listNav3.add(new SelectRvItemBean(3, 2, "污水处理设施", false));

        binding.rv.setLayoutManager(new FlexboxLayoutManager(getContext()));
        adapter = new SelectRvItemAdapter(getContext());
        binding.rv.setAdapter(adapter);
        adapter.setData(listNav1);
        loadMapZdjgdw();
    }


    private void initClick() {

        adapter.setOnItemClickListener(new SelectRvItemAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(int type, int childType, int position) {
                resetList();
                selectType = type;
                selectChildType = childType;
                switch (type) {
                    case 1:
                        selectTypeName = listNav1.get(position).getName();
                        listNav1.get(position).setSelect(true);
                        adapter.setData(listNav1);
                        switch (childType) {
                            case 1:
                                loadMapZdjgdw();
                                break;
                            case 2:
                                break;
                        }
                        break;
                    case 2:
                        selectTypeName = listNav2.get(position).getName();
                        listNav2.get(position).setSelect(true);
                        adapter.setData(listNav2);
                        switch (childType) {
                            case 1:
                                loadMapJcj();
                                break;
                            case 2:
                                loadMapBfj();
                                break;
                            case 3:
                                loadMapKhdw();
                                break;
                        }
                        break;
                    case 3:
                        selectTypeName = listNav3.get(position).getName();
                        listNav3.get(position).setSelect(true);
                        adapter.setData(listNav3);
                        switch (childType) {
                            case 1:
                                loadMapHcst();
                                break;
                            case 2:
                                loadMapNcshwsclss();
                                break;
                        }
                        break;
                }
            }
        });
        binding.nav1View.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                setSelectType(1);
                adapter.setData(listNav1);
            }
        });
        binding.nav2View.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                setSelectType(2);
                adapter.setData(listNav2);
            }
        });
        binding.nav3View.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                setSelectType(3);
                adapter.setData(listNav3);
            }
        });
        binding.search.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                searchName = binding.searchEd.getText().toString().trim();
                switch (selectType) {
                    case 1:
                        switch (selectChildType) {
                            case 1:
                                loadMapBfj();
                                break;
                            case 2:
                                break;
                        }
                        break;
                    case 2:
                        switch (selectChildType) {
                            case 1:
                                loadMapJcj();
                                break;
                            case 2:
                                loadMapBfj();
                                break;
                            case 3:
                                loadMapKhdw();
                                break;
                        }
                        break;
                    case 3:
                        switch (selectChildType) {
                            case 1:
                                loadMapHcst();
                                break;
                            case 2:
                                loadMapNcshwsclss();
                                break;
                        }
                        break;
                }
            }
        });
        binding.del.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                searchName = "";
                binding.searchEd.setText(searchName);
                binding.del.setVisibility(View.GONE);
            }
        });
        binding.searchEd.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView arg0, int arg1, KeyEvent arg2) {
                if (arg1 == EditorInfo.IME_ACTION_SEARCH) {
                    searchName = arg0.getText().toString().trim();
                    switch (selectType) {
                        case 1:
                            switch (selectChildType) {
                                case 1:
                                    loadMapBfj();
                                    break;
                                case 2:
                                    break;
                            }
                            break;
                        case 2:
                            switch (selectChildType) {
                                case 1:
                                    loadMapJcj();
                                    break;
                                case 2:
                                    loadMapBfj();
                                    break;
                                case 3:
                                    loadMapKhdw();
                                    break;
                            }
                            break;
                        case 3:
                            switch (selectChildType) {
                                case 1:
                                    loadMapHcst();
                                    break;
                                case 2:
                                    loadMapNcshwsclss();
                                    break;
                            }
                            break;
                    }
                }
                return false;
            }
        });
        binding.searchEd.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                searchName = charSequence.toString();
                if (charSequence.length() > 0) {
                    binding.del.setVisibility(View.VISIBLE);
                } else {
                    binding.del.setVisibility(View.GONE);
                }
            }

            @Override
            public void afterTextChanged(Editable editable) {
            }
        });
    }

    //设置大类选中
    private void setSelectType(int type) {
        binding.nav1.setTextColor(getResources().getColor(R.color.color333333));
        binding.nav1Icon.setBackgroundResource(R.mipmap.jiantou_ic_grey);
        binding.nav2.setTextColor(getResources().getColor(R.color.color333333));
        binding.nav2Icon.setBackgroundResource(R.mipmap.jiantou_ic_grey);
        binding.nav3.setTextColor(getResources().getColor(R.color.color333333));
        binding.nav3Icon.setBackgroundResource(R.mipmap.jiantou_ic_grey);
        switch (type) {
            case 1:
                binding.nav1.setTextColor(getResources().getColor(R.color.color007AFF));
                binding.nav1Icon.setBackgroundResource(R.mipmap.jiantou_ic_blue);
                break;
            case 2:
                binding.nav2.setTextColor(getResources().getColor(R.color.color007AFF));
                binding.nav2Icon.setBackgroundResource(R.mipmap.jiantou_ic_blue);
                break;
            case 3:
                binding.nav3.setTextColor(getResources().getColor(R.color.color007AFF));
                binding.nav3Icon.setBackgroundResource(R.mipmap.jiantou_ic_blue);
                break;
        }
    }

    //重置列表数据选中状态
    private void resetList() {
        for (int i = 0; i < listNav1.size(); i++) {
            listNav1.get(i).setSelect(false);
        }
        for (int j = 0; j < listNav2.size(); j++) {
            listNav2.get(j).setSelect(false);
        }
        for (int k = 0; k < listNav3.size(); k++) {
            listNav3.get(k).setSelect(false);
        }
    }

    /**
     * 设置地图属性
     */
    private void setMapAttribute() {
        //设置默认缩放级别
        aMap.moveCamera(CameraUpdateFactory.zoomTo(12));
        //隐藏的右下角缩放按钮
        uiSettings.setZoomControlsEnabled(false);
        //设置地图默认的比例尺是否显示
        uiSettings.setScaleControlsEnabled(true);
//        aMap.setLocationSource(this);// 设置定位监听
//        uiSettings.setMyLocationButtonEnabled(true); // 是否显示默认的定位按钮
        aMap.setMyLocationEnabled(false);// 是否可触发定位并显示定位层

        //设置marker点击事件监听
        //aMap.setOnMarkerClickListener(this);
        //设置自定义信息窗口
        //aMap.setInfoWindowAdapter(this);
        //设置地图点击事件监听
        //aMap.setOnMapClickListener(this);
    }

    /**
     * 对marker标注点点击响应事件
     */
    @Override
    public boolean onMarkerClick(final Marker marker) {
        initImageDialog(marker.getTitle(), marker.getSnippet(), marker.getPosition());
        return true;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        //在activity执行onDestroy时执行mMapView.onDestroy()，销毁地图
        binding.map.onDestroy();
        binding = null;
        RxApiManager.get().cancel("mapZdjgdw");
        RxApiManager.get().cancel("mapBfj");
        RxApiManager.get().cancel("mapJcj");
        RxApiManager.get().cancel("mapKhdw");
        RxApiManager.get().cancel("mapHcst");
        RxApiManager.get().cancel("mapNcshwsclss");
    }

    @Override
    public void onResume() {
        super.onResume();
        //在activity执行onResume时执行mMapView.onResume ()，重新绘制加载地图
        binding.map.onResume();
    }

    @Override
    public void onPause() {
        super.onPause();
        //在activity执行onPause时执行mMapView.onPause ()，暂停地图的绘制
        binding.map.onPause();
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        //在activity执行onSaveInstanceState时执行mMapView.onSaveInstanceState (outState)，保存地图当前的状态
        binding.map.onSaveInstanceState(outState);
    }

    /**
     * 初始化Dialog
     */
    private PopupWindow mPopWindow;

    private void initImageDialog(String name, String positionS, LatLng latLng) {
        View contentView = LayoutInflater.from(getContext()).inflate(R.layout.map_marker_info_dialog, null);
        mPopWindow = new PopupWindow(contentView, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT, true);
        ImageView cancle = contentView.findViewById(R.id.cancel);
        TextView title = contentView.findViewById(R.id.title);
        RecyclerView rv = contentView.findViewById(R.id.rv);
//        TextView codeTv = contentView.findViewById(R.id.code);
//        TextView nameTv = contentView.findViewById(R.id.name);
        TextView jd = contentView.findViewById(R.id.jd);
        TextView wd = contentView.findViewById(R.id.wd);
        RelativeLayout popRootView = contentView.findViewById(R.id.pop_root_view);
        RelativeLayout popRoot = contentView.findViewById(R.id.pop_root);
        ImageView addressIcon = contentView.findViewById(R.id.address);


        title.setText(selectTypeName + "基本信息");
//        codeTv.setText(code);
//        nameTv.setText(name);

        //当前点击点位的下标
        int position = Integer.parseInt(positionS);
        KLog.e("------11-------");
        KLog.e("-------------:" + position + "," + selectType + "," + selectChildType);
        List<String> arrayList = new ArrayList<>();
        rv.setLayoutManager(new FlexboxLayoutManager(getContext()));
        FlexBoxItemAdapter adapter = new FlexBoxItemAdapter(getContext());
        switch (selectType) {
            case 1:
                switch (selectChildType) {
                    case 1:
//                        loadMapZdjgdw();
                        arrayList.add("企业名称：" + zdjgdwlist.get(position).getQymc());
                        arrayList.add("统一社会信用代码：" + zdjgdwlist.get(position).getShtyxydm());
                        arrayList.add("行政区划：" + zdjgdwlist.get(position).getCountyName());
                        arrayList.add("纳入原因：" + zdjgdwlist.get(position).getNrmdyy());
//                        arrayList.add("自行监测、隐患排查、拆除活动、有毒有害物质排放报告（所处阶段）：" + zdjgdwlist.get(position).get());
                        rv.setAdapter(adapter);
                        adapter.setData(arrayList);
                        break;
                    case 2:
                        break;
                }
                break;
            case 2:
                switch (selectChildType) {
                    case 1:
//                         loadMapJcj();
                        arrayList.add("监测井统一编号：" + jcjList.get(position).getJcjtybh());
                        arrayList.add("行政区划：" + jcjList.get(position).getCountyName());
                        arrayList.add("地理位置：" + jcjList.get(position).getCountyName());
                        arrayList.add("双源清单类型：" + jcjList.get(position).getSyqdlx());
                        arrayList.add("双源清单名称：" + jcjList.get(position).getSyqdmc());
                        arrayList.add("所属单位：" + jcjList.get(position).getSsdw());
                        arrayList.add("联系人：" + jcjList.get(position).getLxr());
                        rv.setAdapter(adapter);
                        adapter.setData(arrayList);
                        break;
                    case 2:
//                        loadMapBfj();
                        arrayList.add("废弃井编号：" + bfjList.get(position).getFqjbh());
                        arrayList.add("名称：" + bfjList.get(position).getMc());
                        arrayList.add("废弃井类型：" + bfjList.get(position).getFqjlx());
                        arrayList.add("废弃井井别：" + bfjList.get(position).getFqjjb());
                        arrayList.add("所属单位：" + bfjList.get(position).getSsdw());
                        switch (bfjList.get(position).getZt()) {//状态 0 为回填 1、已回填
                            case 0:
                                arrayList.add("回填状态：回填");
                                break;
                            case 1:
                                arrayList.add("回填状态：已回填");
                                break;
                        }
                        rv.setAdapter(adapter);
                        adapter.setData(arrayList);
                        break;
                    case 3:
                        arrayList.add("点位编号：" + khdwList.get(position).getDwbh());
                        arrayList.add("站点名称：" + khdwList.get(position).getZdmc());
//                        arrayList.add("地理坐标（经度)：" + khdwList.get(position).getJd());
//                        arrayList.add("地理坐标（纬度）" + khdwList.get(position).getWd());
                        arrayList.add("考核点位类型：" + khdwList.get(position).getKhdwlx());
                        arrayList.add("水质要求：" + khdwList.get(position).getKhdwszyq());
                        rv.setAdapter(adapter);
                        adapter.setData(arrayList);
                        break;
                }
                break;
            case 3:
                switch (selectChildType) {
                    case 1:
//                        loadMapHcst();
                        arrayList.add("水体编号：" + hcstList.get(position).getStbh());
                        arrayList.add("水体名称：" + hcstList.get(position).getStmc());
                        arrayList.add("行政村名称：" + hcstList.get(position).getVillageName());
                        arrayList.add("行政区划：" + hcstList.get(position).getCityName());
                        switch (hcstList.get(position).getZljb()) {//治理级别0、正常治理 1、优先治理
                            case 0:
                                arrayList.add("治理级别：正常治理");
                                break;
                            case 1:
                                arrayList.add("治理级别：优先治理");
                                break;
                        }
                        switch (hcstList.get(position).getZljz()) {//治理进展0、未开始 1、治理中 2、治理完成
                            case 0:
                                arrayList.add("治理进展：未开始");
                                break;
                            case 1:
                                arrayList.add("治理进展：治理中");
                                break;
                            case 2:
                                arrayList.add("治理进展：治理完成");
                                break;
                        }
                        switch (hcstList.get(position).getAqzt()) {//安全状态0、黑臭水体 1、正常水体 默认0
                            case 0:
                                arrayList.add("水体状态：黑臭水体");
                                break;
                            case 1:
                                arrayList.add("水体状态：正常水体");
                                break;
                        }

                        switch (hcstList.get(position).getIsFfst()) {//是否是反复水体0、否 1、是 默认0
                            case 0:
                                arrayList.add("是否反复水体：否");
                                break;
                            case 1:
                                arrayList.add("是否反复水体：是");
                                break;
                        }
                        arrayList.add("黑臭段起点：" + hcstList.get(position).getHcdqd());
                        arrayList.add("黑臭段终点：" + hcstList.get(position).getHcdzd());
                        arrayList.add("黑臭段起点精度：" + hcstList.get(position).getHcdqdjd());
                        arrayList.add("黑臭段起点纬度：" + hcstList.get(position).getHcdqdwd());
                        arrayList.add("黑臭段终点精度：" + hcstList.get(position).getHcdzdjd());
                        arrayList.add("黑臭段终点纬度：" + hcstList.get(position).getHcdzdwd());
                        rv.setAdapter(adapter);
                        adapter.setData(arrayList);
                        break;
                    case 2:
//                        loadMapNcshwsclss();
                        arrayList.add("设备编号：" + ncshwsclssList.get(position).getSbbh());
                        arrayList.add("设备名称：" + ncshwsclssList.get(position).getSbmc());
                        arrayList.add("行政区划：" + ncshwsclssList.get(position).getCountyName());
                        arrayList.add("运行状态：" + ncshwsclssList.get(position).getYxzt());
                        arrayList.add("设计规模（吨/日）：" + ncshwsclssList.get(position).getSjgm());
                        arrayList.add("实际规模（吨/日）：" + ncshwsclssList.get(position).getSjgm());
                        arrayList.add("设计出水标准：" + ncshwsclssList.get(position).getSjcsbz());
                        arrayList.add("设备所处经度：" + ncshwsclssList.get(position).getSbscwzjd());
                        arrayList.add("设备所处纬度：" + ncshwsclssList.get(position).getSbscwzwd());
                        rv.setAdapter(adapter);
                        adapter.setData(arrayList);
                        break;
                }
                break;
        }

        KLog.e("------33-------");

        //经纬度
//        DecimalFormat df = new DecimalFormat("#0.000000"); // 保留两位小数，四舍五入
//        jd.setText(df.format(latLng.longitude));
//        wd.setText(df.format(latLng.latitude));

        jd.setText(MyUtil.getNumberPoint(latLng.longitude));
        wd.setText(MyUtil.getNumberPoint(latLng.latitude));
        //取消
        cancle.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (mPopWindow != null && mPopWindow.isShowing()) {
                    mPopWindow.dismiss();
                }
            }
        });
        popRootView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (mPopWindow != null && mPopWindow.isShowing()) {
                    mPopWindow.dismiss();
                }
            }
        });
        popRoot.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
            }
        });
        addressIcon.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                initAddressDialog(name, latLng);
            }
        });
//        mPopWindow.setBackgroundDrawable(new BitmapDrawable());
//        mPopWindow.setOutsideTouchable(true);
//        mPopWindow.setFocusable(true);
//        mPopWindow.setAnimationStyle(R.style.main_menu_animstyle);
        KLog.e("------44-------");

        if (mPopWindow != null && !mPopWindow.isShowing()) {
            KLog.e("------55-------");
            mPopWindow.showAtLocation(binding.rootView, Gravity.TOP, 0, 0);
        }
    }

    /**
     * 初始化Dialog
     */
    private void initAddressDialog(String addressName, LatLng latLng) {
        View dialogView = getLayoutInflater().inflate(R.layout.nagination_choose_dialog, null);
        Button btn_cancle = (Button) dialogView.findViewById(R.id.btn_cancle);
        btn_cancle.setTextColor(Color.parseColor("#0076FF"));
        Button btn_baidu = (Button) dialogView.findViewById(R.id.btn_baidu_navigation);
        btn_baidu.setTextColor(Color.parseColor("#0076FF"));
        Button btn_gaode = (Button) dialogView.findViewById(R.id.btn_gaode_navigation);
        btn_gaode.setTextColor(Color.parseColor("#0076FF"));
        final Dialog navigationDialog = new Dialog(getActivity(), R.style.transparentFrameWindowStyle);
        navigationDialog.setContentView(dialogView, new LinearLayoutCompat.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
        Window window = navigationDialog.getWindow();
        window.setWindowAnimations(R.style.main_menu_animstyle);
        WindowManager.LayoutParams wl = window.getAttributes();
        wl.x = 0;
        wl.y = getActivity().getWindowManager().getDefaultDisplay().getHeight();
        wl.width = ViewGroup.LayoutParams.MATCH_PARENT;
        wl.height = ViewGroup.LayoutParams.WRAP_CONTENT;
        navigationDialog.onWindowAttributesChanged(wl);
        navigationDialog.setCanceledOnTouchOutside(true);
        navigationDialog.show();
        //取消
        btn_cancle.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                navigationDialog.dismiss();
            }
        });

        //百度
        btn_baidu.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                openMapToDaoHan(2, addressName, latLng);
                navigationDialog.dismiss();
            }
        });

        //高德
        btn_gaode.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                openMapToDaoHan(1, addressName, latLng);
                navigationDialog.dismiss();
            }
        });
    }

    /**
     * type:1 高德 2百度
     */
    private void openMapToDaoHan(int type, String name, LatLng latLng) {
        String errString = "";
        try {
            switch (type) {
                case 1:
                    errString = "手机未安装高德地图APP";
//                Uri uri1 = Uri.parse("amapuri://route/plan/?dlat=" + "目的地lat" + "&dlon=" + "目的地lng" + "&dname=" + "目的地名称" + "&dev=0&t=0");
                    Uri uri1 = Uri.parse("amapuri://route/plan/?dlat=" + latLng.latitude + "&dlon=" + latLng.longitude + "&dname=" + name + "&dev=0&t=0");
                    startActivity(new Intent(Intent.ACTION_VIEW, uri1));
                    break;
                case 2:
                    errString = "手机未安装百度地图APP";
//                    Uri uri = Uri.parse("baidumap://map/direction?destination=latlng:" + "目的地lat" + "," + "目的地lng" + "|name:" + "目的地名称" + "&mode=driving");
                    Uri uri2 = Uri.parse("baidumap://map/direction?destination=latlng:" + latLng.latitude + "," + latLng.longitude + "|name:" + name + "&mode=driving");
                    startActivity(new Intent(Intent.ACTION_VIEW, uri2));
                    break;
            }
        } catch (Exception ActivityNotFoundException) {
            ToastUtils.make().setGravity(Gravity.CENTER, 0, 0).show(errString);
        }
    }


    //-----------------------------------------------
    //重点监管单位-土壤环境
    private void loadMapZdjgdw() {
        ISubscriberOnNextListener<HttpResult<List<MapZdjgdwListBean>>> iSubscriberOnNextListener = new ISubscriberOnNextListener<HttpResult<List<MapZdjgdwListBean>>>() {
            @Override
            public void onNext(HttpResult<List<MapZdjgdwListBean>> httpResult) {
                if (httpResult.getCode() == 200) {
                    zdjgdwlist = httpResult.getResult();
                    aMap.clear();
                    for (int i = 0; i < zdjgdwlist.size(); i++) {
                        if (!TextUtils.isEmpty(zdjgdwlist.get(i).getWd()) && !TextUtils.isEmpty(zdjgdwlist.get(i).getJd())) {
                            MarkerOptions markerOption = new MarkerOptions();
                            double latitude = Double.parseDouble(zdjgdwlist.get(i).getWd());
                            double longitude = Double.parseDouble(zdjgdwlist.get(i).getJd());
                            LatLng latLng = new LatLng(latitude, longitude);
                            markerOption.position(latLng);
                            //todo title snippet
//                        markerOption.title(list.get(i).getCountyName()).snippet(list.get(i).getQymc());
                            markerOption.title(zdjgdwlist.get(i).getCountyName()).snippet(i + "");
                            //自定义点标记的icon图标为drawable文件下图片
                            markerOption.icon(BitmapDescriptorFactory.fromBitmap(BitmapFactory.decodeResource(getResources(), R.mipmap.ditu_dibiao)));
                            Marker marker = aMap.addMarker(markerOption);
                            marker.hideInfoWindow();
                            if (i == 0) {
                                defaultLocation = latLng;
                            }
                        }
                    }
                    aMap.moveCamera(CameraUpdateFactory.newLatLngZoom(defaultLocation, 11));
                } else {
                    ToastUtils.make().setGravity(Gravity.CENTER, 0, 0).show(httpResult.getMessage());
                }
            }

            @Override
            public void onError(int code, String message) {
                ToastUtils.make().setGravity(Gravity.CENTER, 0, 0).show(message);
            }
        };
        ProgressSubscriber<HttpResult<List<MapZdjgdwListBean>>> httpResultProgressSubscriber = new ProgressSubscriber<>(iSubscriberOnNextListener, getContext(), true);
        RxApiManager.get().add("mapZdjgdw", httpResultProgressSubscriber);
        HttpMethods.getInstance(getContext()).mapZdjgdw(httpResultProgressSubscriber, searchName);
    }

    //报废井-地下水环境
    private void loadMapBfj() {
        ISubscriberOnNextListener<HttpResult<List<MapBfjListBean>>> iSubscriberOnNextListener = new ISubscriberOnNextListener<HttpResult<List<MapBfjListBean>>>() {
            @Override
            public void onNext(HttpResult<List<MapBfjListBean>> httpResult) {
                if (httpResult.getCode() == 200) {
                    bfjList = httpResult.getResult();
                    aMap.clear();
                    for (int i = 0; i < bfjList.size(); i++) {
                        MarkerOptions markerOption = new MarkerOptions();
                        double latitude = Double.parseDouble(bfjList.get(i).getWd());
                        double longitude = Double.parseDouble(bfjList.get(i).getJd());
                        LatLng latLng = new LatLng(latitude, longitude);
                        markerOption.position(latLng);
                        //todo title snippet
//                        markerOption.title(bfjList.get(i).getCountyName()).snippet(bfjList.get(i).getMc());
                        markerOption.title(bfjList.get(i).getCountyName()).snippet(i + "");
                        //自定义点标记的icon图标为drawable文件下图片
                        markerOption.icon(BitmapDescriptorFactory.fromBitmap(BitmapFactory.decodeResource(getResources(), R.mipmap.ditu_dibiao)));
                        Marker marker = aMap.addMarker(markerOption);
                        marker.hideInfoWindow();
                        if (i == 0) {
                            defaultLocation = latLng;
                        }
                    }
                    aMap.moveCamera(CameraUpdateFactory.newLatLngZoom(defaultLocation, 11));
                } else {
                    ToastUtils.make().setGravity(Gravity.CENTER, 0, 0).show(httpResult.getMessage());
                }
            }

            @Override
            public void onError(int code, String message) {
                ToastUtils.make().setGravity(Gravity.CENTER, 0, 0).show(message);
            }
        };
        ProgressSubscriber<HttpResult<List<MapBfjListBean>>> httpResultProgressSubscriber = new ProgressSubscriber<>(iSubscriberOnNextListener, getContext(), true);
        RxApiManager.get().add("mapBfj", httpResultProgressSubscriber);
        HttpMethods.getInstance(getContext()).mapBfj(httpResultProgressSubscriber, searchName);
    }

    //检测井-地下水环境
    private void loadMapJcj() {
        ISubscriberOnNextListener<HttpResult<List<MapJcjListBean>>> iSubscriberOnNextListener = new ISubscriberOnNextListener<HttpResult<List<MapJcjListBean>>>() {
            @Override
            public void onNext(HttpResult<List<MapJcjListBean>> httpResult) {
                if (httpResult.getCode() == 200) {
                    jcjList = httpResult.getResult();
                    aMap.clear();
                    for (int i = 0; i < jcjList.size(); i++) {
                        MarkerOptions markerOption = new MarkerOptions();
                        double latitude = Double.parseDouble(jcjList.get(i).getWd());
                        double longitude = Double.parseDouble(jcjList.get(i).getJd());
                        LatLng latLng = new LatLng(latitude, longitude);
                        markerOption.position(latLng);
                        //todo title snippet
//                        markerOption.title(jcjList.get(i).getCountyName()).snippet(jcjList.get(i).getMc());
                        markerOption.title(jcjList.get(i).getCountyName()).snippet(i + "");
                        //自定义点标记的icon图标为drawable文件下图片
                        markerOption.icon(BitmapDescriptorFactory.fromBitmap(BitmapFactory.decodeResource(getResources(), R.mipmap.ditu_dibiao)));
                        Marker marker = aMap.addMarker(markerOption);
                        marker.hideInfoWindow();
                        if (i == 0) {
                            defaultLocation = latLng;
                        }
                    }
                    aMap.moveCamera(CameraUpdateFactory.newLatLngZoom(defaultLocation, 11));
                } else {
                    ToastUtils.make().setGravity(Gravity.CENTER, 0, 0).show(httpResult.getMessage());
                }
            }

            @Override
            public void onError(int code, String message) {
                ToastUtils.make().setGravity(Gravity.CENTER, 0, 0).show(message);
            }
        };
        ProgressSubscriber<HttpResult<List<MapJcjListBean>>> httpResultProgressSubscriber = new ProgressSubscriber<>(iSubscriberOnNextListener, getContext(), true);
        RxApiManager.get().add("mapJcj", httpResultProgressSubscriber);
        HttpMethods.getInstance(getContext()).mapJcj(httpResultProgressSubscriber, searchName);
    }

    //考核点位-地下水环境
    private void loadMapKhdw() {
        ISubscriberOnNextListener<HttpResult<List<MapKhdwListBean>>> iSubscriberOnNextListener = new ISubscriberOnNextListener<HttpResult<List<MapKhdwListBean>>>() {
            @Override
            public void onNext(HttpResult<List<MapKhdwListBean>> httpResult) {
                if (httpResult.getCode() == 200) {
                    khdwList = httpResult.getResult();
                    aMap.clear();
                    for (int i = 0; i < khdwList.size(); i++) {
                        MarkerOptions markerOption = new MarkerOptions();
                        double latitude = Double.parseDouble(khdwList.get(i).getWd());
                        double longitude = Double.parseDouble(khdwList.get(i).getJd());
                        LatLng latLng = new LatLng(latitude, longitude);
                        markerOption.position(latLng);
                        //todo title snippet
//                        markerOption.title(khdwList.get(i).getCountyName()).snippet(khdwList.get(i).getMc());
                        markerOption.title(khdwList.get(i).getCountyName()).snippet(i + "");
                        //自定义点标记的icon图标为drawable文件下图片
                        markerOption.icon(BitmapDescriptorFactory.fromBitmap(BitmapFactory.decodeResource(getResources(), R.mipmap.ditu_dibiao)));
                        Marker marker = aMap.addMarker(markerOption);
                        marker.hideInfoWindow();
                        if (i == 0) {
                            defaultLocation = latLng;
                        }
                    }
                    aMap.moveCamera(CameraUpdateFactory.newLatLngZoom(defaultLocation, 11));
                } else {
                    ToastUtils.make().setGravity(Gravity.CENTER, 0, 0).show(httpResult.getMessage());
                }
            }

            @Override
            public void onError(int code, String message) {
                ToastUtils.make().setGravity(Gravity.CENTER, 0, 0).show(message);
            }
        };
        ProgressSubscriber<HttpResult<List<MapKhdwListBean>>> httpResultProgressSubscriber = new ProgressSubscriber<>(iSubscriberOnNextListener, getContext(), true);
        RxApiManager.get().add("mapKhdw", httpResultProgressSubscriber);
        HttpMethods.getInstance(getContext()).mapKhdw(httpResultProgressSubscriber, searchName);
    }

    //农业农村-黑臭水体
    private void loadMapHcst() {
        ISubscriberOnNextListener<HttpResult<List<MapHcstListBean>>> iSubscriberOnNextListener = new ISubscriberOnNextListener<HttpResult<List<MapHcstListBean>>>() {
            @Override
            public void onNext(HttpResult<List<MapHcstListBean>> httpResult) {
                if (httpResult.getCode() == 200) {
                    hcstList = httpResult.getResult();
                    aMap.clear();
                    for (int i = 0; i < hcstList.size(); i++) {
                        if (!TextUtils.isEmpty(hcstList.get(i).getHcdqdwd())) {
                            MarkerOptions markerOption = new MarkerOptions();
                            double latitude = Double.parseDouble(hcstList.get(i).getHcdqdwd());
                            double longitude = Double.parseDouble(hcstList.get(i).getHcdqdjd());
                            LatLng latLng = new LatLng(latitude, longitude);
                            markerOption.position(latLng);
                            //todo title snippet
//                            markerOption.title(list.get(i).getCountyName()).snippet(list.get(i).getStmc());
                            markerOption.title(hcstList.get(i).getCountyName()).snippet(i + "");
                            //自定义点标记的icon图标为drawable文件下图片
                            markerOption.icon(BitmapDescriptorFactory.fromBitmap(BitmapFactory.decodeResource(getResources(), R.mipmap.ditu_dibiao)));
                            Marker marker = aMap.addMarker(markerOption);
                            marker.hideInfoWindow();
                            if (i == 0) {
                                defaultLocation = latLng;
                            }
                        }
                    }
                    aMap.moveCamera(CameraUpdateFactory.newLatLngZoom(defaultLocation, 11));
                } else {
                    ToastUtils.make().setGravity(Gravity.CENTER, 0, 0).show(httpResult.getMessage());
                }
            }

            @Override
            public void onError(int code, String message) {
                ToastUtils.make().setGravity(Gravity.CENTER, 0, 0).show(message);
            }
        };
        ProgressSubscriber<HttpResult<List<MapHcstListBean>>> httpResultProgressSubscriber = new ProgressSubscriber<>(iSubscriberOnNextListener, getContext(), true);
        RxApiManager.get().add("mapHcst", httpResultProgressSubscriber);
        HttpMethods.getInstance(getContext()).mapHcst(httpResultProgressSubscriber, searchName);
    }

    //农业农村-农村生活污水处理设施
    private void loadMapNcshwsclss() {
        ISubscriberOnNextListener<HttpResult<List<MapNcshwsclssListBean>>> iSubscriberOnNextListener = new ISubscriberOnNextListener<HttpResult<List<MapNcshwsclssListBean>>>() {
            @Override
            public void onNext(HttpResult<List<MapNcshwsclssListBean>> httpResult) {
                if (httpResult.getCode() == 200) {
                    ncshwsclssList = httpResult.getResult();
                    aMap.clear();
                    for (int i = 0; i < ncshwsclssList.size(); i++) {
                        if (!TextUtils.isEmpty(ncshwsclssList.get(i).getSbscwzwd())) {
                            MarkerOptions markerOption = new MarkerOptions();
                            double latitude = Double.parseDouble(ncshwsclssList.get(i).getSbscwzwd()
                            );
                            double longitude = Double.parseDouble(ncshwsclssList.get(i).getSbscwzjd());
                            LatLng latLng = new LatLng(latitude, longitude);
                            markerOption.position(latLng);
                            //todo title snippet
//                            markerOption.title(list.get(i).getCountyName()).snippet(list.get(i).getSbmc());
                            markerOption.title(ncshwsclssList.get(i).getCountyName()).snippet(i + "");
                            //自定义点标记的icon图标为drawable文件下图片
                            markerOption.icon(BitmapDescriptorFactory.fromBitmap(BitmapFactory.decodeResource(getResources(), R.mipmap.ditu_dibiao)));
                            Marker marker = aMap.addMarker(markerOption);
                            marker.hideInfoWindow();
                            if (i == 0) {
                                defaultLocation = latLng;
                            }
                        }
                    }
                    aMap.moveCamera(CameraUpdateFactory.newLatLngZoom(defaultLocation, 11));
                } else {
                    ToastUtils.make().setGravity(Gravity.CENTER, 0, 0).show(httpResult.getMessage());
                }
            }

            @Override
            public void onError(int code, String message) {
                ToastUtils.make().setGravity(Gravity.CENTER, 0, 0).show(message);
            }
        };
        ProgressSubscriber<HttpResult<List<MapNcshwsclssListBean>>> httpResultProgressSubscriber = new ProgressSubscriber<>(iSubscriberOnNextListener, getContext(), true);
        RxApiManager.get().add("mapNcshwsclss", httpResultProgressSubscriber);
        HttpMethods.getInstance(getContext()).mapNcshwsclss(httpResultProgressSubscriber, searchName);
    }
}