Location change glitches on map using polyline options

落花浮王杯 提交于 2020-01-06 02:49:30

问题


Im facing some problem with my application, specifically with route plot/draw on my google maps. Ive made test route around my house and found out, that GPS providers are not as accurate as similar applications like runtastic or endomondo.

Sometimes Location listener makes incomprehensible changes on my map and the polyline then draws any lines on the map near my location even with perfect GPS signal.
Some other time, it just doesnot work. It does not listen to location change.

Can anybody explain me (like Im five) how does other fitness application get their current position and the plot route onto the google map? Thanks!

//Map Fragment

    map = ((MapFragment) getFragmentManager().findFragmentById(R.id.fragment1)).getMap();
    map.setMyLocationEnabled(true);
    locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);

    Criteria criteria = new Criteria();
    String provider = locationManager.getBestProvider(criteria, true);

//Permission gain

    if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
        ActivityCompat.requestPermissions(this, new String[]{android.Manifest.permission.ACCESS_COARSE_LOCATION},
                MY_PERMISSION_ACCESS_COURSE_LOCATION);
        return;
    }
    isLocationEnabled(getApplicationContext());

    Location myLocation = locationManager.getLastKnownLocation(provider);
    if (myLocation != null) {
        latitude = myLocation.getLatitude();
        longitude = myLocation.getLongitude();
        float zoom = (float) 17.0;
        map.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(latitude, longitude), zoom));
        Log.e("TAG", "GPS is on");
        Toast.makeText(MainActivity.this, "latitude:" + latitude + " longitude:" + longitude, Toast.LENGTH_SHORT).show();
    } else {
        locationManager.requestLocationUpdates(provider, 5000, 0, this);
        locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 5000, 10, this);

    }

 public void onLocationChanged(Location mylocation) {

    if (lastLocationloc == null) {
        lastLocationloc = mylocation;
    }
    LatLng lastLatLng = locationToLatLng(lastLocationloc);
    LatLng thisLatLng = locationToLatLng(mylocation);
    map.addPolyline(new PolylineOptions().add(lastLatLng).add(thisLatLng).width(6).color(Color.RED));
    lastLocationloc = mylocation;
    Toast.makeText(MainActivity.this, "!!!!Location CHANGE!!!!", Toast.LENGTH_SHORT).show();

}

public static LatLng locationToLatLng(Location loc) {
    if (loc != null)
        return new LatLng(loc.getLatitude(), loc.getLongitude());
    return null;
}


For example, I want to plot it like this:


But my application works its own way..


回答1:


First of all, you need to take into account the accuracy of the received location. You can get the accuracy using the Location.getAccuracy() method (documentation). The accuracy is measured in meters, so the lower the better:

if (location.getAccuracy() < MINIMUM_ACCURACY) {
    // Add the new location to your polyline
}

You can set your MINIMUM_ACCURACY to be 10 meter for example.

On the other hand, you may want to add a new location to your polyline only if your new location is farther than a given distance to your last added location. As an example:

private static final float MINIMUM_ACCURACY = 10;
private static final float MINIMUM_DISTANCE_BETWEEN_POINTS = 20;
private Location lastLocationloc;

// ...

public void onLocationChanged(Location mylocation) {
    if (mylocation.getAccuracy() < MINIMUM_ACCURACY) {
        if (lastLocationloc == null || lastLocationloc.distanceTo(mylocation) > MINIMUM_DISTANCE_BETWEEN_POINTS) {
            // Add the new location to your polyline

            lastLocationloc = mylocation;
        }
    }
}


来源:https://stackoverflow.com/questions/37225838/location-change-glitches-on-map-using-polyline-options

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!