From 3034628e8295f60b4afd96c4a9319879fecc5380 Mon Sep 17 00:00:00 2001 From: Victor Shcherb Date: Sun, 3 Aug 2014 10:45:00 +0200 Subject: [PATCH 1/2] Refactor poi search --- OsmAnd/src/net/osmand/plus/PoiFilter.java | 6 +- .../osmand/plus/SearchOnTheRouteHelper.java | 54 +++++++++ .../resources/AmenityIndexRepository.java | 12 -- .../AmenityIndexRepositoryBinary.java | 86 +-------------- .../plus/resources/AsyncLoadingThread.java | 90 +-------------- .../plus/resources/ResourceManager.java | 104 ++++++++++++------ .../osmand/plus/routing/RoutingHelper.java | 9 ++ .../net/osmand/plus/views/POIMapLayer.java | 88 +++++++++++---- 8 files changed, 204 insertions(+), 245 deletions(-) create mode 100644 OsmAnd/src/net/osmand/plus/SearchOnTheRouteHelper.java diff --git a/OsmAnd/src/net/osmand/plus/PoiFilter.java b/OsmAnd/src/net/osmand/plus/PoiFilter.java index a6521f7a11..1f1c0efdd5 100644 --- a/OsmAnd/src/net/osmand/plus/PoiFilter.java +++ b/OsmAnd/src/net/osmand/plus/PoiFilter.java @@ -14,9 +14,6 @@ import net.osmand.ResultMatcher; import net.osmand.data.Amenity; import net.osmand.data.AmenityType; import net.osmand.util.MapUtils; - -import org.apache.http.client.protocol.ClientContext; - import android.content.Context; public class PoiFilter { @@ -173,9 +170,8 @@ public class PoiFilter { protected List searchAmenities(double lat, double lon, double topLatitude, double bottomLatitude, double leftLongitude, double rightLongitude, final ResultMatcher matcher) { - return app.getResourceManager().searchAmenities(this, - topLatitude, leftLongitude, bottomLatitude, rightLongitude, lat, lon, matcher); + topLatitude, leftLongitude, bottomLatitude, rightLongitude, matcher); } public List searchAgain(double lat, double lon) { diff --git a/OsmAnd/src/net/osmand/plus/SearchOnTheRouteHelper.java b/OsmAnd/src/net/osmand/plus/SearchOnTheRouteHelper.java new file mode 100644 index 0000000000..63260d110e --- /dev/null +++ b/OsmAnd/src/net/osmand/plus/SearchOnTheRouteHelper.java @@ -0,0 +1,54 @@ +package net.osmand.plus; + +import java.io.IOException; +import java.util.HashMap; + +import net.osmand.Location; +import net.osmand.binary.RouteDataObject; +import net.osmand.plus.routing.RouteCalculationResult; +import net.osmand.router.RoutePlannerFrontEnd; +import net.osmand.router.RoutingConfiguration; +import net.osmand.router.BinaryRoutePlanner.RouteSegment; +import net.osmand.router.GeneralRouter.GeneralRouterProfile; +import net.osmand.util.MapUtils; + +public class SearchOnTheRouteHelper { + + private OsmandApplication app; + private PoiFilter filter; + private Thread calculatingThread; + + public SearchOnTheRouteHelper(OsmandApplication app) { + this.app = app; + } + + public void searchOnTheRoute(RouteCalculationResult route) { + scheduleRouteSegmentFind(route); + } + + + private void scheduleRouteSegmentFind(final RouteCalculationResult route){ + } + + 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; + } + + + +} diff --git a/OsmAnd/src/net/osmand/plus/resources/AmenityIndexRepository.java b/OsmAnd/src/net/osmand/plus/resources/AmenityIndexRepository.java index 9182c1c088..439e498bc9 100644 --- a/OsmAnd/src/net/osmand/plus/resources/AmenityIndexRepository.java +++ b/OsmAnd/src/net/osmand/plus/resources/AmenityIndexRepository.java @@ -20,17 +20,5 @@ public interface AmenityIndexRepository { public List searchAmenities(int stop, int sleft, int sbottom, int sright, int zoom, PoiFilter filter, List amenities, ResultMatcher matcher); - - public void clearCache(); - - public boolean checkCachedAmenities(double topLatitude, double leftLongitude, double bottomLatitude, double rightLongitude, int zoom, - String filterId, List toFill, boolean fillFound); - - public void evaluateCachedAmenities(double topLatitude, double leftLongitude, double bottomLatitude, double rightLongitude, int zoom, - PoiFilter filter, ResultMatcher matcher); - - public boolean hasChange(); - - public void clearChange(); } diff --git a/OsmAnd/src/net/osmand/plus/resources/AmenityIndexRepositoryBinary.java b/OsmAnd/src/net/osmand/plus/resources/AmenityIndexRepositoryBinary.java index 46d0014437..82a41ece38 100644 --- a/OsmAnd/src/net/osmand/plus/resources/AmenityIndexRepositoryBinary.java +++ b/OsmAnd/src/net/osmand/plus/resources/AmenityIndexRepositoryBinary.java @@ -2,7 +2,6 @@ package net.osmand.plus.resources; import java.io.IOException; -import java.util.ArrayList; import java.util.Collections; import java.util.List; import java.util.Map; @@ -15,9 +14,7 @@ import net.osmand.binary.BinaryMapIndexReader.SearchPoiTypeFilter; import net.osmand.binary.BinaryMapIndexReader.SearchRequest; import net.osmand.data.Amenity; import net.osmand.data.AmenityType; -import net.osmand.data.LatLon; import net.osmand.plus.PoiFilter; -import net.osmand.util.Algorithms; import net.osmand.util.MapUtils; import org.apache.commons.logging.Log; @@ -40,17 +37,6 @@ public class AmenityIndexRepositoryBinary implements AmenityIndexRepository { } } - - @Override - public boolean hasChange() { - return false; //no change ever - } - - @Override - public void clearChange() { - //nothing to do - } - @Override public boolean checkContains(double latitude, double longitude) { return index.containsPoiData(latitude, longitude); @@ -62,7 +48,7 @@ public class AmenityIndexRepositoryBinary implements AmenityIndexRepository { } - public Map> searchAmenityCategoriesByName(String query, Map> map) { + public synchronized Map> searchAmenityCategoriesByName(String query, Map> map) { try { return index.searchPoiCategoriesByName(query, map); } catch (IOException e) { @@ -72,7 +58,7 @@ public class AmenityIndexRepositoryBinary implements AmenityIndexRepository { } - public List searchAmenitiesByName(int x, int y, int l, int t, int r, int b, String query, ResultMatcher resulMatcher) { + public synchronized List searchAmenitiesByName(int x, int y, int l, int t, int r, int b, String query, ResultMatcher resulMatcher) { long now = System.currentTimeMillis(); List amenities = Collections.emptyList(); SearchRequest req = BinaryMapIndexReader.buildSearchPoiRequest(x, y, query, l, r, t, b,resulMatcher); @@ -95,7 +81,7 @@ public class AmenityIndexRepositoryBinary implements AmenityIndexRepository { } @Override - public List searchAmenities(int stop, int sleft, int sbottom, int sright, int zoom, + public synchronized List searchAmenities(int stop, int sleft, int sbottom, int sright, int zoom, final PoiFilter filter, final List amenities, ResultMatcher matcher) { long now = System.currentTimeMillis(); SearchPoiTypeFilter poiTypeFilter = new SearchPoiTypeFilter(){ @@ -119,71 +105,5 @@ public class AmenityIndexRepositoryBinary implements AmenityIndexRepository { } return amenities; } - - - - - - // Work with cache (for map copied from AmenityIndexRepositoryOdb) - private String cFilterId; - protected List cachedObjects = new ArrayList(); - protected double cTopLatitude; - protected double cBottomLatitude; - protected double cLeftLongitude; - protected double cRightLongitude; - protected int cZoom; - - @Override - public synchronized boolean checkCachedAmenities(double topLatitude, double leftLongitude, double bottomLatitude, double rightLongitude, - int zoom, String filterId, List toFill, boolean fillFound){ - boolean inside = cTopLatitude >= topLatitude && cLeftLongitude <= leftLongitude && cRightLongitude >= rightLongitude - && cBottomLatitude <= bottomLatitude && zoom == cZoom; - boolean noNeedToSearch = inside && Algorithms.objectEquals(filterId, cFilterId); - if((inside || fillFound) && toFill != null && Algorithms.objectEquals(filterId, cFilterId)){ - for(Amenity a : cachedObjects){ - LatLon location = a.getLocation(); - if (location.getLatitude() <= topLatitude && location.getLongitude() >= leftLongitude && location.getLongitude() <= rightLongitude - && location.getLatitude() >= bottomLatitude) { - toFill.add(a); - } - } - } - return noNeedToSearch; - } - - @Override - public void clearCache() { - cachedObjects.clear(); - cTopLatitude = 0; - cBottomLatitude = 0; - cRightLongitude = 0; - cLeftLongitude = 0; - cZoom = 0; - cFilterId = null; - } - - @Override - public void evaluateCachedAmenities(double topLatitude, double leftLongitude, double bottomLatitude, double rightLongitude, int zoom, - PoiFilter filter, ResultMatcher matcher) { - cTopLatitude = topLatitude ; - cBottomLatitude = bottomLatitude ; - cLeftLongitude = leftLongitude ; - cRightLongitude = rightLongitude ; - cFilterId = filter == null ? null : filter.getFilterId(); - cZoom = zoom; - // first of all put all entities in temp list in order to not freeze other read threads - ArrayList tempList = new ArrayList(); - int sleft = MapUtils.get31TileNumberX(cLeftLongitude); - int sright = MapUtils.get31TileNumberX(cRightLongitude); - int sbottom = MapUtils.get31TileNumberY(cBottomLatitude); - int stop = MapUtils.get31TileNumberY(cTopLatitude); - searchAmenities(stop, sleft, sbottom, sright, zoom, filter, tempList, matcher); - synchronized (this) { - cachedObjects.clear(); - cachedObjects.addAll(tempList); - } - - } - } diff --git a/OsmAnd/src/net/osmand/plus/resources/AsyncLoadingThread.java b/OsmAnd/src/net/osmand/plus/resources/AsyncLoadingThread.java index 5ef820490f..5a7192b0c3 100644 --- a/OsmAnd/src/net/osmand/plus/resources/AsyncLoadingThread.java +++ b/OsmAnd/src/net/osmand/plus/resources/AsyncLoadingThread.java @@ -8,16 +8,12 @@ import java.util.Stack; import net.osmand.PlatformUtil; import net.osmand.ResultMatcher; -import net.osmand.data.Amenity; import net.osmand.data.RotatedTileBox; import net.osmand.data.TransportStop; import net.osmand.map.ITileSource; import net.osmand.map.MapTileDownloader.DownloadRequest; import net.osmand.map.MapTileDownloader.IMapDownloaderCallback; import net.osmand.plus.BusyIndicator; -import net.osmand.plus.OsmAndFormatter; -import net.osmand.plus.PoiFilter; -import net.osmand.util.Algorithms; import org.apache.commons.logging.Log; @@ -37,7 +33,6 @@ public class AsyncLoadingThread extends Thread { private Handler asyncLoadingTransport; Stack requests = new Stack(); - AmenityLoadRequest poiLoadRequest = null; TransportLoadRequest transportLoadRequest = null; @@ -68,8 +63,6 @@ public class AsyncLoadingThread extends Thread { progress = BusyIndicator.STATUS_ORANGE; } else if (!requests.isEmpty()) { progress = BusyIndicator.STATUS_BLACK; - } else if (poiLoadRequest != null && poiLoadRequest.isRunning()) { - progress = BusyIndicator.STATUS_BLACK; } else if (transportLoadRequest != null && transportLoadRequest.isRunning()) { progress = BusyIndicator.STATUS_BLACK; } @@ -96,18 +89,6 @@ public class AsyncLoadingThread extends Thread { if (req instanceof TileLoadDownloadRequest) { TileLoadDownloadRequest r = (TileLoadDownloadRequest) req; tileLoaded |= resourceManger.getRequestedImageTile(r) != null; - } else if (req instanceof AmenityLoadRequest) { - if (!amenityLoaded) { - if (poiLoadRequest == null || asyncLoadingPoi == null) { - startPoiLoadingThread(); - poiLoadRequest = (AmenityLoadRequest) req; - asyncLoadingPoi.post(poiLoadRequest.prepareToRun()); - } else if (poiLoadRequest.recalculateRequest((AmenityLoadRequest) req)) { - poiLoadRequest = (AmenityLoadRequest) req; - asyncLoadingPoi.post(poiLoadRequest.prepareToRun()); - } - amenityLoaded = true; - } } else if (req instanceof TransportLoadRequest) { if (!transportLoaded) { if (transportLoadRequest == null || asyncLoadingTransport == null) { @@ -155,10 +136,6 @@ public class AsyncLoadingThread extends Thread { requests.push(req); } - public void requestToLoadAmenities(AmenityLoadRequest req) { - requests.push(req); - } - public void requestToLoadMap(MapLoadRequest req) { requests.push(req); } @@ -240,72 +217,7 @@ public class AsyncLoadingThread extends Thread { } - protected class AmenityLoadRequest extends MapObjectLoadRequest { - private final List res; - private final PoiFilter filter; - private final int zoom; - private String filterByName; - - public AmenityLoadRequest(List repos, int zoom, PoiFilter filter, String nameFilter) { - super(); - this.res = repos; - this.zoom = zoom; - this.filter = filter; - this.filterByName = nameFilter; - if(this.filterByName != null) { - this.filterByName = this.filterByName.toLowerCase().trim(); - } - } - - @Override - public boolean publish(Amenity object) { - if(filterByName == null || filterByName.length() == 0) { - return true; - } else { - String lower = OsmAndFormatter.getPoiStringWithoutType(object, resourceManger.getContext().getSettings().usingEnglishNames()).toLowerCase(); - return lower.indexOf(filterByName) != -1; - } - } - - public Runnable prepareToRun() { - final double ntopLatitude = topLatitude + (topLatitude - bottomLatitude) / 2; - final double nbottomLatitude = bottomLatitude - (topLatitude - bottomLatitude) / 2; - final double nleftLongitude = leftLongitude - (rightLongitude - leftLongitude) / 2; - final double nrightLongitude = rightLongitude + (rightLongitude - leftLongitude) / 2; - setBoundaries(ntopLatitude, nleftLongitude, nbottomLatitude, nrightLongitude); - return new Runnable() { - @Override - public void run() { - start(); - try { - for (AmenityIndexRepository repository : res) { - repository.evaluateCachedAmenities(ntopLatitude, nleftLongitude, nbottomLatitude, nrightLongitude, zoom, - filter, AmenityLoadRequest.this); - } - } finally { - finish(); - } - } - }; - } - - private boolean repoHasChange() { - for (AmenityIndexRepository r : res) { - if (r.hasChange()) { - r.clearChange(); - return true; - } - } - return false; - } - public boolean recalculateRequest(AmenityLoadRequest req) { - if (this.zoom != req.zoom || !Algorithms.objectEquals(this.filter, req.filter) || req.repoHasChange()) { - return true; - } - return !isContains(req.topLatitude, req.leftLongitude, req.bottomLatitude, req.rightLongitude); - } - - } + protected class TransportLoadRequest extends MapObjectLoadRequest { private final List repos; diff --git a/OsmAnd/src/net/osmand/plus/resources/ResourceManager.java b/OsmAnd/src/net/osmand/plus/resources/ResourceManager.java index 8a98629300..eb87652d0f 100644 --- a/OsmAnd/src/net/osmand/plus/resources/ResourceManager.java +++ b/OsmAnd/src/net/osmand/plus/resources/ResourceManager.java @@ -1,6 +1,8 @@ package net.osmand.plus.resources; +import gnu.trove.list.array.TIntArrayList; + import java.io.File; import java.io.FileOutputStream; import java.io.IOException; @@ -36,6 +38,7 @@ import net.osmand.map.MapTileDownloader.DownloadRequest; import net.osmand.map.OsmandRegions; import net.osmand.plus.BusyIndicator; import net.osmand.plus.NameFinderPoiFilter; +import net.osmand.plus.OsmAndFormatter; import net.osmand.plus.OsmandApplication; import net.osmand.plus.OsmandPlugin; import net.osmand.plus.PoiFilter; @@ -45,7 +48,6 @@ import net.osmand.plus.SearchByNameFilter; import net.osmand.plus.Version; import net.osmand.plus.render.MapRenderRepositories; import net.osmand.plus.render.NativeOsmandLibrary; -import net.osmand.plus.resources.AsyncLoadingThread.AmenityLoadRequest; import net.osmand.plus.resources.AsyncLoadingThread.MapLoadRequest; import net.osmand.plus.resources.AsyncLoadingThread.TileLoadDownloadRequest; import net.osmand.plus.resources.AsyncLoadingThread.TransportLoadRequest; @@ -702,17 +704,57 @@ public class ResourceManager { } ////////////////////////////////////////////// Working with amenities //////////////////////////////////////////////// + public boolean checkNameFilter(Amenity object, String filterByName) { + boolean publish = false; + if (filterByName == null || filterByName.length() == 0) { + publish = true; + } else { + String lower = OsmAndFormatter.getPoiStringWithoutType(object, context.getSettings().usingEnglishNames()) + .toLowerCase(); + publish = lower.indexOf(filterByName) != -1; + } + return publish; + } + public List searchAmenities(PoiFilter filter, - double topLatitude, double leftLongitude, double bottomLatitude, double rightLongitude, - double lat, double lon, ResultMatcher matcher) { - List amenities = new ArrayList(); - for (AmenityIndexRepository index : amenityRepositories) { - if (index.checkContains(topLatitude, leftLongitude, bottomLatitude, rightLongitude)) { - index.searchAmenities(MapUtils.get31TileNumberY(topLatitude), MapUtils.get31TileNumberX(leftLongitude), - MapUtils.get31TileNumberY(bottomLatitude), MapUtils.get31TileNumberX(rightLongitude), -1, filter, amenities, matcher); + double topLatitude, double leftLongitude, double bottomLatitude, double rightLongitude, final ResultMatcher matcher) { + final List amenities = new ArrayList(); + if(filter instanceof NameFinderPoiFilter || filter instanceof SearchByNameFilter){ + List tempResults = filter instanceof NameFinderPoiFilter ? + ((NameFinderPoiFilter) filter).getSearchedAmenities() :((SearchByNameFilter) filter).getSearchedAmenities() ; + for(Amenity a : tempResults){ + LatLon l = a.getLocation(); + if(l != null && l.getLatitude() <= topLatitude && l.getLatitude() >= bottomLatitude && l.getLongitude() >= leftLongitude && l.getLongitude() <= rightLongitude){ + if(matcher.publish(a) ){ + amenities.add(a); + } + } + } + } else { + final String filterByName = filter.getFilterByName(); + for (AmenityIndexRepository index : amenityRepositories) { + if (index.checkContains(topLatitude, leftLongitude, bottomLatitude, rightLongitude)) { + index.searchAmenities(MapUtils.get31TileNumberY(topLatitude), + MapUtils.get31TileNumberX(leftLongitude), MapUtils.get31TileNumberY(bottomLatitude), + MapUtils.get31TileNumberX(rightLongitude), -1, filter, amenities, + new ResultMatcher() { + + @Override + public boolean publish(Amenity object) { + if (checkNameFilter(object, filterByName)) { + return matcher.publish(object); + } + return false; + } + + @Override + public boolean isCancelled() { + return matcher.isCancelled(); + } + }); + } } } - return amenities; } @@ -772,31 +814,31 @@ public class ResourceManager { return map; } - public void searchAmenitiesAsync(double topLatitude, double leftLongitude, double bottomLatitude, double rightLongitude, int zoom, PoiFilter filter, List toFill){ - if(filter instanceof NameFinderPoiFilter || filter instanceof SearchByNameFilter){ - List amenities = filter instanceof NameFinderPoiFilter ? - ((NameFinderPoiFilter) filter).getSearchedAmenities() :((SearchByNameFilter) filter).getSearchedAmenities() ; - for(Amenity a : amenities){ - LatLon l = a.getLocation(); - if(l != null && l.getLatitude() <= topLatitude && l.getLatitude() >= bottomLatitude && l.getLongitude() >= leftLongitude && l.getLongitude() <= rightLongitude){ - toFill.add(a); - } - } - } else { - String filterId = filter == null ? null : filter.getFilterId(); + public void searchAmenitiesOnTheArea(TIntArrayList tiles16z, PoiFilter filter, ResultMatcher results) { + if (tiles16z.size() > 0) { + int z = 16; + int x = tiles16z.get(0) >> z; + int y = tiles16z.get(0) & ((1 << z) - 1); List repos = new ArrayList(); + double topLatitude = MapUtils.getLatitudeFromTile(z, y); + double bottomLatitude = MapUtils.getLatitudeFromTile(z, y + 1); + double leftLongitude = MapUtils.getLongitudeFromTile(z, x); + double rightLongitude = MapUtils.getLongitudeFromTile(z, x + 1); + for (int k = 1; k < tiles16z.size(); k++) { + topLatitude = Math.max(topLatitude, MapUtils.getLatitudeFromTile(z, y)); + bottomLatitude = Math.min(bottomLatitude, MapUtils.getLatitudeFromTile(z, y + 1)); + leftLongitude = Math.min(leftLongitude, MapUtils.getLongitudeFromTile(z, x)); + rightLongitude = Math.max(rightLongitude, MapUtils.getLongitudeFromTile(z, x + 1)); + } for (AmenityIndexRepository index : amenityRepositories) { if (index.checkContains(topLatitude, leftLongitude, bottomLatitude, rightLongitude)) { - if (!index.checkCachedAmenities(topLatitude, leftLongitude, bottomLatitude, rightLongitude, zoom, filterId, toFill, - true)) { - repos.add(index); - } + repos.add(index); } } - if(!repos.isEmpty()){ - AmenityLoadRequest req = asyncLoadingThread.new AmenityLoadRequest(repos, zoom, filter, filter.getFilterByName()); - req.setBoundaries(topLatitude, leftLongitude, bottomLatitude, rightLongitude); - asyncLoadingThread.requestToLoadAmenities(req); + if (!repos.isEmpty()) { + for(AmenityIndexRepository r : repos) { +// r.searchAmenities(stop, sleft, sbottom, sright, zoom, filter, amenities, matcher) + } } } } @@ -811,6 +853,7 @@ public class ResourceManager { return addressMap.values(); } + ////////////////////////////////////////////// Working with transport //////////////////////////////////////////////// public List searchTransportRepositories(double latitude, double longitude) { List repos = new ArrayList(); @@ -950,9 +993,6 @@ public class ResourceManager { public void onLowMemory() { log.info("On low memory : cleaning tiles - size = " + cacheOfImages.size()); //$NON-NLS-1$ clearTiles(); - for(AmenityIndexRepository r : amenityRepositories){ - r.clearCache(); - } for(RegionAddressRepository r : addressMap.values()){ r.clearCache(); } diff --git a/OsmAnd/src/net/osmand/plus/routing/RoutingHelper.java b/OsmAnd/src/net/osmand/plus/routing/RoutingHelper.java index 876c1864db..70554ee637 100644 --- a/OsmAnd/src/net/osmand/plus/routing/RoutingHelper.java +++ b/OsmAnd/src/net/osmand/plus/routing/RoutingHelper.java @@ -19,6 +19,7 @@ import net.osmand.plus.OsmAndFormatter; import net.osmand.plus.OsmandApplication; import net.osmand.plus.OsmandPlugin; import net.osmand.plus.OsmandSettings; +import net.osmand.plus.SearchOnTheRouteHelper; import net.osmand.plus.OsmandSettings.MetricsConstants; import net.osmand.plus.R; import net.osmand.plus.TargetPointsHelper; @@ -78,6 +79,7 @@ public class RoutingHelper { //private long wrongMovementDetected = 0; private RouteCalculationProgressCallback progressRoute; + private SearchOnTheRouteHelper searchOnTheRouteHelper; // private ProgressBar progress; // private Handler progressHandler; @@ -89,8 +91,13 @@ public class RoutingHelper { public RoutingHelper(OsmandApplication context, CommandPlayer player){ this.app = context; settings = context.getSettings(); + searchOnTheRouteHelper = new SearchOnTheRouteHelper(context); voiceRouter = new VoiceRouter(this, settings, player); } + + public SearchOnTheRouteHelper getSearchOnTheRouteHelper() { + return searchOnTheRouteHelper; + } public boolean isFollowingMode() { return isFollowingMode; @@ -568,6 +575,8 @@ public class RoutingHelper { voiceRouter.newRouteIsCalculated(newRoute); } } + + searchOnTheRouteHelper.searchOnTheRoute(route); app.runInUIThread(new Runnable() { @Override diff --git a/OsmAnd/src/net/osmand/plus/views/POIMapLayer.java b/OsmAnd/src/net/osmand/plus/views/POIMapLayer.java index 5fc8358c88..1e640a74d1 100644 --- a/OsmAnd/src/net/osmand/plus/views/POIMapLayer.java +++ b/OsmAnd/src/net/osmand/plus/views/POIMapLayer.java @@ -1,9 +1,11 @@ package net.osmand.plus.views; import java.util.ArrayList; +import java.util.Collections; import java.util.List; import net.osmand.PlatformUtil; +import net.osmand.ResultMatcher; import net.osmand.access.AccessibleToast; import net.osmand.data.Amenity; import net.osmand.data.AmenityType; @@ -43,14 +45,46 @@ public class POIMapLayer extends OsmandMapLayer implements ContextMenuLayer.ICon private Paint paintIcon; private Paint point; private OsmandMapTileView view; - private List objects = new ArrayList(); private final static int MAXIMUM_SHOW_AMENITIES = 5; private ResourceManager resourceManager; private PoiFilter filter; private MapTextLayer mapTextLayer; + + /// cache for displayed POI + // Work with cache (for map copied from AmenityIndexRepositoryOdb) + private MapLayerData> data; + public POIMapLayer(MapActivity activity) { + data = new OsmandMapLayer.MapLayerData>() { + { + ZOOM_THRESHOLD = 0; + } + + @Override + public boolean isInterrupted() { + return super.isInterrupted(); + } + + @Override + protected List calculateResult(RotatedTileBox tileBox) { + QuadRect latLonBounds = tileBox.getLatLonBounds(); + return resourceManager.searchAmenities(filter, latLonBounds.top, latLonBounds.left, + latLonBounds.bottom, latLonBounds.right, new ResultMatcher() { + + @Override + public boolean publish(Amenity object) { + return true; + } + + @Override + public boolean isCancelled() { + return isInterrupted(); + } + }); + } + }; } public PoiFilter getFilter() { @@ -59,9 +93,11 @@ public class POIMapLayer extends OsmandMapLayer implements ContextMenuLayer.ICon public void setFilter(PoiFilter filter) { this.filter = filter; + data.clearCache(); } public void getAmenityFromPoint(RotatedTileBox tb, PointF point, List am) { + List objects = data.getResults(); if (objects != null) { int ex = (int) point.x; int ey = (int) point.y; @@ -126,6 +162,7 @@ public class POIMapLayer extends OsmandMapLayer implements ContextMenuLayer.ICon resourceManager = view.getApplication().getResourceManager(); mapTextLayer = view.getLayerByClass(MapTextLayer.class); } + public int getRadiusPoi(RotatedTileBox tb) { int r = 0; @@ -146,30 +183,33 @@ public class POIMapLayer extends OsmandMapLayer implements ContextMenuLayer.ICon @Override public void onPrepareBufferImage(Canvas canvas, RotatedTileBox tileBox, DrawSettings settings) { - objects.clear(); + List objects = Collections.emptyList(); if (tileBox.getZoom() >= startZoom) { - final QuadRect latLonBounds = tileBox.getLatLonBounds(); - resourceManager.searchAmenitiesAsync(latLonBounds.top, latLonBounds.left, latLonBounds.bottom, - latLonBounds.right, tileBox.getZoom(), filter, objects); - int r = getRadiusPoi(tileBox); - for (Amenity o : objects) { - int x = (int) tileBox.getPixXFromLatLon(o.getLocation().getLatitude(), o.getLocation().getLongitude()); - int y = (int) tileBox.getPixYFromLatLon(o.getLocation().getLatitude(), o.getLocation().getLongitude()); - canvas.drawCircle(x, y, r, pointAltUI); - canvas.drawCircle(x, y, r, point); - String id = null; - StringBuilder tag = new StringBuilder(); - StringBuilder value = new StringBuilder(); - MapRenderingTypes.getDefault().getAmenityTagValue(o.getType(), o.getSubType(), tag, value); - if (RenderingIcons.containsIcon(tag + "_" + value)) { - id = tag + "_" + value; - } else if (RenderingIcons.containsIcon(tag.toString())) { - id = tag.toString(); - } - if (id != null) { - Bitmap bmp = RenderingIcons.getIcon(view.getContext(), id); - if (bmp != null) { - canvas.drawBitmap(bmp, x - bmp.getWidth() / 2, y - bmp.getHeight() / 2, paintIcon); + data.queryNewData(tileBox); + objects = data.getResults(); + if (objects != null) { + int r = getRadiusPoi(tileBox); + for (Amenity o : objects) { + int x = (int) tileBox.getPixXFromLatLon(o.getLocation().getLatitude(), o.getLocation() + .getLongitude()); + int y = (int) tileBox.getPixYFromLatLon(o.getLocation().getLatitude(), o.getLocation() + .getLongitude()); + canvas.drawCircle(x, y, r, pointAltUI); + canvas.drawCircle(x, y, r, point); + String id = null; + StringBuilder tag = new StringBuilder(); + StringBuilder value = new StringBuilder(); + MapRenderingTypes.getDefault().getAmenityTagValue(o.getType(), o.getSubType(), tag, value); + if (RenderingIcons.containsIcon(tag + "_" + value)) { + id = tag + "_" + value; + } else if (RenderingIcons.containsIcon(tag.toString())) { + id = tag.toString(); + } + if (id != null) { + Bitmap bmp = RenderingIcons.getIcon(view.getContext(), id); + if (bmp != null) { + canvas.drawBitmap(bmp, x - bmp.getWidth() / 2, y - bmp.getHeight() / 2, paintIcon); + } } } } From 92124a598351dd1e5473d1d3c2a1df05ba8cee9d Mon Sep 17 00:00:00 2001 From: Victor Shcherb Date: Sun, 3 Aug 2014 11:03:13 +0200 Subject: [PATCH 2/2] Revert fix for POI button config, because it causes troubles for waypoint dialog -checkbox --- OsmAnd/res/layout/list_menu_item.xml | 1 - OsmAnd/res/layout/list_menu_item_native.xml | 3 +-- 2 files changed, 1 insertion(+), 3 deletions(-) diff --git a/OsmAnd/res/layout/list_menu_item.xml b/OsmAnd/res/layout/list_menu_item.xml index f2bf19d569..59c76e0f15 100644 --- a/OsmAnd/res/layout/list_menu_item.xml +++ b/OsmAnd/res/layout/list_menu_item.xml @@ -48,7 +48,6 @@ android:layout_marginRight="2dip" android:button="@drawable/ic_btn_wocheckbox" android:focusable="false" - android:clickable="false" /> diff --git a/OsmAnd/res/layout/list_menu_item_native.xml b/OsmAnd/res/layout/list_menu_item_native.xml index a4676522cf..e4c0daf796 100644 --- a/OsmAnd/res/layout/list_menu_item_native.xml +++ b/OsmAnd/res/layout/list_menu_item_native.xml @@ -46,8 +46,7 @@ android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginRight="2dip" - android:focusable="false" - android:clickable="false"/> + android:focusable="false"/> \ No newline at end of file