package lwk.hellomap;

import android.Manifest;
import android.content.Context;
import android.content.pm.PackageManager;
import android.graphics.Color;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.support.v4.app.FragmentActivity;
import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.widget.Toast;

import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.GoogleMapOptions;
import com.google.android.gms.maps.MapFragment;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.UiSettings;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.CameraPosition;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.Marker;
import com.google.android.gms.maps.model.MarkerOptions;
import com.google.android.gms.maps.model.Polyline;
import com.google.android.gms.maps.model.PolylineOptions;

public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {

    private GoogleMap mMap;
    private LocationManager locationManager;
    private LocationListener locationListener;
    private LatLng latLng;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_maps);
        // Obtain the SupportMapFragment and get notified when the map is ready to be used.
        SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
                .findFragmentById(R.id.map);
        mapFragment.getMapAsync(this);

        //设置地图属性
        GoogleMapOptions options = new GoogleMapOptions();
        options.mapType(GoogleMap.MAP_TYPE_SATELLITE)
                .compassEnabled(false)
                .rotateGesturesEnabled(false)
                .tiltGesturesEnabled(false);
    }

      //My Location的当前位置显示函数

//    private void showLocation(LatLng latlng){
//        if(latlng != null){
//            Toast.makeText(MapsActivity.this, "经度：" + latlng.longitude + '\n' + "纬度："
//                    + latlng.latitude, Toast.LENGTH_LONG).show();
//            mMap.moveCamera(CameraUpdateFactory.newLatLng(latlng));
//        }else{
//            Toast.makeText(MapsActivity.this,"无法获得位置",Toast.LENGTH_SHORT).show();
//        }
//    }

    //标记的当前位置显示函数
    private void showLocation(LatLng latlng){
        if(latlng != null){
            mMap.addMarker(new MarkerOptions()
                    .position(latlng)
                    .title("当前位置"));
            mMap.moveCamera(CameraUpdateFactory.newLatLng(latlng));
        }else{
        }
    }


    /**
     * Manipulates the map once available.
     * This callback is triggered when the map is ready to be used.
     * This is where we can add markers or lines, add listeners or move the camera. In this case,
     * we just add a marker near Sydney, Australia.
     * If Google Play services is not installed on the device, the user will be prompted to install
     * it inside the SupportMapFragment. This method will only be triggered once the user has
     * installed Google Play services and returned to the app.
     */
    @Override
    public void onMapReady(GoogleMap googleMap) {
        mMap = googleMap;

        // Add a marker in Sydney and move the camera
        // 标记
//        LatLng sydney = new LatLng(-34, 151);
//        mMap.addMarker(new MarkerOptions().position(sydney)
//                .title("Marker in Sydney"))
//                .setSnippet("Population: *** ");

//        Marker marker = mMap.addMarker(new MarkerOptions().position(sydney)
//                .title("Marker in Sydney"));
//
//        marker.showInfoWindow();
//        marker.hideInfoWindow();

//                //设置可拖动
//                .setDraggable(true);
                //设置标记颜色
//                .setIcon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_CYAN));
                //自定义图片设置标记
//                .setIcon(BitmapDescriptorFactory.fromResource(R.mipmap.marker));

//        mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));


//        //设置摄像头的位置和缩放
//        LatLng cali = new LatLng(3.4383,-76.5161);
//        CameraPosition cameraPosition = CameraPosition.builder()
//                .target(cali)       //目的地
//                .zoom(10)           //缩放级别
//                .build();
//        googleMap.moveCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));


        //设置地图类型
//        googleMap.setMapType(GoogleMap.MAP_TYPE_HYBRID );
//        googleMap.setMapType(GoogleMap.MAP_TYPE_TERRAIN);
//        googleMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);


//        //设置手势控制
//        UiSettings uiSettings = googleMap.getUiSettings();
//        uiSettings.setAllGesturesEnabled(false);            //设置所有手势控制不可用

//        LatLng Guangzhou = new LatLng(23.016,113.23);
//        CameraPosition cameraPosition = CameraPosition.builder()
//                .target(Guangzhou)  //目的地
//                .zoom(10)           //缩放级别
//                .build();
//        googleMap.moveCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));

        //My location
        if(ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION)
                == PackageManager.PERMISSION_GRANTED){
            googleMap.setMyLocationEnabled(true);
        }else{
            //show error
        }
        googleMap.getUiSettings().setZoomControlsEnabled(true);


//        //地图定位
//        locationManager = (LocationManager)getSystemService(LOCATION_SERVICE); //获取系统服务
//        //定义回调函数
//        locationListener = new LocationListener() {
//            @Override
//            public void onLocationChanged(Location location) {
//                latLng = new LatLng(location.getLatitude(),location.getLongitude());
//                showLocation(latLng);
//            }
//            @Override
//            public void onStatusChanged(String provider, int status, Bundle extras) {
//                //在提供定位功能的硬件的状态改变时调用
//            }
//            @Override
//            public void onProviderEnabled(String provider) {
//                //在用户启用具有定位功能的硬件时被调用
//            }
//            @Override
//            public void onProviderDisabled(String provider) {
//                //在用户禁用具有定位功能的硬件时调用
//            }
//        };
//        locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,2000,10,locationListener);

        //向地图添加矩形
        PolylineOptions sudamericaRect = new PolylineOptions()
                .add(new LatLng(12.897489,-82.441406))  //P1
                .add(new LatLng(12.897489,-32.167969))  //P2
                .add(new LatLng(-55.37911,-32.167969))  //P3
                .add(new LatLng(-55.37911,-82.441406))  //P4
                .add(new LatLng(12.897489,-82.441406))  //P1
                .color(Color.parseColor("#f44336"));  //Red 500
        Polyline polyline = mMap.addPolyline(sudamericaRect);
        mMap.moveCamera(CameraUpdateFactory.newLatLng(new LatLng(-20.96144,-61.347656)));
    }
}
