From 8acdf595af0eae08185299b5f2a8eafd42d35f5e Mon Sep 17 00:00:00 2001 From: Victor Shcherb Date: Mon, 21 Sep 2015 09:38:42 +0200 Subject: [PATCH] Add method to find location --- .../osmand/plus/CurrentPositionHelper.java | 30 +++++++++++++------ .../osmand/plus/OsmAndLocationProvider.java | 5 ++++ 2 files changed, 26 insertions(+), 9 deletions(-) diff --git a/OsmAnd/src/net/osmand/plus/CurrentPositionHelper.java b/OsmAnd/src/net/osmand/plus/CurrentPositionHelper.java index 99bd2b91f6..562ac6ff06 100644 --- a/OsmAnd/src/net/osmand/plus/CurrentPositionHelper.java +++ b/OsmAnd/src/net/osmand/plus/CurrentPositionHelper.java @@ -4,6 +4,7 @@ import java.io.IOException; import java.util.HashMap; import net.osmand.Location; +import net.osmand.ResultMatcher; import net.osmand.binary.RouteDataObject; import net.osmand.router.BinaryRoutePlanner.RouteSegment; import net.osmand.router.GeneralRouter.GeneralRouterProfile; @@ -35,14 +36,14 @@ public class CurrentPositionHelper { } else if (am.isDerivedRoutingFrom(ApplicationMode.CAR)) { p = GeneralRouterProfile.CAR; } else { - return; + p = GeneralRouterProfile.PEDESTRIAN; } RoutingConfiguration cfg = app.getDefaultRoutingConfig().build(p.name().toLowerCase(), 10, new HashMap()); ctx = new RoutePlannerFrontEnd(false).buildRoutingContext(cfg, null, app.getResourceManager().getRoutingMapFiles()); } - public synchronized RouteDataObject runUpdateInThread(double lat, double lon) throws IOException { + public synchronized RouteDataObject runUpdateInThread(double lat, double lon, ResultMatcher result) throws IOException { RoutePlannerFrontEnd rp = new RoutePlannerFrontEnd(false); if (ctx == null || am != app.getSettings().getApplicationMode()) { initCtx(app); @@ -50,7 +51,14 @@ public class CurrentPositionHelper { return null; } } - RouteSegment sg = rp.findRouteSegment(lat, lon, ctx); + final RouteSegment sg = rp.findRouteSegment(lat, lon, ctx); + if(result != null) { + app.runInUIThread(new Runnable() { + public void run() { + result.publish(sg == null ? null : sg.getRoad()); + } + }); + } if (sg == null) { return null; } @@ -59,18 +67,18 @@ public class CurrentPositionHelper { } - private void scheduleRouteSegmentFind(final Location loc) { + private void scheduleRouteSegmentFind(final Location loc, final ResultMatcher result) { Thread calcThread = calculatingThread; if (calcThread == Thread.currentThread()) { - lastFound = runUpdateInThreadCatch(loc.getLatitude(), loc.getLongitude()); + 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()); - if (lastAskedLocation != loc) { + lastFound = runUpdateInThreadCatch(loc.getLatitude(), loc.getLongitude(), result); + if (lastAskedLocation != loc && result != null) { // refresh and run new task if needed getLastKnownRouteSegment(lastAskedLocation); } @@ -87,9 +95,9 @@ public class CurrentPositionHelper { } - protected RouteDataObject runUpdateInThreadCatch(double latitude, double longitude) { + protected RouteDataObject runUpdateInThreadCatch(double latitude, double longitude, final ResultMatcher result) { try { - return runUpdateInThread(latitude, longitude); + return runUpdateInThread(latitude, longitude, result); } catch (IOException e) { e.printStackTrace(); return null; @@ -115,6 +123,10 @@ public class CurrentPositionHelper { return d; } + public void getLastKnownRouteSegment(Location loc, ResultMatcher result) { + scheduleRouteSegmentFind(loc); + } + public RouteDataObject getLastKnownRouteSegment(Location loc) { lastAskedLocation = loc; RouteDataObject r = lastFound; diff --git a/OsmAnd/src/net/osmand/plus/OsmAndLocationProvider.java b/OsmAnd/src/net/osmand/plus/OsmAndLocationProvider.java index 91c39081ac..9ca7e64bd5 100644 --- a/OsmAnd/src/net/osmand/plus/OsmAndLocationProvider.java +++ b/OsmAnd/src/net/osmand/plus/OsmAndLocationProvider.java @@ -8,6 +8,7 @@ import java.util.List; import net.osmand.GeoidAltitudeCorrection; import net.osmand.PlatformUtil; +import net.osmand.ResultMatcher; import net.osmand.access.NavigationInfo; import net.osmand.binary.RouteDataObject; import net.osmand.data.LatLon; @@ -846,6 +847,10 @@ public class OsmAndLocationProvider implements SensorEventListener { public RouteDataObject getLastKnownRouteSegment() { return currentPositionHelper.getLastKnownRouteSegment(getLastKnownLocation()); } + + public void getRouteSegment(net.osmand.Location loc, ResultMatcher result) { + currentPositionHelper.getLastKnownRouteSegment(loc, result); + } public net.osmand.Location getLastKnownLocation() { return location;