package com.example.map;

import android.Manifest;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;

import com.baidu.location.BDAbstractLocationListener;
import com.baidu.location.BDLocation;
import com.baidu.location.LocationClient;
import com.baidu.location.LocationClientOption;
import com.baidu.mapapi.SDKInitializer;
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.MapStatusUpdate;
import com.baidu.mapapi.map.MapStatusUpdateFactory;
import com.baidu.mapapi.map.Marker;
import com.baidu.mapapi.map.MarkerOptions;
import com.baidu.mapapi.map.MyLocationData;
import com.baidu.mapapi.map.OverlayOptions;
import com.baidu.mapapi.map.TextureMapView;
import com.baidu.mapapi.model.LatLng;

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

public class MainActivity extends Activity implements View.OnClickListener {

    TextView locationInfo;
    public LocationClient mLocationClient = null;
    private MyLocationListener myListener = new MyLocationListener();


    //显示地图
    TextureMapView mMapView;
    BaiduMap mBaiduMap = null;

    boolean isFirstLocate = true;
    private InfoWindow infoWindow;


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

        //环境初始化
        SDKInitializer.initialize(getApplicationContext());

        setContentView(R.layout.activity_main);

        //这个是显示经纬度信息
        //locationInfo = findViewById(R.id.locationInfo);
        //添加弹窗
//        findViewById(R.id.show).setOnClickListener(this);

        LocationClient.setAgreePrivacy(true);
        try {
            mLocationClient = new LocationClient(getApplicationContext());
            mLocationClient.registerLocationListener(new MyLocationListener());


            mMapView = findViewById(R.id.bmapView);
            mBaiduMap = mMapView.getMap();

            mBaiduMap.setMapType(BaiduMap.MAP_TYPE_NORMAL);

            mBaiduMap.setMyLocationEnabled(true);//当前的定位

            requestLocation();

            //三个最基础的权限
            List<String> PermissionList = new ArrayList<String>();
            if (ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.ACCESS_FINE_LOCATION)!= PackageManager.PERMISSION_GRANTED){
                PermissionList.add(Manifest.permission.ACCESS_FINE_LOCATION);
            }
            if (ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.READ_PHONE_STATE)!= PackageManager.PERMISSION_GRANTED){
                PermissionList.add(Manifest.permission.READ_PHONE_STATE);
            }
            if (ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.WRITE_EXTERNAL_STORAGE)!= PackageManager.PERMISSION_GRANTED){
                PermissionList.add(Manifest.permission.WRITE_EXTERNAL_STORAGE);
            }
            if (!PermissionList.isEmpty()) {
                String[] permissions = PermissionList.toArray(new String[PermissionList.size()]);
                ActivityCompat.requestPermissions(MainActivity.this,permissions,1);
            } else {
                requestLocation();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        //控件选择
        //按钮“我的”
        Button button = findViewById(R.id.Myself);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            //按钮点击后执行下面回调方法
            public void onClick(View v) {
                Intent intent = new Intent();
                //setclass显示意图，直接指定来源和目的
                intent.setClass(getApplicationContext(), me.class);//跳转的目的地
                startActivity(intent);
            }
        });

        button = findViewById(R.id.classtable);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            //按钮点击后执行下面回调方法
            public void onClick(View v) {
                Intent intent = new Intent();
                //setclass显示意图，直接指定来源和目的
                intent.setClass(getApplicationContext(), ClassTable.class);//跳转的目的地
                startActivity(intent);
            }
        });

        button = findViewById(R.id.show);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            //按钮点击后执行下面回调方法
            public void onClick(View v) {
                Intent intent = new Intent();
                //setclass显示意图，直接指定来源和目的
                intent.setClass(getApplicationContext(), BNaviMainActivity.class);//跳转的目的地
                startActivity(intent);
            }
        });

        


