Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/57.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 由于运行时问题,Google Maps API无法在API 23以上运行_Android_Google Maps Markers_Android 6.0 Marshmallow_Google Maps Api 2 - Fatal编程技术网

Android 由于运行时问题,Google Maps API无法在API 23以上运行

Android 由于运行时问题,Google Maps API无法在API 23以上运行,android,google-maps-markers,android-6.0-marshmallow,google-maps-api-2,Android,Google Maps Markers,Android 6.0 Marshmallow,Google Maps Api 2,问题:Maps API在API 23下运行良好,但由于运行时问题,它们在第一次运行时无法获得权限。他们第一次请求权限,但第一次不工作。当我第二次运行该应用程序时,它在23以上的API上也能正常工作。但对于第一次运行,它不起作用。这段代码应该做的,第二次做的,是要求用户一个接一个地放置两个标记,并使用用户选择的坐标对其进行反向地理编码 import android.Manifest; import android.content.DialogInterface; import android.co

问题:Maps API在API 23下运行良好,但由于运行时问题,它们在第一次运行时无法获得权限。他们第一次请求权限,但第一次不工作。当我第二次运行该应用程序时,它在23以上的API上也能正常工作。但对于第一次运行,它不起作用。这段代码应该做的,第二次做的,是要求用户一个接一个地放置两个标记,并使用用户选择的坐标对其进行反向地理编码

import android.Manifest;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.location.Address;
import android.location.Geocoder;
import android.location.Location;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.ActivityCompat;
import android.support.v4.app.FragmentActivity;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AlertDialog;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.location.LocationListener;
import com.google.android.gms.location.LocationRequest;
import com.google.android.gms.location.LocationServices;
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.Marker;
import com.google.android.gms.maps.model.MarkerOptions;

import java.io.IOException;
import java.util.List;
import java.util.Locale;


