diff --git a/OsmAnd-java/src/net/osmand/router/RoutePlannerFrontEnd.java b/OsmAnd-java/src/net/osmand/router/RoutePlannerFrontEnd.java index 4d7ef34c81..690c061244 100644 --- a/OsmAnd-java/src/net/osmand/router/RoutePlannerFrontEnd.java +++ b/OsmAnd-java/src/net/osmand/router/RoutePlannerFrontEnd.java @@ -18,6 +18,7 @@ import net.osmand.data.LatLon; import net.osmand.data.QuadPoint; import net.osmand.router.BinaryRoutePlanner.RouteSegment; import net.osmand.router.BinaryRoutePlanner.RouteSegmentPoint; +import net.osmand.util.Algorithms; import net.osmand.util.MapUtils; import org.apache.commons.logging.Log; @@ -54,7 +55,7 @@ public class RoutePlannerFrontEnd { return dx * dx + dy * dy; } - public RouteSegmentPoint findRouteSegment(double lat, double lon, RoutingContext ctx) throws IOException { + public RouteSegmentPoint findRouteSegment(double lat, double lon, boolean searchWithName, RoutingContext ctx) throws IOException { int px = MapUtils.get31TileNumberX(lon); int py = MapUtils.get31TileNumberY(lat); ArrayList dataObjects = new ArrayList(); @@ -64,6 +65,10 @@ public class RoutePlannerFrontEnd { } List list = new ArrayList(); for (RouteDataObject r : dataObjects) { + boolean emptyName = Algorithms.isEmpty(r.getName()) && Algorithms.isEmpty(r.getRef()) ; + if(searchWithName && emptyName) { + continue; + } if (r.getPointsLength() > 1) { RouteSegmentPoint road = null; for (int j = 1; j < r.getPointsLength(); j++) { @@ -279,7 +284,7 @@ public class RoutePlannerFrontEnd { } private boolean addSegment(LatLon s, RoutingContext ctx, int indexNotFound, List res) throws IOException { - RouteSegmentPoint f = findRouteSegment(s.getLatitude(), s.getLongitude(), ctx); + RouteSegmentPoint f = findRouteSegment(s.getLatitude(), s.getLongitude(), false, ctx); if(f == null){ ctx.calculationProgress.segmentNotFound = indexNotFound; return false; diff --git a/OsmAnd-java/src/net/osmand/router/TestRouting.java b/OsmAnd-java/src/net/osmand/router/TestRouting.java index c0ce7178df..dacdf62c01 100644 --- a/OsmAnd-java/src/net/osmand/router/TestRouting.java +++ b/OsmAnd-java/src/net/osmand/router/TestRouting.java @@ -310,8 +310,8 @@ public class TestRouting { RoutingConfiguration rconfig = config.build(vehicle, MEMORY_TEST_LIMIT); RoutePlannerFrontEnd router = new RoutePlannerFrontEnd(oldRouting); RoutingContext ctx = router.buildRoutingContext(rconfig, lib, rs); - RouteSegment startSegment = router.findRouteSegment(startLat, startLon, ctx); - RouteSegment endSegment = router.findRouteSegment(endLat, endLon, ctx); + RouteSegment startSegment = router.findRouteSegment(startLat, startLon, false, ctx); + RouteSegment endSegment = router.findRouteSegment(endLat, endLon, false, ctx); if(startSegment == null){ throw new IllegalArgumentException("Start segment is not found "); } diff --git a/OsmAnd/src/net/osmand/plus/CurrentPositionHelper.java b/OsmAnd/src/net/osmand/plus/CurrentPositionHelper.java index b21d986da8..3450eef08e 100644 --- a/OsmAnd/src/net/osmand/plus/CurrentPositionHelper.java +++ b/OsmAnd/src/net/osmand/plus/CurrentPositionHelper.java @@ -7,6 +7,7 @@ import net.osmand.Location; import net.osmand.ResultMatcher; import net.osmand.binary.RouteDataObject; import net.osmand.router.BinaryRoutePlanner.RouteSegment; +import net.osmand.router.BinaryRoutePlanner.RouteSegmentPoint; import net.osmand.router.GeneralRouter.GeneralRouterProfile; import net.osmand.router.RoutePlannerFrontEnd; import net.osmand.router.RoutingConfiguration; @@ -17,7 +18,6 @@ public class CurrentPositionHelper { private RouteDataObject lastFound; private Location lastAskedLocation = null; - private Thread calculatingThread = null; private RoutingContext ctx; private OsmandApplication app; private ApplicationMode am; @@ -43,67 +43,29 @@ public class CurrentPositionHelper { ctx = new RoutePlannerFrontEnd(false).buildRoutingContext(cfg, null, app.getResourceManager().getRoutingMapFiles()); } - public synchronized RouteDataObject runUpdateInThread(double lat, double lon, final ResultMatcher result) throws IOException { - RoutePlannerFrontEnd rp = new RoutePlannerFrontEnd(false); - if (ctx == null || am != app.getSettings().getApplicationMode()) { - initCtx(app); - if (ctx == null) { - return null; - } - } - final RouteSegment sg = rp.findRouteSegment(lat, lon, ctx); - if(result != null) { - app.runInUIThread(new Runnable() { + + + + private void scheduleRouteSegmentFind(final Location loc, final ResultMatcher result, final boolean storeFound) { + if (loc != null) { + Runnable run = new Runnable() { + @Override public void run() { - result.publish(sg == null ? null : sg.getRoad()); - } - }); - } - if (sg == null) { - return null; - } - return sg.getRoad(); - - } - - - private void scheduleRouteSegmentFind(final Location loc, final ResultMatcher result) { - Thread calcThread = calculatingThread; - if (calcThread == Thread.currentThread()) { - lastFound = runUpdateInThreadCatch(loc.getLatitude(), loc.getLongitude(), result); - } else if (loc != null) { - if (calcThread == null) { - Runnable run = new Runnable() { - @Override - public void run() { - try { - lastFound = runUpdateInThreadCatch(loc.getLatitude(), loc.getLongitude(), result); - if (lastAskedLocation != loc && result != null) { - // refresh and run new task if needed - getLastKnownRouteSegment(lastAskedLocation); - } - } finally { - calculatingThread = null; + try { + RouteDataObject res = runUpdateInThread(loc.getLatitude(), loc.getLongitude(), result); + if (storeFound) { + lastAskedLocation = loc; + lastFound = res; } + } catch (IOException e) { + e.printStackTrace(); } - }; - calculatingThread = app.getRoutingHelper().startTaskInRouteThreadIfPossible(run); - } else if (calcThread != null && !calcThread.isAlive()) { - calculatingThread = null; - } + } + }; + app.getRoutingHelper().startTaskInRouteThreadIfPossible(run); } - } - protected RouteDataObject runUpdateInThreadCatch(double latitude, double longitude, final ResultMatcher result) { - try { - return runUpdateInThread(latitude, longitude, result); - } catch (IOException e) { - e.printStackTrace(); - return null; - } - } - private static double getOrthogonalDistance(RouteDataObject r, Location loc){ double d = 1000; if (r.getPointsLength() > 0) { @@ -124,22 +86,25 @@ public class CurrentPositionHelper { } public void getRouteSegment(Location loc, ResultMatcher result) { - scheduleRouteSegmentFind(loc, result); + scheduleRouteSegmentFind(loc, result, false); } public RouteDataObject getLastKnownRouteSegment(Location loc) { - lastAskedLocation = loc; + Location last = lastAskedLocation; RouteDataObject r = lastFound; if (loc == null || loc.getAccuracy() > 50) { return null; } + if(last != null && last.distanceTo(loc) < 20) { + return r; + } if (r == null) { - scheduleRouteSegmentFind(loc, null); + scheduleRouteSegmentFind(loc, null, true); return null; } double d = getOrthogonalDistance(r, loc); if (d > 25) { - scheduleRouteSegmentFind(loc, null); + scheduleRouteSegmentFind(loc, null, true); } if (d < 70) { return r; @@ -147,4 +112,35 @@ public class CurrentPositionHelper { return null; } + + private synchronized RouteDataObject runUpdateInThread(double lat, double lon, final ResultMatcher resultMatcher) throws IOException { + RoutePlannerFrontEnd rp = new RoutePlannerFrontEnd(false); + if (ctx == null || am != app.getSettings().getApplicationMode()) { + initCtx(app); + if (ctx == null) { + return null; + } + } + final RouteSegmentPoint sg = rp.findRouteSegment(lat, lon, true, ctx); + final RouteDataObject res; + if(sg == null) { + res = null; + } else { + RouteSegmentPoint ff = rp.findRouteSegment(lat, lon, false, ctx); + if(ff == null || ff.dist + 70 * 70 < sg.dist) { + res = null; + } else { + res = sg.getRoad(); + } + } + if(resultMatcher != null) { + app.runInUIThread(new Runnable() { + public void run() { + resultMatcher.publish(res); + } + }); + } + return res; + + } } diff --git a/OsmAnd/src/net/osmand/plus/routing/RoutingHelper.java b/OsmAnd/src/net/osmand/plus/routing/RoutingHelper.java index de02b50bd6..2ac23e7f4f 100644 --- a/OsmAnd/src/net/osmand/plus/routing/RoutingHelper.java +++ b/OsmAnd/src/net/osmand/plus/routing/RoutingHelper.java @@ -892,7 +892,7 @@ public class RoutingHelper { } public Thread startTaskInRouteThreadIfPossible(final Runnable r) { - if(currentRunningJob == null) { + if (currentRunningJob == null) { synchronized (this) { currentRunningJob = new Thread(new Runnable() { @Override @@ -908,7 +908,6 @@ public class RoutingHelper { }, "Calculating position"); //$NON-NLS-1$ currentRunningJob.start(); } - return currentRunningJob; } return null; }