//        //定义Maker坐标点--南苑
//        //以下是南苑的坐标点
//        //南1教学楼
//        LatLng point = new LatLng(39.112298,117.358615);
//        Bundle mBundle = new Bundle();//这一行是后面的点击事件
//
//        //构建Marker图标
//        BitmapDescriptor bitmap = BitmapDescriptorFactory
//                .fromResource(R.drawable.school);
//        //构建MarkerOption，用于在地图上添加Marker
//        OverlayOptions option = new MarkerOptions()
//                .position(point) //必传参数
//                .icon(bitmap) //必传参数
//                .draggable(true)
//        //设置平贴地图，在地图中双指下拉查看效果
//                .flat(true)
//                .alpha(0.5f)
//        //后面的点击事件
//                .extraInfo(mBundle);
//        //在地图上添加Marker，并显示
//        mBaiduMap.addOverlay(option);


//        mBaiduMap.setOnMarkerClickListener(new BaiduMap.OnMarkerClickListener() {
//            //marker被点击时回调的方法
//            //若响应点击事件，返回true，否则返回false
//            //默认返回false
//            @Override
//            public boolean onMarkerClick(Marker marker) {
//                Bundle extraInfo = marker.getExtraInfo(); //通过这个方法获取到，你前面塞的值
//                Toast.makeText(MainActivity.this, "南1教学楼", Toast.LENGTH_SHORT).show();
//                InfoWindow mInfoWindow = new InfoWindow(button, point, -47);
//                mBaiduMap.showInfoWindow(mInfoWindow);
//                return false;
//            }
//        });



        //定义坐标
        List<LatLng> latLngList = new ArrayList<>();
        List<OverlayOptions> optionsList = new ArrayList<>();
        mBaiduMap.clear();//先清除 地图上的覆盖物
        //定义Maker坐标点
        LatLng point = new LatLng(39.112298,117.358615);
        //南院教学楼
        latLngList.add(new LatLng(39.112193,117.359531));//1
        latLngList.add(new LatLng(39.112759,117.358732));//2
        latLngList.add(new LatLng(39.111363,117.359266));//3
        latLngList.add(new LatLng(39.110474,117.359217));//4
        latLngList.add(new LatLng(39.108321,117.360066));//5
        latLngList.add(new LatLng(39.106142,117.363084));//空A
        latLngList.add(new LatLng(39.106404,117.363194));//空B
        latLngList.add(new LatLng(39.10696,117.363457));//空C
        latLngList.add(new LatLng(39.10696,117.363457));//航海科技大厦
        latLngList.add(new LatLng(39.107646,117.362559));//电子信息与自动化学
        //北院教学楼
        latLngList.add(new LatLng(39.117437,117.35455));//北1
        latLngList.add(new LatLng(39.117755,117.353755));//北2
        latLngList.add(new LatLng(39.11786,117.354846));//北3
        latLngList.add(new LatLng(39.11672,117.356387));//北4
        latLngList.add(new LatLng(39.116557,117.355909));//北6
        latLngList.add(new LatLng(39.11724,117.356567));//北8
        latLngList.add(new LatLng(39.12175,117.356234));//北14
        latLngList.add(new LatLng(39.1228,117.358565));//北16
        latLngList.add(new LatLng(39.118786,117.36109));//北19
        latLngList.add(new LatLng(39.117056,117.361164));//北20
        latLngList.add(new LatLng(39.115893,117.361754));//北21
        latLngList.add(new LatLng(39.115432,117.361703));//北22
        latLngList.add(new LatLng(39.115987,117.360279));//北23
        latLngList.add(new LatLng(39.116167,117.35932));//北24
        latLngList.add(new LatLng(39.12252,117.35663));//北25
        //南院宿舍楼
        latLngList.add(new LatLng(39.109831,117.359846));//1
        latLngList.add(new LatLng(39.109505,117.359846));//2
        latLngList.add(new LatLng(39.109831,117.360394));//3
        latLngList.add(new LatLng(39.109505,117.360394));//4
        latLngList.add(new LatLng(39.109131,117.359846));//5
        latLngList.add(new LatLng(39.108795,117.359846));//6
        latLngList.add(new LatLng(39.109131,117.360394));//7
        latLngList.add(new LatLng(39.108795,117.360394));//8
        latLngList.add(new LatLng(39.109831,117.360977));//9
        latLngList.add(new LatLng(39.109505,117.360977));//10
        latLngList.add(new LatLng(39.109131,117.360977));//11
        latLngList.add(new LatLng(39.108795,117.360977));//12
        latLngList.add(new LatLng(39.109137,117.359046));//13
        latLngList.add(new LatLng(39.10885,117.359015));//14
        latLngList.add(new LatLng(39.109928,117.361656));//15
        latLngList.add(new LatLng(39.109548,117.361793));//16
        latLngList.add(new LatLng(39.109214,117.361681));//17
        latLngList.add(new LatLng(39.10923,117.362534));//18
        latLngList.add(new LatLng(39.108878,117.362538));//19
        latLngList.add(new LatLng(39.108294,117.358777));//20
        latLngList.add(new LatLng(39.108005,117.358797));//21
        latLngList.add(new LatLng(39.107442,117.358763));//22
        latLngList.add(new LatLng(39.107191,117.358739));//23
        //北院宿舍楼
        latLngList.add(new LatLng(39.11919,117.354523));//1
        latLngList.add(new LatLng(39.119435,117.354658));//2
        latLngList.add(new LatLng(39.119659,117.354842));//3
        latLngList.add(new LatLng(39.119953,117.354981));//4
        latLngList.add(new LatLng(39.120177,117.35512));//5
        latLngList.add(new LatLng(39.121296,117.355713));//6
        latLngList.add(new LatLng(39.121107,117.357555));//7
        latLngList.add(new LatLng(39.121286,117.357802));//8
        latLngList.add(new LatLng(39.121496,117.357968));//9
        latLngList.add(new LatLng(39.121814,117.358305));//10
        latLngList.add(new LatLng(39.120938,117.358655));//11
        latLngList.add(new LatLng(39.121318,117.358718));//12
        latLngList.add(new LatLng(39.11859,117.35524));//13
        latLngList.add(new LatLng(39.116535,117.357445));//14

        latLngList.add(new LatLng(39.116575,117.357335));//17
        latLngList.add(new LatLng(39.116575,117.357335));//18
        latLngList.add(new LatLng(39.116575,117.357335));//19
        latLngList.add(new LatLng(39.116575,117.357335));//20
        latLngList.add(new LatLng(39.116575,117.357335));//21
        latLngList.add(new LatLng(39.116575,117.357335));//23

        latLngList.add(new LatLng(39.118086,117.358226));//24
        latLngList.add(new LatLng(39.117663,117.357941));//25
        latLngList.add(new LatLng(39.11714,117.35775));//26
        latLngList.add(new LatLng(39.1169,117.357535));//27
        
        //南院食堂
        latLngList.add(new LatLng(39.109673,117.359073));//1
        latLngList.add(new LatLng(39.108135,117.357461));//2
        latLngList.add(new LatLng(39.109613,117.357759));//3
        //北院食堂
        latLngList.add(new LatLng(39.117583,117.355635));//1
        latLngList.add(new LatLng(39.11834,117.357357));//2
        latLngList.add(new LatLng(39.119609,117.355716));//3
        latLngList.add(new LatLng(39.120747,117.358574));//5
        latLngList.add(new LatLng(39.119232,117.355929));//清真

        
        //构建MarkerOption，用于在地图上添加Marker
        int north_school = 1;//北院教学楼
        int south_dormitory = 1;//南院宿舍楼
        int north_dormitory = 1;//北院宿舍楼
        int south_canteen = 1;//南院食堂
        int north_canteen = 1;//北院院食堂
        for (int i = 0; i < 75; i++) {
            Bundle mBundle = new Bundle(); //用来传值 也可以识别点击的是哪一个marker
            if ( i<10 ) { //南院教学楼
                //构建Marker图标
                BitmapDescriptor bitmap = BitmapDescriptorFactory
                        .fromResource(R.drawable.school);
                i = i+1;
                mBundle.putString("title", "南" + i + "教学楼");
                i = i-1;
                mBundle.putDouble("lat", latLngList.get(i).latitude);
                mBundle.putDouble("lng", latLngList.get(i).longitude);

                OverlayOptions option = new MarkerOptions()
                        .extraInfo(mBundle) //这里bundle 跟maker关联上
                        //                    .title("lat="+latLngList.get(i).latitude+"---lng="+latLngList.get(i).longitude) //也可以用title，但是官方提示已经过时了
                        .position(latLngList.get(i)) //设置经纬度
                        .icon(bitmap);//设置marker图标
                optionsList.add(option);
            } else if ( i<25 ){      //北院教学楼
                //构建Marker图标
                BitmapDescriptor bitmap = BitmapDescriptorFactory
                        .fromResource(R.drawable.school);
                mBundle.putString("title", "北" + north_school + "教学楼");
                mBundle.putDouble("lat", latLngList.get(i).latitude);
                mBundle.putDouble("lng", latLngList.get(i).longitude);
                north_school = north_school + 1;

                OverlayOptions option = new MarkerOptions()
                        .extraInfo(mBundle) //这里bundle 跟maker关联上
                        //                    .title("lat="+latLngList.get(i).latitude+"---lng="+latLngList.get(i).longitude) //也可以用title，但是官方提示已经过时了
                        .position(latLngList.get(i)) //设置经纬度
                        .icon(bitmap);//设置marker图标
                optionsList.add(option);
            } else if ( i<48 ){    //南院宿舍楼
                //构建Marker图标
                BitmapDescriptor bitmap = BitmapDescriptorFactory
                        .fromResource(R.drawable.dormitory);
                mBundle.putString("title", "南" + south_dormitory + "宿舍楼");
                mBundle.putDouble("lat", latLngList.get(i).latitude);
                mBundle.putDouble("lng", latLngList.get(i).longitude);
                south_dormitory = south_dormitory + 1;

                OverlayOptions option = new MarkerOptions()
                        .extraInfo(mBundle) //这里bundle 跟maker关联上
                        //                    .title("lat="+latLngList.get(i).latitude+"---lng="+latLngList.get(i).longitude) //也可以用title，但是官方提示已经过时了
                        .position(latLngList.get(i)) //设置经纬度
                        .icon(bitmap);//设置marker图标
                optionsList.add(option);
            } else if ( i < 73){     //北院宿舍楼
                //构建Marker图标
                BitmapDescriptor bitmap = BitmapDescriptorFactory
                        .fromResource(R.drawable.dormitory);
                mBundle.putString("title", "北" + north_dormitory + "宿舍楼");
                mBundle.putDouble("lat", latLngList.get(i).latitude);
                mBundle.putDouble("lng", latLngList.get(i).longitude);
                north_dormitory = north_dormitory + 1;

                OverlayOptions option = new MarkerOptions()
                        .extraInfo(mBundle) //这里bundle 跟maker关联上
                        //                    .title("lat="+latLngList.get(i).latitude+"---lng="+latLngList.get(i).longitude) //也可以用title，但是官方提示已经过时了
                        .position(latLngList.get(i)) //设置经纬度
                        .icon(bitmap);//设置marker图标
                optionsList.add(option);
            } else if (i<76) {
                //构建Marker图标
                BitmapDescriptor bitmap = BitmapDescriptorFactory
                        .fromResource(R.drawable.canteen);
                mBundle.putString("title", "南" + south_canteen + "食堂");
                mBundle.putDouble("lat", latLngList.get(i).latitude);
                mBundle.putDouble("lng", latLngList.get(i).longitude);
                south_canteen = south_canteen + 1;
    
                OverlayOptions option = new MarkerOptions()
                        .extraInfo(mBundle) //这里bundle 跟maker关联上
                        //                    .title("lat="+latLngList.get(i).latitude+"---lng="+latLngList.get(i).longitude) //也可以用title，但是官方提示已经过时了
                        .position(latLngList.get(i)) //设置经纬度
                        .icon(bitmap);//设置marker图标
                optionsList.add(option);
            } else if (i<81) {
                //构建Marker图标
                BitmapDescriptor bitmap = BitmapDescriptorFactory
                        .fromResource(R.drawable.canteen);
                mBundle.putString("title", "北" + north_canteen + "食堂");
                mBundle.putDouble("lat", latLngList.get(i).latitude);
                mBundle.putDouble("lng", latLngList.get(i).longitude);
                north_canteen = north_canteen + 1;
    
                OverlayOptions option = new MarkerOptions()
                        .extraInfo(mBundle) //这里bundle 跟maker关联上
                        //                    .title("lat="+latLngList.get(i).latitude+"---lng="+latLngList.get(i).longitude) //也可以用title，但是官方提示已经过时了
                        .position(latLngList.get(i)) //设置经纬度
                        .icon(bitmap);//设置marker图标
                optionsList.add(option);
            }

        }

        //在地图上添加Marker，并显示
        //        mBaiduMap.addOverlay(option);  添加一个marker使用这个
        mBaiduMap.addOverlays(optionsList); //添加marker list 的话，用这个
        mBaiduMap.setOnMarkerClickListener(new BaiduMap.OnMarkerClickListener() {
            @Override
            public boolean onMarkerClick(Marker marker) {
                Bundle extraInfo = marker.getExtraInfo(); //通过这个方法获取到，你前面塞的值
                String title = extraInfo.getString("title");
                double lat = extraInfo.getDouble("lat");
                double lng = extraInfo.getDouble("lng");
                Toast.makeText(MainActivity.this, title + " " + lat + " " + lng, Toast.LENGTH_SHORT).show();
                return false;
            }
        });
        



