Add method to find location

This commit is contained in:
Victor Shcherb 2015-09-21 09:38:42 +02:00
parent dd62e80446
commit 8acdf595af
2 changed files with 26 additions and 9 deletions

View file

@ -4,6 +4,7 @@ import java.io.IOException;
import java.util.HashMap; import java.util.HashMap;
import net.osmand.Location; import net.osmand.Location;
import net.osmand.ResultMatcher;
import net.osmand.binary.RouteDataObject; import net.osmand.binary.RouteDataObject;
import net.osmand.router.BinaryRoutePlanner.RouteSegment; import net.osmand.router.BinaryRoutePlanner.RouteSegment;
import net.osmand.router.GeneralRouter.GeneralRouterProfile; import net.osmand.router.GeneralRouter.GeneralRouterProfile;
@ -35,14 +36,14 @@ public class CurrentPositionHelper {
} else if (am.isDerivedRoutingFrom(ApplicationMode.CAR)) { } else if (am.isDerivedRoutingFrom(ApplicationMode.CAR)) {
p = GeneralRouterProfile.CAR; p = GeneralRouterProfile.CAR;
} else { } else {
return; p = GeneralRouterProfile.PEDESTRIAN;
} }
RoutingConfiguration cfg = app.getDefaultRoutingConfig().build(p.name().toLowerCase(), 10, RoutingConfiguration cfg = app.getDefaultRoutingConfig().build(p.name().toLowerCase(), 10,
new HashMap<String, String>()); new HashMap<String, String>());
ctx = new RoutePlannerFrontEnd(false).buildRoutingContext(cfg, null, app.getResourceManager().getRoutingMapFiles()); 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<RouteDataObject> result) throws IOException {
RoutePlannerFrontEnd rp = new RoutePlannerFrontEnd(false); RoutePlannerFrontEnd rp = new RoutePlannerFrontEnd(false);
if (ctx == null || am != app.getSettings().getApplicationMode()) { if (ctx == null || am != app.getSettings().getApplicationMode()) {
initCtx(app); initCtx(app);
@ -50,7 +51,14 @@ public class CurrentPositionHelper {
return null; 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) { if (sg == null) {
return null; return null;
} }
@ -59,18 +67,18 @@ public class CurrentPositionHelper {
} }
private void scheduleRouteSegmentFind(final Location loc) { private void scheduleRouteSegmentFind(final Location loc, final ResultMatcher<RouteDataObject> result) {
Thread calcThread = calculatingThread; Thread calcThread = calculatingThread;
if (calcThread == Thread.currentThread()) { if (calcThread == Thread.currentThread()) {
lastFound = runUpdateInThreadCatch(loc.getLatitude(), loc.getLongitude()); lastFound = runUpdateInThreadCatch(loc.getLatitude(), loc.getLongitude(), result);
} else if (loc != null) { } else if (loc != null) {
if (calcThread == null) { if (calcThread == null) {
Runnable run = new Runnable() { Runnable run = new Runnable() {
@Override @Override
public void run() { public void run() {
try { try {
lastFound = runUpdateInThreadCatch(loc.getLatitude(), loc.getLongitude()); lastFound = runUpdateInThreadCatch(loc.getLatitude(), loc.getLongitude(), result);
if (lastAskedLocation != loc) { if (lastAskedLocation != loc && result != null) {
// refresh and run new task if needed // refresh and run new task if needed
getLastKnownRouteSegment(lastAskedLocation); 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<RouteDataObject> result) {
try { try {
return runUpdateInThread(latitude, longitude); return runUpdateInThread(latitude, longitude, result);
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
return null; return null;
@ -115,6 +123,10 @@ public class CurrentPositionHelper {
return d; return d;
} }
public void getLastKnownRouteSegment(Location loc, ResultMatcher<RouteDataObject> result) {
scheduleRouteSegmentFind(loc);
}
public RouteDataObject getLastKnownRouteSegment(Location loc) { public RouteDataObject getLastKnownRouteSegment(Location loc) {
lastAskedLocation = loc; lastAskedLocation = loc;
RouteDataObject r = lastFound; RouteDataObject r = lastFound;

View file

@ -8,6 +8,7 @@ import java.util.List;
import net.osmand.GeoidAltitudeCorrection; import net.osmand.GeoidAltitudeCorrection;
import net.osmand.PlatformUtil; import net.osmand.PlatformUtil;
import net.osmand.ResultMatcher;
import net.osmand.access.NavigationInfo; import net.osmand.access.NavigationInfo;
import net.osmand.binary.RouteDataObject; import net.osmand.binary.RouteDataObject;
import net.osmand.data.LatLon; import net.osmand.data.LatLon;
@ -846,6 +847,10 @@ public class OsmAndLocationProvider implements SensorEventListener {
public RouteDataObject getLastKnownRouteSegment() { public RouteDataObject getLastKnownRouteSegment() {
return currentPositionHelper.getLastKnownRouteSegment(getLastKnownLocation()); return currentPositionHelper.getLastKnownRouteSegment(getLastKnownLocation());
} }
public void getRouteSegment(net.osmand.Location loc, ResultMatcher<RouteDataObject> result) {
currentPositionHelper.getLastKnownRouteSegment(loc, result);
}
public net.osmand.Location getLastKnownLocation() { public net.osmand.Location getLastKnownLocation() {
return location; return location;