From c4e0f87871044101f418b98ae467800b87c9f362 Mon Sep 17 00:00:00 2001 From: Victor Shcherb Date: Mon, 21 Jan 2013 18:19:14 +0100 Subject: [PATCH] Move to core --- .../osmand/plus/CurrentPositionHelper.java | 118 ------------------ 1 file changed, 118 deletions(-) delete mode 100644 OsmAnd/src/net/osmand/plus/CurrentPositionHelper.java diff --git a/OsmAnd/src/net/osmand/plus/CurrentPositionHelper.java b/OsmAnd/src/net/osmand/plus/CurrentPositionHelper.java deleted file mode 100644 index e28219fce7..0000000000 --- a/OsmAnd/src/net/osmand/plus/CurrentPositionHelper.java +++ /dev/null @@ -1,118 +0,0 @@ -package net.osmand.plus; - -import java.io.IOException; - -import net.osmand.Location; -import net.osmand.binary.RouteDataObject; -import net.osmand.osm.MapUtils; -import net.osmand.router.BinaryRoutePlanner.RouteSegment; -import net.osmand.router.GeneralRouter.GeneralRouterProfile; -import net.osmand.router.RoutePlannerFrontEnd; -import net.osmand.router.RoutingConfiguration; -import net.osmand.router.RoutingContext; - -public class CurrentPositionHelper { - - private RouteDataObject lastFound; - private Location lastAskedLocation = null; - private Thread calculatingThread = null; - private RoutingContext ctx; - private OsmandApplication app; - private ApplicationMode am; - - public CurrentPositionHelper(OsmandApplication app) { - this.app = app; - } - - private void initCtx(OsmandApplication app) { - ResourceManager r = app.getResourceManager(); - am = app.getSettings().getApplicationMode(); - GeneralRouterProfile p ; - if (am == ApplicationMode.BICYCLE) { - p = GeneralRouterProfile.BICYCLE; - } else if (am == ApplicationMode.PEDESTRIAN) { - p = GeneralRouterProfile.PEDESTRIAN; - } else { - p = GeneralRouterProfile.CAR; - } - RoutingConfiguration cfg = RoutingConfiguration.getDefault().build(p.name().toLowerCase(), 10); - ctx = new RoutingContext(cfg, null, r.getRoutingMapFiles()); - } - - private RouteDataObject runUpdateInThread(Location loc) { - RoutePlannerFrontEnd rp = new RoutePlannerFrontEnd(false); - try { - if(ctx == null || am != app.getSettings().getApplicationMode()) { - initCtx(app); - } - RouteSegment sg = rp.findRouteSegment(loc.getLatitude(), loc.getLongitude(), ctx); - return sg.getRoad(); - } catch (IOException e) { - return null; - } - } - - - private void scheduleRouteSegmentFind(final Location loc){ - if(calculatingThread == Thread.currentThread()) { - lastFound = runUpdateInThread(loc); - } else if(calculatingThread == null && loc != null) { - calculatingThread = new Thread(new Runnable() { - @Override - public void run() { - try { - lastFound = runUpdateInThread(loc); - if (lastAskedLocation != loc) { - // refresh and run new task if needed - getLastKnownRouteSegment(lastAskedLocation); - } - } finally { - calculatingThread = null; - } - } - }); - calculatingThread.start(); - } - - } - - private static double getOrthogonalDistance(RouteDataObject r, Location loc){ - double d = 1000; - if (r.getPointsLength() > 0) { - double pLt = MapUtils.get31LatitudeY(r.getPoint31YTile(0)); - double pLn = MapUtils.get31LongitudeX(r.getPoint31XTile(0)); - for (int i = 1; i < r.getPointsLength(); i++) { - double lt = MapUtils.get31LatitudeY(r.getPoint31YTile(i)); - double ln = MapUtils.get31LongitudeX(r.getPoint31XTile(i)); - double od = MapUtils.getOrthogonalDistance(loc.getLatitude(), loc.getLongitude(), pLt, pLn, lt, ln); - if (od < d) { - d = od; - } - pLt = lt; - pLn = ln; - } - } - return d; - } - - public RouteDataObject getLastKnownRouteSegment(Location loc) { - lastAskedLocation = loc; - RouteDataObject r = lastFound; - if (loc == null || loc.getAccuracy() > 50) { - return null; - } - if (r == null) { - scheduleRouteSegmentFind(loc); - return null; - } - double d = getOrthogonalDistance(r, loc); - if (d > 25) { - scheduleRouteSegmentFind(loc); - } - if (d < 70) { - return r; - } - return null; - } - -}