//        //南二教学楼
//        LatLng point_south_two_school = new LatLng(39.112193,117.359531);
//        BitmapDescriptor bitmap_south_two_school = BitmapDescriptorFactory
//                .fromResource(R.drawable.school);
//        OverlayOptions option_south_two_school = new MarkerOptions()
//                .position(point_south_two_school)
//                .icon(bitmap_south_two_school)
//                .draggable(true)
//                .flat(true)
//                .alpha(0.5f);
//        mBaiduMap.addOverlay(option_south_two_school);
//
//
//
//        //南三教学楼
//        LatLng point_south_three_school = new LatLng(39.111363,117.359266);
//        BitmapDescriptor bitmap_south_three_school = BitmapDescriptorFactory
//                .fromResource(R.drawable.school);
//        OverlayOptions option_south_three_school = new MarkerOptions()
//                .position(point_south_three_school)
//                .icon(bitmap_south_three_school)
//                .draggable(true)
//                .flat(true)
//                .alpha(0.5f);
//        mBaiduMap.addOverlay(option_south_three_school);
//
//        //南四教学楼
//        LatLng point_south_four_school = new LatLng(39.110474,117.359217);
//        BitmapDescriptor bitmap_south_four_school = BitmapDescriptorFactory
//                .fromResource(R.drawable.school);
//        OverlayOptions option_south_four_school = new MarkerOptions()
//                .position(point_south_four_school)
//                .icon(bitmap_south_four_school)
//                .draggable(true)
//                .flat(true)
//                .alpha(0.5f);
//        mBaiduMap.addOverlay(option_south_four_school);
//
//        //南五教学楼
//        LatLng point_south_five_school = new LatLng(39.108553,117.360111);
//        BitmapDescriptor bitmap_south_five_school = BitmapDescriptorFactory
//                .fromResource(R.drawable.school);
//        OverlayOptions option_south_five_school = new MarkerOptions()
//                .position(point_south_five_school)
//                .icon(bitmap_south_five_school)
//                .draggable(true)
//                .flat(true)
//                .alpha(0.5f);
//        mBaiduMap.addOverlay(option_south_five_school);
//
//        //南一宿舍
//        LatLng point_south_one_domitory = new LatLng(39.109831,117.359846);
//        BitmapDescriptor bitmap_south_one_domitory = BitmapDescriptorFactory
//                .fromResource(R.drawable.dormitory);
//        OverlayOptions option_south_one_domitory = new MarkerOptions()
//                .position(point_south_one_domitory)
//                .icon(bitmap_south_one_domitory)
//                .draggable(true)
//                .flat(true)
//                .alpha(0.5f);
//        mBaiduMap.addOverlay(option_south_one_domitory);
//
//        //南二宿舍
//        LatLng point_south_two_domitory = new LatLng(39.109505,117.359846);
//        BitmapDescriptor bitmap_south_two_domitory = BitmapDescriptorFactory
//                .fromResource(R.drawable.dormitory);
//        OverlayOptions option_south_two_domitory = new MarkerOptions()
//                .position(point_south_two_domitory)
//                .icon(bitmap_south_two_domitory)
//                .draggable(true)
//                .flat(true)
//                .alpha(0.5f);
//        mBaiduMap.addOverlay(option_south_two_domitory);
//
//        //南三宿舍
//        LatLng point_south_three_domitory = new LatLng(39.109831,117.360394);
//        BitmapDescriptor bitmap_south_three_domitory = BitmapDescriptorFactory
//                .fromResource(R.drawable.dormitory);
//        OverlayOptions option_south_three_domitory = new MarkerOptions()
//                .position(point_south_three_domitory)
//                .icon(bitmap_south_three_domitory)
//                .draggable(true)
//                .flat(true)
//                .alpha(0.5f);
//        mBaiduMap.addOverlay(option_south_three_domitory);
//
//        //南四宿舍
//        LatLng point_south_four_domitory = new LatLng(39.109505,117.360394);
//        BitmapDescriptor bitmap_south_four_domitory = BitmapDescriptorFactory
//                .fromResource(R.drawable.dormitory);
//        OverlayOptions option_south_four_domitory = new MarkerOptions()
//                .position(point_south_four_domitory)
//                .icon(bitmap_south_four_domitory)
//                .draggable(true)
//                .flat(true)
//                .alpha(0.5f);
//        mBaiduMap.addOverlay(option_south_four_domitory);
//
//        //南五宿舍
//        LatLng point_south_five_domitory = new LatLng(39.109131,117.359846);
//        BitmapDescriptor bitmap_south_five_domitory = BitmapDescriptorFactory
//                .fromResource(R.drawable.dormitory);
//        OverlayOptions option_south_five_domitory = new MarkerOptions()
//                .position(point_south_five_domitory)
//                .icon(bitmap_south_five_domitory)
//                .draggable(true)
//                .flat(true)
//                .alpha(0.5f);
//        mBaiduMap.addOverlay(option_south_five_domitory);
//
//        //南六宿舍
//        LatLng point_south_six_domitory = new LatLng(39.108795,117.359846);
//        BitmapDescriptor bitmap_south_six_domitory = BitmapDescriptorFactory
//                .fromResource(R.drawable.dormitory);
//        OverlayOptions option_south_six_domitory = new MarkerOptions()
//                .position(point_south_six_domitory)
//                .icon(bitmap_south_six_domitory)
//                .draggable(true)
//                .flat(true)
//                .alpha(0.5f);
//        mBaiduMap.addOverlay(option_south_six_domitory);
//
//        //南七宿舍
//        LatLng point_south_seven_domitory = new LatLng(39.109131,117.360394);
//        BitmapDescriptor bitmap_south_seven_domitory = BitmapDescriptorFactory
//                .fromResource(R.drawable.dormitory);
//        OverlayOptions option_south_seven_domitory = new MarkerOptions()
//                .position(point_south_seven_domitory)
//                .icon(bitmap_south_seven_domitory)
//                .draggable(true)
//                .flat(true)
//                .alpha(0.5f);
//        mBaiduMap.addOverlay(option_south_seven_domitory);
//
//        //南八宿舍
//        LatLng point_south_eight_domitory = new LatLng(39.108795,117.360394);
//        BitmapDescriptor bitmap_south_eight_domitory = BitmapDescriptorFactory
//                .fromResource(R.drawable.dormitory);
//        OverlayOptions option_south_eight_domitory = new MarkerOptions()
//                .position(point_south_eight_domitory)
//                .icon(bitmap_south_eight_domitory)
//                .draggable(true)
//                .flat(true)
//                .alpha(0.5f);
//        mBaiduMap.addOverlay(option_south_eight_domitory);
//
//        //南九宿舍
//        LatLng point_south_nine_domitory = new LatLng(39.109831,117.360977);
//        BitmapDescriptor bitmap_south_nine_domitory = BitmapDescriptorFactory
//                .fromResource(R.drawable.dormitory);
//        OverlayOptions option_south_nine_domitory = new MarkerOptions()
//                .position(point_south_nine_domitory)
//                .icon(bitmap_south_nine_domitory)
//                .draggable(true)
//                .flat(true)
//                .alpha(0.5f);
//        mBaiduMap.addOverlay(option_south_nine_domitory);
//
//        //南十宿舍
//        LatLng point_south_ten_domitory = new LatLng(39.109505,117.360977);
//        BitmapDescriptor bitmap_south_ten_domitory = BitmapDescriptorFactory
//                .fromResource(R.drawable.dormitory);
//        OverlayOptions option_south_ten_domitory = new MarkerOptions()
//                .position(point_south_ten_domitory)
//                .icon(bitmap_south_ten_domitory)
//                .draggable(true)
//                .flat(true)
//                .alpha(0.5f);
//        mBaiduMap.addOverlay(option_south_ten_domitory);
//
//        //南十一宿舍
//        LatLng point_south_eleven_domitory = new LatLng(39.109131,117.360977);
//        BitmapDescriptor bitmap_south_eleven_domitory = BitmapDescriptorFactory
//                .fromResource(R.drawable.dormitory);
//        OverlayOptions option_south_eleven_domitory = new MarkerOptions()
//                .position(point_south_eleven_domitory)
//                .icon(bitmap_south_eleven_domitory)
//                .draggable(true)
//                .flat(true)
//                .alpha(0.5f);
//        mBaiduMap.addOverlay(option_south_eleven_domitory);
//
//        //南十二宿舍
//        LatLng point_south_twelve_domitory = new LatLng(39.108795,117.360977);
//        BitmapDescriptor bitmap_south_twelve_domitory = BitmapDescriptorFactory
//                .fromResource(R.drawable.dormitory);
//        OverlayOptions option_south_twelve_domitory = new MarkerOptions()
//                .position(point_south_twelve_domitory)
//                .icon(bitmap_south_twelve_domitory)
//                .draggable(true)
//                .flat(true)
//                .alpha(0.5f);
//        mBaiduMap.addOverlay(option_south_twelve_domitory);
//
//        //南十三宿舍
//        LatLng point_south_thirteen_domitory = new LatLng(39.109134,117.359037);
//        BitmapDescriptor bitmap_south_thirteen_domitory = BitmapDescriptorFactory
//                .fromResource(R.drawable.dormitory);
//        OverlayOptions option_south_thirteen_domitory = new MarkerOptions()
//                .position(point_south_thirteen_domitory)
//                .icon(bitmap_south_thirteen_domitory)
//                .draggable(true)
//                .flat(true)
//                .alpha(0.5f);
//        mBaiduMap.addOverlay(option_south_thirteen_domitory);
//
//        //南14宿舍
//        LatLng point_south_fourteen_domitory = new LatLng(39.108809,117.359037);
//        BitmapDescriptor bitmap_south_fourteen_domitory = BitmapDescriptorFactory
//                .fromResource(R.drawable.dormitory);
//        OverlayOptions option_south_fourteen_domitory = new MarkerOptions()
//                .position(point_south_fourteen_domitory)
//                .icon(bitmap_south_fourteen_domitory)
//                .draggable(true)
//                .flat(true)
//                .alpha(0.5f);
//        mBaiduMap.addOverlay(option_south_fourteen_domitory);

        //南15宿舍
        LatLng point_south_fifteen_domitory = new LatLng(39.109131,117.359846);
        BitmapDescriptor bitmap_south_fifteen_domitory = BitmapDescriptorFactory
                .fromResource(R.drawable.dormitory);
        OverlayOptions option_south_fifteen_domitory = new MarkerOptions()
                .position(point_south_fifteen_domitory)
                .icon(bitmap_south_fifteen_domitory)
                .draggable(true)
                .flat(true)
                .alpha(0.5f);
        mBaiduMap.addOverlay(option_south_fifteen_domitory);
    }



    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        switch (requestCode) {
            case 1:
                if (grantResults.length>0) {
                    for (int result:grantResults) {
                        if (result !=PackageManager.PERMISSION_GRANTED) {
                            Toast.makeText(this,"必须统一所有的权限才能使用本程序",Toast.LENGTH_SHORT).show();
                            finish();
                            return;
                        }
                    }
                    requestLocation();
                } else {
                    Toast.makeText(this,"发生未知错误",Toast.LENGTH_SHORT).show();
                    finish();
                }
                break;
            case 2:
        }
    }

    private void requestLocation() {
        initLocation();//对监听的地理位置的参数
        mLocationClient.start();
    }

    private void initLocation() {
        //通过LocationClientOption设置LocationClient相关参数
        LocationClientOption option = new LocationClientOption();
        option.setOpenGps(true); // 打开gps
        option.setCoorType("bd09ll"); // 设置坐标类型
        option.setScanSpan(1000);
        option.setLocationMode(LocationClientOption.LocationMode.Battery_Saving);//设置的高精度模式
        option.setLocationNotify(true);//设置是否当GPS有效时按照1s/1次频率输出GPS结果
        option.setIsNeedAddress(true);//显示地理位置信息;

        //设置locationClientOption
        mLocationClient.setLocOption(option);
    }

    @Override
    public void onPointerCaptureChanged(boolean hasCapture) {
        super.onPointerCaptureChanged(hasCapture);
    }


    private class MyLocationListener extends BDAbstractLocationListener {

        @Override
        public void onReceiveLocation(BDLocation location) {
            navigateTo(location);

//            这个是显示信息
//            StringBuilder currentPosition = new StringBuilder();
//            currentPosition.append("纬度：").append(location.getLongitude()).append("\n");
//            currentPosition.append("经度：").append(location.getLatitude()).append("\n");
//            currentPosition.append("国家：").append(location.getCountry()).append("\n");
//            currentPosition.append("省：").append(location.getProvince()).append("\n");
//            currentPosition.append("市：").append(location.getCity()).append("\n");
//            currentPosition.append("区：").append(location.getDistrict()).append("\n");
//            currentPosition.append("村镇：").append(location.getTown()).append("\n");
//            currentPosition.append("街道：").append(location.getStreet()).append("\n");
//            currentPosition.append("地址：").append(location.getAddrStr()).append("\n");
//            currentPosition.append("定位方式：");
//            if (location.getLocType()==BDLocation.TypeGpsLocation) {
//                currentPosition.append("GPS");
//            } else if (location.getLocType()==BDLocation.TypeNetWorkLocation) {
//                currentPosition.append("网络");
//            }
//            locationInfo.setText(currentPosition);
        }
    }

    //缩放？
    private void navigateTo(BDLocation location) {
        if (isFirstLocate) {
            LatLng ll = new LatLng(location.getLatitude(),location.getLongitude());
            MapStatusUpdate update = MapStatusUpdateFactory.newLatLng(ll);
            mBaiduMap.animateMapStatus(update);//更新
            update = MapStatusUpdateFactory.zoomTo(16f);
            mBaiduMap.animateMapStatus(update);
            isFirstLocate = false;
        }

        MyLocationData.Builder locationBuilder = new MyLocationData.Builder();
        locationBuilder.longitude(location.getLongitude());
        locationBuilder.latitude(location.getLatitude());
        MyLocationData locationData = locationBuilder.build();
        mBaiduMap.setMyLocationData(locationData);

    }

    @Override
    public void onClick(View view) {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("展示");
        builder.setMessage("12345");
        builder.setPositiveButton("关闭", (dialogInterface, i) -> {
        });
        AlertDialog dialog= builder.create();
        dialog.show();
    }

    //相应的生命周期
    @Override
    protected void onDestroy() {
        super.onDestroy();
        mMapView.onDestroy();
        mBaiduMap.setMyLocationEnabled(false);
        mLocationClient.stop();
    }

    @Override
    protected void onResume() {
        super.onResume();
        //在activity执行onResume时执行mMapView. onResume ()，实现地图生命周期管理
        mMapView.onResume();
    }

    @Override
    protected void onPause() {
        super.onPause();
        //在activity执行onPause时执行mMapView. onPause ()，实现地图生命周期管理
        mMapView.onPause();
    }


    

}