use of net.osmand.plus.TargetPointsHelper in project Osmand by osmandapp.
the class DirectionsDialogs method addWaypointDialogAndLaunchMap.
public static void addWaypointDialogAndLaunchMap(final AppCompatActivity act, final double lat, final double lon, final PointDescription name) {
final TargetPointsHelper targetPointsHelper = ((OsmandApplication) act.getApplication()).getTargetPointsHelper();
if (targetPointsHelper.getPointToNavigate() != null) {
Bundle args = new Bundle();
args.putDouble(AddWaypointBottomSheetDialogFragment.LAT_KEY, lat);
args.putDouble(AddWaypointBottomSheetDialogFragment.LON_KEY, lon);
args.putString(AddWaypointBottomSheetDialogFragment.POINT_DESCRIPTION_KEY, PointDescription.serializeToString(name));
AddWaypointBottomSheetDialogFragment fragment = new AddWaypointBottomSheetDialogFragment();
fragment.setArguments(args);
fragment.show(act.getSupportFragmentManager(), AddWaypointBottomSheetDialogFragment.TAG);
} else {
targetPointsHelper.navigateToPoint(new LatLon(lat, lon), true, -1, name);
closeContextMenu(act);
MapActivity.launchMapActivityMoveToTop(act);
}
}
use of net.osmand.plus.TargetPointsHelper in project Osmand by osmandapp.
the class FailSafeFuntions method restoreRoutingMode.
public static void restoreRoutingMode(final MapActivity ma) {
final OsmandApplication app = ma.getMyApplication();
final OsmandSettings settings = app.getSettings();
final Handler uiHandler = new Handler();
final String gpxPath = settings.FOLLOW_THE_GPX_ROUTE.get();
final TargetPointsHelper targetPoints = app.getTargetPointsHelper();
final TargetPoint pointToNavigate = targetPoints.getPointToNavigate();
if (pointToNavigate == null && gpxPath == null) {
notRestoreRoutingMode(ma, app);
} else {
quitRouteRestoreDialog = false;
Runnable encapsulate = new Runnable() {
int delay = 7;
Runnable delayDisplay = null;
@Override
public void run() {
AlertDialog.Builder builder = new AlertDialog.Builder(ma);
final TextView tv = new TextView(ma);
tv.setText(ma.getString(R.string.continue_follow_previous_route_auto, delay + ""));
tv.setPadding(7, 5, 7, 5);
builder.setView(tv);
builder.setPositiveButton(R.string.shared_string_yes, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
quitRouteRestoreDialog = true;
restoreRoutingModeInner();
}
});
builder.setNegativeButton(R.string.shared_string_no, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
quitRouteRestoreDialog = true;
notRestoreRoutingMode(ma, app);
}
});
final AlertDialog dlg = builder.show();
dlg.setOnDismissListener(new OnDismissListener() {
@Override
public void onDismiss(DialogInterface dialog) {
quitRouteRestoreDialog = true;
}
});
dlg.setOnCancelListener(new OnCancelListener() {
@Override
public void onCancel(DialogInterface dialog) {
quitRouteRestoreDialog = true;
}
});
delayDisplay = new Runnable() {
@Override
public void run() {
if (!quitRouteRestoreDialog) {
delay--;
tv.setText(ma.getString(R.string.continue_follow_previous_route_auto, delay + ""));
if (delay <= 0) {
try {
if (dlg.isShowing() && !quitRouteRestoreDialog) {
dlg.dismiss();
}
quitRouteRestoreDialog = true;
restoreRoutingModeInner();
} catch (Exception e) {
// swalow view not attached exception
log.error(e.getMessage() + "", e);
}
} else {
uiHandler.postDelayed(delayDisplay, 1000);
}
}
}
};
delayDisplay.run();
}
private void restoreRoutingModeInner() {
AsyncTask<String, Void, GPXFile> task = new AsyncTask<String, Void, GPXFile>() {
@Override
protected GPXFile doInBackground(String... params) {
if (gpxPath != null) {
// Reverse also should be stored ?
GPXFile f = GPXUtilities.loadGPXFile(app, new File(gpxPath));
if (f.warning != null) {
return null;
}
return f;
} else {
return null;
}
}
@Override
protected void onPostExecute(GPXFile result) {
final GPXRouteParamsBuilder gpxRoute;
if (result != null) {
gpxRoute = new GPXRouteParamsBuilder(result, settings);
if (settings.GPX_ROUTE_CALC_OSMAND_PARTS.get()) {
gpxRoute.setCalculateOsmAndRouteParts(true);
}
if (settings.GPX_CALCULATE_RTEPT.get()) {
gpxRoute.setUseIntermediatePointsRTE(true);
}
if (settings.GPX_ROUTE_CALC.get()) {
gpxRoute.setCalculateOsmAndRoute(true);
}
} else {
gpxRoute = null;
}
TargetPoint endPoint = pointToNavigate;
if (endPoint == null) {
notRestoreRoutingMode(ma, app);
} else {
enterRoutingMode(ma, gpxRoute);
}
}
};
task.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, gpxPath);
}
};
encapsulate.run();
}
}
use of net.osmand.plus.TargetPointsHelper in project Osmand by osmandapp.
the class RoutingHelper method updateCurrentRouteStatus.
private boolean updateCurrentRouteStatus(Location currentLocation, float posTolerance) {
List<Location> routeNodes = route.getImmutableAllLocations();
int currentRoute = route.currentRoute;
// 1. Try to proceed to next point using orthogonal distance (finding minimum orthogonal dist)
while (currentRoute + 1 < routeNodes.size()) {
double dist = currentLocation.distanceTo(routeNodes.get(currentRoute));
if (currentRoute > 0) {
dist = getOrthogonalDistance(currentLocation, routeNodes.get(currentRoute - 1), routeNodes.get(currentRoute));
}
boolean processed = false;
// if we are still too far try to proceed many points
// if not then look ahead only 3 in order to catch sharp turns
boolean longDistance = dist >= 250;
int newCurrentRoute = lookAheadFindMinOrthogonalDistance(currentLocation, routeNodes, currentRoute, longDistance ? 15 : 8);
double newDist = getOrthogonalDistance(currentLocation, routeNodes.get(newCurrentRoute), routeNodes.get(newCurrentRoute + 1));
if (longDistance) {
if (newDist < dist) {
if (log.isDebugEnabled()) {
// $NON-NLS-1$//$NON-NLS-2$
log.debug("Processed by distance : (new) " + newDist + " (old) " + dist);
}
processed = true;
}
} else if (newDist < dist || newDist < 10) {
// newDist < 10 (avoid distance 0 till next turn)
if (dist > posTolerance) {
processed = true;
if (log.isDebugEnabled()) {
// $NON-NLS-1$//$NON-NLS-2$
log.debug("Processed by distance : " + newDist + " " + dist);
}
} else {
// but you have not yet turned (could be checked bearing)
if (currentLocation.hasBearing() || lastFixedLocation != null) {
float bearingToRoute = currentLocation.bearingTo(routeNodes.get(currentRoute));
float bearingRouteNext = routeNodes.get(newCurrentRoute).bearingTo(routeNodes.get(newCurrentRoute + 1));
float bearingMotion = currentLocation.hasBearing() ? currentLocation.getBearing() : lastFixedLocation.bearingTo(currentLocation);
double diff = Math.abs(MapUtils.degreesDiff(bearingMotion, bearingToRoute));
double diffToNext = Math.abs(MapUtils.degreesDiff(bearingMotion, bearingRouteNext));
if (diff > diffToNext) {
if (log.isDebugEnabled()) {
log.debug("Processed point bearing deltas : " + diff + " " + diffToNext);
}
processed = true;
}
}
}
}
if (processed) {
// that node already passed
route.updateCurrentRoute(newCurrentRoute + 1);
currentRoute = newCurrentRoute + 1;
app.getNotificationHelper().refreshNotification(NotificationType.NAVIGATION);
} else {
break;
}
}
// 2. check if intermediate found
if (route.getIntermediatePointsToPass() > 0 && route.getDistanceToNextIntermediate(lastFixedLocation) < getArrivalDistance() * 2f && !isRoutePlanningMode) {
showMessage(app.getString(R.string.arrived_at_intermediate_point));
route.passIntermediatePoint();
TargetPointsHelper targets = app.getTargetPointsHelper();
String name = "";
if (intermediatePoints != null && !intermediatePoints.isEmpty()) {
LatLon rm = intermediatePoints.remove(0);
List<TargetPoint> ll = targets.getIntermediatePointsNavigation();
int ind = -1;
for (int i = 0; i < ll.size(); i++) {
if (ll.get(i).point != null && MapUtils.getDistance(ll.get(i).point, rm) < 5) {
name = ll.get(i).getOnlyName();
ind = i;
break;
}
}
if (ind >= 0) {
targets.removeWayPoint(false, ind);
}
}
if (isFollowingMode) {
voiceRouter.arrivedIntermediatePoint(name);
}
// double check
while (intermediatePoints != null && route.getIntermediatePointsToPass() < intermediatePoints.size()) {
intermediatePoints.remove(0);
}
}
// 3. check if destination found
Location lastPoint = routeNodes.get(routeNodes.size() - 1);
if (currentRoute > routeNodes.size() - 3 && currentLocation.distanceTo(lastPoint) < getArrivalDistance() && !isRoutePlanningMode) {
// showMessage(app.getString(R.string.arrived_at_destination));
TargetPointsHelper targets = app.getTargetPointsHelper();
TargetPoint tp = targets.getPointToNavigate();
String description = tp == null ? "" : tp.getOnlyName();
if (isFollowingMode) {
voiceRouter.arrivedDestinationPoint(description);
}
boolean onDestinationReached = OsmandPlugin.onDestinationReached();
onDestinationReached &= app.getAppCustomization().onDestinationReached();
if (onDestinationReached) {
clearCurrentRoute(null, null);
setRoutePlanningMode(false);
app.runInUIThread(new Runnable() {
@Override
public void run() {
settings.LAST_ROUTING_APPLICATION_MODE = settings.APPLICATION_MODE.get();
// settings.APPLICATION_MODE.set(settings.DEFAULT_APPLICATION_MODE.get());
}
});
finishCurrentRoute();
// targets.clearPointToNavigate(false);
return true;
}
}
return false;
}
use of net.osmand.plus.TargetPointsHelper in project Osmand by osmandapp.
the class MapActivityActions method setFirstMapMarkerAsTarget.
public void setFirstMapMarkerAsTarget() {
if (getMyApplication().getMapMarkersHelper().getMapMarkers().size() > 0) {
MapMarkersHelper.MapMarker marker = getMyApplication().getMapMarkersHelper().getMapMarkers().get(0);
PointDescription pointDescription = marker.getOriginalPointDescription();
if (pointDescription.isLocation() && pointDescription.getName().equals(PointDescription.getAddressNotFoundStr(mapActivity))) {
pointDescription = new PointDescription(PointDescription.POINT_TYPE_LOCATION, "");
}
TargetPointsHelper targets = getMyApplication().getTargetPointsHelper();
targets.navigateToPoint(new LatLon(marker.getLatitude(), marker.getLongitude()), true, targets.getIntermediatePoints().size() + 1, pointDescription);
}
}
use of net.osmand.plus.TargetPointsHelper in project Osmand by osmandapp.
the class MapActivityActions method setGPXRouteParams.
public void setGPXRouteParams(GPXFile result) {
if (result == null) {
mapActivity.getRoutingHelper().setGpxParams(null);
settings.FOLLOW_THE_GPX_ROUTE.set(null);
} else {
GPXRouteParamsBuilder params = new GPXRouteParamsBuilder(result, mapActivity.getMyApplication().getSettings());
if (result.hasRtePt() && !result.hasTrkPt()) {
settings.GPX_CALCULATE_RTEPT.set(true);
} else {
settings.GPX_CALCULATE_RTEPT.set(false);
}
params.setCalculateOsmAndRouteParts(settings.GPX_ROUTE_CALC_OSMAND_PARTS.get());
params.setUseIntermediatePointsRTE(settings.GPX_CALCULATE_RTEPT.get());
params.setCalculateOsmAndRoute(settings.GPX_ROUTE_CALC.get());
List<Location> ps = params.getPoints();
mapActivity.getRoutingHelper().setGpxParams(params);
settings.FOLLOW_THE_GPX_ROUTE.set(result.path);
if (!ps.isEmpty()) {
Location startLoc = ps.get(0);
Location finishLoc = ps.get(ps.size() - 1);
TargetPointsHelper tg = mapActivity.getMyApplication().getTargetPointsHelper();
tg.navigateToPoint(new LatLon(finishLoc.getLatitude(), finishLoc.getLongitude()), false, -1);
if (startLoc != finishLoc) {
tg.setStartPoint(new LatLon(startLoc.getLatitude(), startLoc.getLongitude()), false, null);
} else {
tg.clearStartPoint(false);
}
}
}
}
Aggregations