Answer the question
In order to leave comments, you need to log in
Android golocation: Why is geolocation not stable?
Here is the last algorithm for obtaining geolocation, what is wrong here:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mapView = (MapView) findViewById(R.id.mapView);
mapView.onCreate(savedInstanceState);
mapView.getExtendedMapAsync(new OnMapReadyCallback() {
@Override
public void onMapReady(GoogleMap googleMap) {
setUpMap(googleMap);
}
});
}
private void setUpMap(GoogleMap map) {
mMap = map;
setUpMyLocation();
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
setUpMyLocation();
}
@Override
public void onProviderEnabled(String provider) {
ProviderEnabled = true;
}
@Override
public void onProviderDisabled(String provider) {
ProviderEnabled = false;
}
@Override
public void onLocationChanged(Location location) {
mMap.clear();
//DO обработка карты
update_interval = System.currentTimeMillis();
}
private String getEnabledLocationProvider() {
if ( Build.VERSION.SDK_INT >= 23 &&
ContextCompat.checkSelfPermission( getBaseContext(), android.Manifest.permission.ACCESS_FINE_LOCATION ) != PackageManager.PERMISSION_GRANTED &&
ContextCompat.checkSelfPermission( getBaseContext(), android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
}else{
}
locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
Criteria criteria = new Criteria();
String bestProvider = locationManager.getBestProvider(criteria, true);
ProviderEnabled = locationManager.isProviderEnabled(bestProvider);
if (!ProviderEnabled) {
return null;
}
return bestProvider;
}
private void setUpMyLocation() {
locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
String locationProvider = null;
try {
locationProvider = this.getEnabledLocationProvider();
} catch (Exception e) {
e.printStackTrace();
}
if (locationProvider == null) {
return;
}
final long MIN_TIME_BW_UPDATES = 5000;
final float MIN_DISTANCE_CHANGE_FOR_UPDATES = 5;
Location myLocation = null;
try {
locLis = (LocationListener) this;
// This code need permissions (Asked above ***)
locationManager.requestLocationUpdates(locationProvider,MIN_TIME_BW_UPDATES,MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
// Getting Location.
myLocation = locationManager.getLastKnownLocation(locationProvider);
}
// With Android API >= 23, need to catch SecurityException.
catch (SecurityException e) {
Log.e("MY", "Show My Location Error:" + e.getMessage());
e.printStackTrace();
}
if (myLocation != null) {
mMap.clear();
LatLng latLng = new LatLng(myLocation.getLatitude(), myLocation.getLongitude());
mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(latLng, 13));
if(userCircle!=null) {
userCircle.remove();
}
CircleOptions circleOptions = new CircleOptions()
.center(latLng)
.radius(15000)
.fillColor(Color.TRANSPARENT)
.strokeColor(Color.BLUE)
.strokeWidth(1);
userCircle = mMap.addCircle(circleOptions);
CameraPosition cameraPosition = new CameraPosition.Builder()
.target(latLng)
.zoom(15)
.build();
mMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
} else {
}
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question