O
O
OM12016-09-21 20:16:26
Android
OM1, 2016-09-21 20:16:26

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 {
    }

}

Questions:
0. To begin with, it is not at all clear what methods to use for geolocation. Tried about 5 sets. Is this one correct?
1. at the first start, the coordinates arrive, onLocationChanged is triggered, but the actions in onLocationChanged are not processed
2. on the 6th android, it immediately crashes, as I understand it, this is connected with Permissions, it’s not enough to write them in the manifest, do you still need to request it when calling the method? I haven’t worked out the info here yet
3. At the first start, it catches GelLastKnownPosition, the setUpMyLocation () method from the code hangs on the button, it doesn’t always give a real result, it returns the previous position again

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question