public class MapsActivity extends FragmentActivity implements OnMapReadyCallback,
        GoogleApiClient.ConnectionCallbacks,
        GoogleApiClient.OnConnectionFailedListener,
        LocationListener {

    private GoogleMap mMap;
    GoogleApiClient mGoogleApiClient;
    Location mLastLocation;
    Marker mCurrLocationMarker;
    LocationRequest mLocationRequest;
    public Button homeLocationFromPin;
    public Button officeLocationFromPin;
    public static double homeLatitude;
    public static double officeLatitude;
    public static double homeLongitude;
    public static double officeLongitude;
    public int count = 0;
    public MarkerOptions placedHomeMarker;
    public MarkerOptions placedOfficeMarker;
    public static final int LOCATION_REQUEST_CODE = 99;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_maps);

        if (ActivityCompat.checkSelfPermission
                (this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED
                && ActivityCompat.checkSelfPermission
                (this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {

            requestPermission(Manifest.permission.ACCESS_FINE_LOCATION,
                    LOCATION_REQUEST_CODE);
        }



        // Obtain the SupportMapFragment and get notified when the map is ready to be used.
        SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(map);
        mapFragment.getMapAsync(this);

        homeLocationFromPin = (Button) findViewById(R.id.confirmHomeLocation);
        homeLocationFromPin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (String.valueOf(homeLongitude) != null && String.valueOf(homeLatitude) != null) {
                    //Get nearby Places here based on Latitude and Longitude
//                    Toast.makeText(MapsActivity.this, getAddress(homeLatitude, homeLongitude), Toast.LENGTH_LONG).show();
                    showAlertBoxForHomeLocationConfirmation(getAddress(homeLatitude, homeLongitude));
                } else {
                    Toast.makeText(MapsActivity.this, "Please drop pin to home location first", Toast.LENGTH_SHORT).show();
                }
            }
        });


        officeLocationFromPin = (Button) findViewById(R.id.confirmOfficeLocation);
        officeLocationFromPin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (String.valueOf(officeLatitude) != null && String.valueOf(officeLongitude) != null) {
                    //Get nearby Places here based on Latitude and Longitude
//                    Toast.makeText(MapsActivity.this, getAddress(homeLatitude, homeLongitude), Toast.LENGTH_LONG).show();
                    showAlertBoxForOfficeLocationConfirmation(getAddress(officeLatitude, officeLongitude));
                } else {
                    Toast.makeText(MapsActivity.this, "Please drop pin to home location first", Toast.LENGTH_SHORT).show();
                }
            }
        });


    }//End Of OnCreate

    @Override
    public void onMapReady(GoogleMap googleMap) {
        mMap = googleMap;
        mMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);
        LatLng boston = new LatLng(42.3601, -71.0589);
        mMap.moveCamera(CameraUpdateFactory.newLatLng(boston));
        mMap.animateCamera(CameraUpdateFactory.zoomTo(15));

        showAlertBox("Home");

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {

            if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
                buildGoogleApiClient();
                mMap.setMyLocationEnabled(true);
                Toast.makeText(this, "Inside Perm", Toast.LENGTH_LONG).show();
                mMap.setOnMapClickListener(new GoogleMap.OnMapClickListener() {
                    @Override
                    public void onMapClick(LatLng latLng) {

                        if (count == 0) {
                            //Make the confirm button available on first tap
                            homeLocationFromPin.setVisibility(View.VISIBLE);
                            //add(point);
                            mMap.clear();
                            placedHomeMarker = new MarkerOptions().position(latLng);
                            placedHomeMarker.title("Home Location");
                            mMap.addMarker(placedHomeMarker);
                            homeLatitude = latLng.latitude;
                            homeLongitude = latLng.longitude;
                            Toast.makeText(MapsActivity.this, getNameOnly(homeLatitude, homeLongitude), Toast.LENGTH_SHORT).show();
                        } else {
                            officeLocationFromPin.setVisibility(View.VISIBLE);
                            placedOfficeMarker = new MarkerOptions().position(latLng);
                            placedOfficeMarker.title("Office Location");
                            mMap.addMarker(placedOfficeMarker);
                            officeLatitude = latLng.latitude;
                            officeLongitude = latLng.longitude;
                            Toast.makeText(MapsActivity.this, getNameOnly(homeLatitude, homeLongitude), Toast.LENGTH_SHORT).show();
                            Toast.makeText(MapsActivity.this, getNameOnly(officeLatitude, officeLongitude), Toast.LENGTH_SHORT).show();
                        }
                    }
                });
            } else {

            buildGoogleApiClient();
            mMap.setMyLocationEnabled(true);
            mMap.setOnMapClickListener(new GoogleMap.OnMapClickListener() {
                @Override
                public void onMapClick(LatLng latLng) {
                    if (count == 0) {
                        //Make the confirm button available on first tap
                        homeLocationFromPin.setVisibility(View.VISIBLE);
                        //add(point);
                        mMap.clear();
                        placedHomeMarker = new MarkerOptions().position(latLng);
                        placedHomeMarker.title("Home Location");
                        mMap.addMarker(placedHomeMarker);
                        homeLatitude = latLng.latitude;
                        homeLongitude = latLng.longitude;
                        Toast.makeText(MapsActivity.this, getNameOnly(homeLatitude, homeLongitude), Toast.LENGTH_SHORT).show();
                    } else {
                        officeLocationFromPin.setVisibility(View.VISIBLE);
                        placedOfficeMarker = new MarkerOptions().position(latLng);
                        placedOfficeMarker.title("Office Location");
                        mMap.addMarker(placedOfficeMarker);
                        officeLatitude = latLng.latitude;
                        officeLongitude = latLng.longitude;
                        Toast.makeText(MapsActivity.this, getNameOnly(homeLatitude, homeLongitude), Toast.LENGTH_SHORT).show();
                        Toast.makeText(MapsActivity.this, getNameOnly(officeLatitude, officeLongitude), Toast.LENGTH_SHORT).show();
                    }
                }
            });
        }
        }
    }//End of onMap Ready

    protected synchronized void buildGoogleApiClient() {
        mGoogleApiClient = new GoogleApiClient.Builder(this)
                .addConnectionCallbacks(this)
                .addOnConnectionFailedListener(this)
                .addApi(LocationServices.API)
                .build();
        mGoogleApiClient.connect();
    }

    @Override
    public void onConnected(Bundle bundle) {

        mLocationRequest = new LocationRequest();
        mLocationRequest.setInterval(1000);
        mLocationRequest.setFastestInterval(1000);
        mLocationRequest.setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY);
        if (ContextCompat.checkSelfPermission(this,
                Manifest.permission.ACCESS_FINE_LOCATION)
                == PackageManager.PERMISSION_GRANTED) {
            LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this);
        }
    }

    @Override
    public void onConnectionSuspended(int i) {

    }


    @Override
    public void onLocationChanged(Location location) {

        mLastLocation = location;
        if (mCurrLocationMarker != null) {
            mCurrLocationMarker.remove();
        }

        //Place current location marker
        LatLng latLng = new LatLng(location.getLatitude(), location.getLongitude());
        MarkerOptions markerOptions = new MarkerOptions();
        markerOptions.position(latLng);
//        markerOptions.title("Your Current Location");
//        markerOptions.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_MAGENTA));
//        markerOptions.icon(BitmapDescriptorFactory.fromResource(R.drawable.home));
//        markerOptions.draggable(true);
        mCurrLocationMarker = mMap.addMarker(markerOptions);

        //move map camera
        mMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
        mMap.animateCamera(CameraUpdateFactory.zoomTo(15));

        //Set a marker drag listener
        mMap.setOnMarkerDragListener(new GoogleMap.OnMarkerDragListener() {
            @Override
            public void onMarkerDragStart(Marker marker) {

            }

            @Override
            public void onMarkerDrag(Marker marker) {

            }

            @Override
            public void onMarkerDragEnd(Marker marker) {
                mMap.animateCamera(CameraUpdateFactory.newLatLng(marker.getPosition()));
                //Get the latitude and longitude of the place where user dropped the marker
                Toast.makeText(MapsActivity.this, "Latitude:" + String.valueOf(marker.getPosition().latitude), Toast.LENGTH_LONG).show();
                homeLatitude = marker.getPosition().latitude;
                Toast.makeText(MapsActivity.this, "Longitude:" + String.valueOf(marker.getPosition().longitude), Toast.LENGTH_LONG).show();
                homeLongitude = marker.getPosition().longitude;

            }
        });


        //stop location updates
        if (mGoogleApiClient != null) {
            LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient, this);
        }

    }

    @Override
    public void onConnectionFailed(ConnectionResult connectionResult) {
    }


    public void showAlertBox(String LocationName) {

        AlertDialog.Builder b1 = new AlertDialog.Builder(MapsActivity.this);
        b1.setMessage("Please select your " + LocationName + " location.\nTap anywhere on the screen to select your "
                + LocationName +
                "location and press button to confirm.");
        b1.setCancelable(false);

        b1.setPositiveButton("Ok",
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {

                    }
                });

        AlertDialog customAlertBox = b1.create();
        customAlertBox.show();

    }


    public void showAlertBoxForHomeLocationConfirmation(String reverseGeoStr) {

        AlertDialog.Builder b1 = new AlertDialog.Builder(MapsActivity.this);
        b1.setMessage("Confirm your home location or search again\n" + reverseGeoStr);
        b1.setCancelable(false);

        b1.setPositiveButton("Confirm",
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        count = 3;

                        homeLocationFromPin.setVisibility(View.GONE);
                        showAlertBox("Office");
                    }
                });

        b1.setNegativeButton("Do it again",
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        Toast.makeText(MapsActivity.this, "Place Pin Again", Toast.LENGTH_SHORT).show();
                    }
                });
        AlertDialog customAlertBox = b1.create();
        customAlertBox.show();

    }


    public void showAlertBoxForOfficeLocationConfirmation(String reverseGeoStr) {

        AlertDialog.Builder b1 = new AlertDialog.Builder(MapsActivity.this);
        b1.setMessage("Confirm your Office location or search again\n" + reverseGeoStr);
        b1.setCancelable(false);

        b1.setPositiveButton("Confirm",
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        Intent st = new Intent(MapsActivity.this, getUserName.class);
                        startActivity(st);
                        finish();
                    }
                });

        b1.setNegativeButton("Do it again",
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        Toast.makeText(MapsActivity.this, "Place Pin Again", Toast.LENGTH_SHORT).show();
                    }
                });
        AlertDialog customAlertBox = b1.create();
        customAlertBox.show();

    }


    private String getAddress(double latitude, double longitude) {
        if (latitude == 0.0 || longitude == 0.0) {
            //Give default values so it does not returns null
            latitude = 42.3601;
            longitude = -71.0589;
        }
        StringBuilder result = new StringBuilder();
        try {
            Geocoder geocoder = new Geocoder(this, Locale.getDefault());
            List<Address> addresses = geocoder.getFromLocation(latitude, longitude, 1);
            if (addresses.size() > 0) {
                Address address = addresses.get(0);
                result.append(address.getAddressLine(0)).append("\n");
                result.append(address.getLocality()).append("\n");
                result.append(address.getAdminArea()).append("\n");
                result.append(address.getPostalCode()).append("\n");
                result.append(address.getCountryName());
            }
        } catch (IOException e) {
            Log.e("tag", e.getMessage());
        }

        return result.toString();
    }

    private String getNameOnly(double latitude, double longitude) {
        if (latitude == 0.0 || longitude == 0.0) {
            //Give default values so it does not returns null
            latitude = 42.3601;
            longitude = -71.0589;
        }
        StringBuilder result = new StringBuilder();
        try {
            Geocoder geocoder = new Geocoder(this, Locale.getDefault());
            List<Address> addresses = geocoder.getFromLocation(latitude, longitude, 1);
            if (addresses.size() > 0) {
                Address address = addresses.get(0);
                result.append(address.getAddressLine(0)).append("\n");
                result.append(address.getLocality()).append("\n");
            }
        } catch (IOException e) {
            Log.e("tag", e.getMessage());
        }
        return result.toString();
    }


    protected void requestPermission(String permissionType, int requestCode) {
        int permission = ContextCompat.checkSelfPermission(this,
                permissionType);

        if (permission != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(this,
                    new String[]{permissionType}, requestCode
            );
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode,
                                           String permissions[], int[] grantResults) {
        switch (requestCode) {
            case LOCATION_REQUEST_CODE: {

                if (grantResults.length == 0
                        || grantResults[0] != PackageManager.PERMISSION_GRANTED) {
                    Toast.makeText(this, "Unable to show location - permission required", Toast.LENGTH_LONG).show();
                }
                return;
            }
        }
    }

}
导入android.Manifest;
导入android.content.DialogInterface;
导入android.content.Intent;
导入android.content.pm.PackageManager;
导入android.location.Address;
导入android.location.Geocoder;
导入android.location.location;
导入android.os.Build;
导入android.os.Bundle;
导入android.support.v4.app.ActivityCompat;
导入android.support.v4.app.FragmentActivity;
导入android.support.v4.content.ContextCompat;
导入android.support.v7.app.AlertDialog;
导入android.util.Log;
导入android.view.view;
导入android.widget.Button;
导入android.widget.Toast;
导入com.google.android.gms.common.ConnectionResult;
导入com.google.android.gms.common.api.GoogleAppClient;
导入com.google.android.gms.location.LocationListener;
导入com.google.android.gms.location.LocationRequest;
导入com.google.android.gms.location.LocationServices;
导入com.google.android.gms.maps.CameraUpdateFactory;
导入com.google.android.gms.maps.GoogleMap;
导入com.google.android.gms.maps.OnMapReadyCallback;
导入com.google.android.gms.maps.SupportMapFragment;
导入com.google.android.gms.maps.model.LatLng;
导入com.google.android.gms.maps.model.Marker;
导入com.google.android.gms.maps.model.MarkerOptions;
导入java.io.IOException;
导入java.util.List;
导入java.util.Locale;
公共类MapsActivity扩展了FragmentActivity在MapReadyCallback上的实现,
GoogleAppClient.ConnectionCallbacks,
GoogleAppClient.OnConnectionFailedListener,
位置侦听器{
私有谷歌地图;
GoogleapClient MGoogleapClient;
位置mLastLocation;
标记器mCurrLocationMarker;
位置请求mLocationRequest;
公共按钮homeLocationFromPin;
公共按钮的位置从PIN;
公共静态双纬度;
公共静态双官化;
公共静态双经度;
公共静态双重办公;
公共整数计数=0;
公共标记放置在家庭标记上的选项;
公共市场选择地点办公室市场;
公共静态最终整数位置\请求\代码=99;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_映射);
如果(ActivityCompat.checkSelfPermission
(此,Manifest.permission.ACCESS\u FINE\u LOCATION)!=PackageManager.permission\u已授予
&&ActivityCompat.checkSelfPermission
(此,Manifest.permission.ACCESS\u\u位置)!=PackageManager.permission\u已授予){
requestPermission(Manifest.permission.ACCESS\u FINE\u位置,
位置(请求代码);
}
//获取SupportMapFragment,并在地图准备好使用时收到通知。
SupportMapFragment mapFragment=(SupportMapFragment)getSupportFragmentManager();
getMapAsync(这个);
homeLocationFromPin=(按钮)findViewById(R.id.confirmHomeLocation);
homeLocationFromPin.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图){
if(String.valueOf(homeLatitude)!=null和String.valueOf(homeLatitude)!=null){
//根据纬度和经度在这里找到附近的地方
//Toast.makeText(MapsActivity.this、getAddress(homeLatitude、homeLatitude)、Toast.LENGTH_LONG.show();
showAlertBoxForHomeLocationConfirmation(获取地址(homeLatitude,homeLatitude));
}否则{
Toast.makeText(MapsActivity.this,“请先将pin放置到主位置”,Toast.LENGTH_SHORT.show();
}
}
});
officeLocationFromPin=(按钮)findViewById(R.id.confirmOfficeLocation);
officeLocationFromPin.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图){
if(String.valueOf(officeLatitude)!=null和String.valueOf(officeLongitude)!=null){
//根据纬度和经度在这里找到附近的地方
//Toast.makeText(MapsActivity.this、getAddress(homeLatitude、homeLatitude)、Toast.LENGTH_LONG.show();
showAlertBoxForOfficeLocationConfirmation(获取地址(officeLatitude,OfficeLongitate));
}否则{
Toast.makeText(MapsActivity.this,“请先将pin放置到主位置”,Toast.LENGTH_SHORT.show();
}
}
});
}//OnCreate结束
@凌驾
4月1日公开作废(谷歌地图谷歌地图){
mMap=谷歌地图;
mMap.setMapType(GoogleMap.MAP\u TYPE\u HYBRID);
LatLng boston=新LatLng(42.3601,-71.0589);
mMap.moveCamera(CameraUpdateFactory.newLatLng(波士顿));
mMap.animateCamera(CameraUpdateFactory.zoomTo(15));
展示柜(“主页”);
if(Build.VERSION.SDK\u INT>=Build.VERSION\u code.M){
if(ActivityCompat.checkSelfPermission(this,Manifest.permission.ACCESS\u FINE\u LOCATION)=授予PackageManager.permission&&ActivityCompat.checkSelfPermission(this,Manifest.permission.ACCESS\u LOCATION)=授予PackageManager.permission\u){
buildGoogleAppClient();
mMap.setMyLocationEnabled(真);
Toast.makeText(这是“内部烫发”,
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
        switch (requestCode) {
            case LOCATION_REQUEST_CODE: {
                // If request is cancelled, the result arrays are empty.
                if (grantResults.length > 0
                        && grantResults[0] == PackageManager.PERMISSION_GRANTED) {

                    mapFragment.getMapAsync(this);


                } else {
                    Toast.makeText(getApplicationContext(), "Please provide the permission", Toast.LENGTH_LONG).show();
                }
                break;
            }
        } 
}
   @Override
    public void onRequestPermissionsResult(int requestCode,
                                           String permissions[], int[] grantResults) {
        switch (requestCode) {
            case LOCATION_REQUEST_CODE: {

                if (grantResults.length == 0 || grantResults[0] != PackageManager.PERMISSION_GRANTED) {
                    Toast.makeText(this, "Unable to show location - permission required", Toast.LENGTH_LONG).show();
                    return;
                }

//                if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED){
//                    mapFragment.getMapAsync(this);
//                }
                else
                {
                    Toast.makeText(this,"Permission not granted",Toast.LENGTH_LONG).show();
                }
                break;
            }
        }
    }

}
@Override
public void onMapReady(GoogleMap googleMap) {
    mMap = googleMap;
    mMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);
    LatLng boston = new LatLng(42.3601, -71.0589);
    mMap.moveCamera(CameraUpdateFactory.newLatLng(boston));
    mMap.animateCamera(CameraUpdateFactory.zoomTo(15));

    showAlertBox("Home");

    if (ActivityCompat.checkSelfPermission
            (this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED

            && ActivityCompat.checkSelfPermission
            (this, Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED) {

        mMap.setMyLocationEnabled(true);

    } else {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            requestPermission(Manifest.permission.ACCESS_FINE_LOCATION, LOCATION_REQUEST_CODE);
        }
    }


    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {

        buildGoogleApiClient();
        Toast.makeText(this, "Inside Perm", Toast.LENGTH_LONG).show();
        mMap.setOnMapClickListener(new GoogleMap.OnMapClickListener() {
            @Override
            public void onMapClick(LatLng latLng) {

                if (count == 0) {
                    //Make the confirm button available on first tap
                    homeLocationFromPin.setVisibility(View.VISIBLE);
                    //add(point);
                    mMap.clear();
                    placedHomeMarker = new MarkerOptions().position(latLng);
                    placedHomeMarker.title("Home Location");
                    mMap.addMarker(placedHomeMarker);
                    homeLatitude = latLng.latitude;
                    homeLongitude = latLng.longitude;
                    Toast.makeText(MapsActivity.this, getNameOnly(homeLatitude, homeLongitude), Toast.LENGTH_SHORT).show();
                } else {
                    officeLocationFromPin.setVisibility(View.VISIBLE);
                    placedOfficeMarker = new MarkerOptions().position(latLng);
                    placedOfficeMarker.title("Office Location");
                    mMap.addMarker(placedOfficeMarker);
                    officeLatitude = latLng.latitude;
                    officeLongitude = latLng.longitude;
                    Toast.makeText(MapsActivity.this, getNameOnly(homeLatitude, homeLongitude), Toast.LENGTH_SHORT).show();
                    Toast.makeText(MapsActivity.this, getNameOnly(officeLatitude, officeLongitude), Toast.LENGTH_SHORT).show();
                }
            }
        });
    } else {

        buildGoogleApiClient();
        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.
            mMap.setMyLocationEnabled(true);
        }

        mMap.setOnMapClickListener(new GoogleMap.OnMapClickListener() {
            @Override
            public void onMapClick(LatLng latLng) {
                if (count == 0) {
                    //Make the confirm button available on first tap
                    homeLocationFromPin.setVisibility(View.VISIBLE);
                    //add(point);
                    mMap.clear();
                    placedHomeMarker = new MarkerOptions().position(latLng);
                    placedHomeMarker.title("Home Location");
                    mMap.addMarker(placedHomeMarker);
                    homeLatitude = latLng.latitude;
                    homeLongitude = latLng.longitude;
                    Toast.makeText(MapsActivity.this, getNameOnly(homeLatitude, homeLongitude), Toast.LENGTH_SHORT).show();
                } else {
                    officeLocationFromPin.setVisibility(View.VISIBLE);
                    placedOfficeMarker = new MarkerOptions().position(latLng);
                    placedOfficeMarker.title("Office Location");
                    mMap.addMarker(placedOfficeMarker);
                    officeLatitude = latLng.latitude;
                    officeLongitude = latLng.longitude;
                    Toast.makeText(MapsActivity.this, getNameOnly(homeLatitude, homeLongitude), Toast.LENGTH_SHORT).show();
                    Toast.makeText(MapsActivity.this, getNameOnly(officeLatitude, officeLongitude), Toast.LENGTH_SHORT).show();
                }
            }
        });
    }
}//End of onMap Ready