package com.example.v001ff.footmark;

import android.Manifest;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.ActivityCompat;
import android.support.v4.app.FragmentActivity;

import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;


public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {

    private GoogleMap mMap;

    private final int REQUEST_PERMISSION = 1000;


    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_maps);
        if (Build.VERSION.SDK_INT >= 23)
            checkPermission();
        else
            start();
    }

    public void start() {
        SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
                .findFragmentById(R.id.map);
        mapFragment.getMapAsync(this);
    }


    /**
     * 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.
     */

    private void setUpMap() {
        mMap.addMarker(new MarkerOptions().position(new LatLng(0, 0)).title("Marker"));
        mMap.setMyLocationEnabled(true);
        if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            // TODO: Consider calling
            //    ActivityCompat#requestPermissions
            // here to request the missing permissions, and then overriding
            //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
            //                                          int[] grantResults)
            // to handle the case where the user grants the permission. See the documentation
            // for ActivityCompat#requestPermissions for more details.
            return;
        }
    }

    @Override
    //開いたときに実行される関数
    public void onMapReady(GoogleMap googleMap) {
        mMap = googleMap;
        LatLng yu = new LatLng(33.956416, 131.2725288);
        mMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);
        mMap.addMarker(new MarkerOptions().position(yu).title("Marker in YU"));
        mMap.moveCamera(CameraUpdateFactory.newLatLng(yu));

        if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            // TODO: Consider calling
            //    ActivityCompat#requestPermissions
            // here to request the missing permissions, and then overriding
            //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
            //                                          int[] grantResults)
            // to handle the case where the user grants the permission. See the documentation
            // for ActivityCompat#requestPermissions for more details.
            return;
        }
        mMap.setMyLocationEnabled(true);
    }

    public void checkPermission() {
        if(ActivityCompat.checkSelfPermission(this,Manifest.permission.ACCESS_FINE_LOCATION)
                == PackageManager.PERMISSION_GRANTED)
            start();
        else
            ActivityCompat.requestPermissions(this,new String[]
                            {Manifest.permission.ACCESS_FINE_LOCATION,},
                    REQUEST_PERMISSION);

    }

    public void onRequestPermissionsResult(int requestCode,String[] permissions,
                                           int[] grantResults) {
        if((requestCode == REQUEST_PERMISSION)&&
                (grantResults[0] == PackageManager.
                        PERMISSION_GRANTED))
            start();
    }
}


@@ -675,17 +675,16 @@
 p mMap;%0A
-%0A
     priv
@@ -725,17 +725,16 @@
  1000;%0A%0A
-%0A
     prot
@@ -1734,965 +1734,8 @@
 */%0A%0A
-    private void setUpMap() %7B%0A        mMap.addMarker(new MarkerOptions().position(new LatLng(0, 0)).title(%22Marker%22));%0A        mMap.setMyLocationEnabled(true);%0A        if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) %7B%0A            // TODO: Consider calling%0A            //    ActivityCompat#requestPermissions%0A            // here to request the missing permissions, and then overriding%0A            //   public void onRequestPermissionsResult(int requestCode, String%5B%5D permissions,%0A            //                                          int%5B%5D grantResults)%0A            // to handle the case where the user grants the permission. See the documentation%0A            // for ActivityCompat#requestPermissions for more details.%0A            return;%0A        %7D%0A    %7D%0A
 %0A   
@@ -3655,8 +3655,970 @@
    %7D%0A%7D%0A%0A
+/*%0A  private void setUpMap() %7B%0A        mMap.addMarker(new MarkerOptions().position(new LatLng(0, 0)).title(%22Marker%22));%0A        mMap.setMyLocationEnabled(true);%0A        if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) %7B%0A            // TODO: Consider calling%0A            //    ActivityCompat#requestPermissions%0A            // here to request the missing permissions, and then overriding%0A            //   public void onRequestPermissionsResult(int requestCode, String%5B%5D permissions,%0A            //                                          int%5B%5D grantResults)%0A            // to handle the case where the user grants the permission. See the documentation%0A            // for ActivityCompat#requestPermissions for more details.%0A            return;%0A        %7D%0A    %7D%0A */%0A

change Matsunaga