У меня проблемы с определением точек широты и долготы.
public class MainActivity extends AppCompatActivity implements FriendsFragment.OnFragmentInteractionListener, LocationListener {
private static final int ACCESS_FINE_LOCATION_INTENT_ID = 3;
LocationManager lm;
Location location;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (Build.VERSION.SDK_INT >= 23) {
if (ContextCompat.checkSelfPermission(MainActivity.this,
android.Manifest.permission.ACCESS_FINE_LOCATION)
!= PackageManager.PERMISSION_GRANTED)
requestLocationPermission();
else {
ensureLocationSettings();
}
} else {
ensureLocationSettings();
}
}
private void requestLocationPermission() {
if (ActivityCompat.shouldShowRequestPermissionRationale(MainActivity.this, android.Manifest.permission.ACCESS_FINE_LOCATION)) {
ActivityCompat.requestPermissions(MainActivity.this,
new String[]{android.Manifest.permission.ACCESS_FINE_LOCATION},
ACCESS_FINE_LOCATION_INTENT_ID);
} else {
ActivityCompat.requestPermissions(MainActivity.this,
new String[]{Manifest.permission.ACCESS_FINE_LOCATION},
ACCESS_FINE_LOCATION_INTENT_ID);
}
}
@Override
public void onLocationChanged(Location location) {
// TODO Auto-generated method stub
double latitude = (location.getLatitude());
double longitude = (location.getLongitude());
Log.i("Geo_Location", "Latitude: " + latitude + ", Longitude: " + longitude);
}
@Override
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
}
@Override
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
switch (requestCode) {
case ACCESS_FINE_LOCATION_INTENT_ID: {
// If request is cancelled, the result arrays are empty.
if (grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Toast.makeText(MainActivity.this, "accesses.", Toast.LENGTH_SHORT).show();
ensureLocationSettings();
LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);
} else {
Toast.makeText(MainActivity.this, "Location Permission denied.", Toast.LENGTH_SHORT).show();
closeNow();
}
return;
}
}
}
// ----------------
private void ensureLocationSettings() {
LocationSettingsRequest locationSettingsRequest = new LocationSettingsRequest.Builder()
.addLocationRequest(LocationRequest.create().setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY))
.build();
RxLocationSettings.with(this).ensure(locationSettingsRequest)
.subscribe(new Action1<Boolean>() {
@Override
public void call(Boolean enabled) {
if (enabled) {
Toast.makeText(MainActivity.this, "Enable", Toast.LENGTH_SHORT).show();
} else {
closeNow();
Toast.makeText(MainActivity.this, "permission denied", Toast.LENGTH_SHORT).show();
}
}
});
}