Remove polyline based on location
1 min read
I want to remove the line when current location is passes the line
This is how I add polyline. Based on directions api response
private void addPolyline(DirectionsResult results, GoogleMap mMap) {
decodedPath = PolyUtil.decode(results.routes[0].overviewPolyline.getEncodedPath());
int PATTERN_DASH_LENGTH_PX = 20;
int PATTERN_GAP_LENGTH_PX = 0;
PatternItem DASH = new Dash(PATTERN_DASH_LENGTH_PX);
PatternItem GAP = new Gap(PATTERN_GAP_LENGTH_PX);
List<PatternItem> PATTERN_POLYGON_ALPHA = Arrays.asList(GAP, DASH);
polyline= mMap.addPolyline(new PolylineOptions()
.geodesic(true)
.color(getResources().getColor(R.color.blue_900))
.width(8)
.pattern(PATTERN_POLYGON_ALPHA)
.addAll(decodedPath));
if (ContextCompat.checkSelfPermission(this.getActivity(), Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED){
startLocationUpdates();
}
}
Code when user changes location using fusedLocationProviderClient
private void setUserLocationMarker(Location location) {
//Remove location if distance is < or equal to 5 meters
for (LatLng latLngPoint : decodedPath) {
float[] results = new float[1];
Location.distanceBetween(location.getLatitude(), location.getLongitude(),
latLngPoint.latitude, latLngPoint.longitude, results);
float dis = results[0];
if(dis<=5){
decodedPath.remove(latLngPoint);
break;
}
}
//code Update user marker
polyline.setPoints(decodedPath);
}
I tried to use distanceBetween to get the distance between the current location and the google directions result. But the line still does not update.
資料來源:Stackoverflow