Пытаюсь получить lat и lng из GoogleMapы. В переменную типа Location кидаю mMap.getMyLocation. Но это переменная null, почему?
private static final int LOCATION_PERMISSION_REQUEST_CODE = 1;
private Location mLocation = null;
private GoogleMap mMap;
private Marker myPos;
private int speed = 0 ;
private TextView speedView;
@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);
speedView = (TextView) findViewById(R.id.speedView);
/*Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
startActivity(intent);*/
}
public void requestPermission(String strPermission, int perCode, Context _c, Activity _a){
if (ActivityCompat.shouldShowRequestPermissionRationale(_a,strPermission)){
Toast.makeText(getApplicationContext(),"GPS permission allows us to access location data. Please allow in App Settings for additional functionality.",Toast.LENGTH_LONG).show();
} else {
Toast.makeText(getApplicationContext(), "GPS permissions dont allow", Toast.LENGTH_SHORT).show();
ActivityCompat.requestPermissions(_a,new String[]{strPermission},perCode);
}
}
public static boolean checkPermission(String strPermission,Context _c,Activity _a){
int result = ContextCompat.checkSelfPermission(_c, strPermission);
if (result == PackageManager.PERMISSION_GRANTED){
Toast.makeText(_c, "Permission granted", Toast.LENGTH_LONG).show();
return true;
} else {
return false;
}
}
@Override
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
switch (requestCode) {
case LOCATION_PERMISSION_REQUEST_CODE:
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
} else {
Toast.makeText(getApplicationContext(),"Permission Denied, You cannot access location data.",Toast.LENGTH_LONG).show();
}
break;
}
}
@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
if (Build.VERSION.SDK_INT <= 23){
if (checkPermission(Manifest.permission.ACCESS_FINE_LOCATION, getApplicationContext(), this)) {
mMap.setMyLocationEnabled(true);
mMap.getUiSettings().setMyLocationButtonEnabled(true);
mMap.getUiSettings().setAllGesturesEnabled(true);
mLocation = mMap.getMyLocation();
if (mLocation != null)
Toast.makeText(this, "Not null", Toast.LENGTH_SHORT).show();
else
Toast.makeText(this, "Null", Toast.LENGTH_SHORT).show();
}
else {
requestPermission(Manifest.permission.ACCESS_FINE_LOCATION,LOCATION_PERMISSION_REQUEST_CODE,getApplicationContext(),this);
}
}
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;
}
}