Fix issue 471. Store gpx layer

This commit is contained in:
Victor Shcherb 2011-06-16 20:06:21 +02:00
parent f4e268275e
commit 57bb4d9fcf
5 changed files with 126 additions and 66 deletions

View file

@ -0,0 +1,18 @@
package net.osmand;
/**
* That class is designed to be common interface for callbacks with one param
* @author victor
* @param <T>
*/
public interface CallbackWithObject<T> {
/**
* Calls on processing result
* @param result could be null depends on usage
* @return processed or not
*/
public boolean processResult(T result);
}

View file

@ -5,6 +5,7 @@ import java.util.Collections;
import java.util.List; import java.util.List;
import net.osmand.Algoritms; import net.osmand.Algoritms;
import net.osmand.CallbackWithObject;
import net.osmand.LogUtil; import net.osmand.LogUtil;
import net.osmand.Version; import net.osmand.Version;
import net.osmand.GPXUtilities.GPXFileResult; import net.osmand.GPXUtilities.GPXFileResult;
@ -1006,7 +1007,7 @@ public class MapActivity extends Activity implements IMapLocationListener, Senso
} }
mapView.refreshMap(); mapView.refreshMap();
} else if (item.getItemId() == R.id.map_gpx_routing) { } else if (item.getItemId() == R.id.map_gpx_routing) {
mapLayers.useGPXFileLayer(true, mapLayers.getNavigationLayer().getPointToNavigate(), mapView); useGPXRouting();
return true; return true;
} else if (item.getItemId() == R.id.map_show_point_options) { } else if (item.getItemId() == R.id.map_show_point_options) {
contextMenuPoint(mapView.getLatitude(), mapView.getLongitude()); contextMenuPoint(mapView.getLatitude(), mapView.getLongitude());
@ -1169,44 +1170,54 @@ public class MapActivity extends Activity implements IMapLocationListener, Senso
return ((OsmandApplication)getApplication()).getFavorites(); return ((OsmandApplication)getApplication()).getFavorites();
} }
public void useGPXRouting(final LatLon endForRouting, final GPXFileResult res) { private void useGPXRouting() {
Builder builder = new AlertDialog.Builder(this); final LatLon endForRouting = getPointToNavigate();
builder.setItems(new String[]{getString(R.string.gpx_direct_route), getString(R.string.gpx_reverse_route)}, mapLayers.selectGPXFileLayer(new CallbackWithObject<GPXFileResult>() {
new DialogInterface.OnClickListener() {
@Override @Override
public void onClick(DialogInterface dialog, int which) { public boolean processResult(final GPXFileResult result) {
boolean reverse = which == 1; Builder builder = new AlertDialog.Builder(MapActivity.this);
ArrayList<List<Location>> locations = res.locations; builder.setItems(new String[]{getString(R.string.gpx_direct_route), getString(R.string.gpx_reverse_route)},
List<Location> l = new ArrayList<Location>(); new DialogInterface.OnClickListener() {
for(List<Location> s : locations){
l.addAll(s); @Override
} public void onClick(DialogInterface dialog, int which) {
if(reverse){ boolean reverse = which == 1;
Collections.reverse(l); ArrayList<List<Location>> locations = result.locations;
} List<Location> l = new ArrayList<Location>();
Location startForRouting = getLastKnownLocation(); for(List<Location> s : locations){
if(startForRouting == null && !l.isEmpty()){ l.addAll(s);
startForRouting = l.get(0); }
} if(reverse){
LatLon endPoint = endForRouting; Collections.reverse(l);
if(/*endForRouting == null && */!l.isEmpty()){ }
LatLon point = new LatLon(l.get(l.size() - 1).getLatitude(), l.get(l.size() - 1).getLongitude()); Location startForRouting = getLastKnownLocation();
settings.setPointToNavigate(point.getLatitude(), point.getLongitude()); if(startForRouting == null && !l.isEmpty()){
endPoint = point; startForRouting = l.get(0);
mapLayers.getNavigationLayer().setPointToNavigate(point); }
} LatLon endPoint = endForRouting;
if(endForRouting != null){ if(/*endForRouting == null && */!l.isEmpty()){
settings.FOLLOW_TO_THE_ROUTE.set(true); LatLon point = new LatLon(l.get(l.size() - 1).getLatitude(), l.get(l.size() - 1).getLongitude());
routingHelper.setFollowingMode(true); settings.setPointToNavigate(point.getLatitude(), point.getLongitude());
routingHelper.setFinalAndCurrentLocation(endPoint, startForRouting, l); endPoint = point;
((OsmandApplication)getApplication()).showDialogInitializingCommandPlayer(MapActivity.this); mapLayers.getNavigationLayer().setPointToNavigate(point);
} }
mapView.refreshMap();
if(endForRouting != null){
settings.FOLLOW_TO_THE_ROUTE.set(true);
routingHelper.setFollowingMode(true);
routingHelper.setFinalAndCurrentLocation(endPoint, startForRouting, l);
((OsmandApplication)getApplication()).showDialogInitializingCommandPlayer(MapActivity.this);
}
}
});
builder.show();
return true;
} }
}); });
builder.show();
} }
public void contextMenuPoint(final double latitude, final double longitude){ public void contextMenuPoint(final double latitude, final double longitude){

View file

@ -8,14 +8,12 @@ import java.util.List;
import java.util.Map; import java.util.Map;
import net.osmand.Algoritms; import net.osmand.Algoritms;
import net.osmand.FavouritePoint; import net.osmand.CallbackWithObject;
import net.osmand.GPXUtilities; import net.osmand.GPXUtilities;
import net.osmand.OsmAndFormatter; import net.osmand.OsmAndFormatter;
import net.osmand.GPXUtilities.GPXFileResult; import net.osmand.GPXUtilities.GPXFileResult;
import net.osmand.GPXUtilities.WptPt;
import net.osmand.data.AmenityType; import net.osmand.data.AmenityType;
import net.osmand.map.ITileSource; import net.osmand.map.ITileSource;
import net.osmand.osm.LatLon;
import net.osmand.plus.OsmandSettings; import net.osmand.plus.OsmandSettings;
import net.osmand.plus.PoiFilter; import net.osmand.plus.PoiFilter;
import net.osmand.plus.PoiFiltersHelper; import net.osmand.plus.PoiFiltersHelper;
@ -47,7 +45,6 @@ import android.app.ProgressDialog;
import android.app.AlertDialog.Builder; import android.app.AlertDialog.Builder;
import android.content.DialogInterface; import android.content.DialogInterface;
import android.content.Intent; import android.content.Intent;
import android.os.Looper;
import android.widget.LinearLayout; import android.widget.LinearLayout;
import android.widget.Toast; import android.widget.Toast;
@ -184,6 +181,7 @@ public class MapActivityLayers {
mapView.removeLayer(favoritesLayer); mapView.removeLayer(favoritesLayer);
} }
} }
updateGPXLayer();
trafficLayer.setVisible(settings.SHOW_YANDEX_TRAFFIC.get()); trafficLayer.setVisible(settings.SHOW_YANDEX_TRAFFIC.get());
} }
@ -298,11 +296,11 @@ public class MapActivityLayers {
settings.SHOW_FAVORITES.set(isChecked); settings.SHOW_FAVORITES.set(isChecked);
} else if(item == 5){ } else if(item == 5){
if(gpxLayer.isVisible()){ if(gpxLayer.isVisible()){
getApplication().setGpxFileToDisplay(null);
gpxLayer.clearCurrentGPX(); gpxLayer.clearCurrentGPX();
getApplication().getFavorites().setFavoritePointsFromGPXFile(null);
} else { } else {
dialog.dismiss(); dialog.dismiss();
useGPXFileLayer(false, null, mapView); showGPXFileLayer(mapView);
} }
} else if(item == 6){ } else if(item == 6){
if(overlayLayer.getMap() != null){ if(overlayLayer.getMap() != null){
@ -336,7 +334,32 @@ public class MapActivityLayers {
builder.show(); builder.show();
} }
public void useGPXFileLayer(final boolean useRouting, final LatLon endForRouting, final OsmandMapTileView mapView) { public void showGPXFileLayer(final OsmandMapTileView mapView){
final OsmandSettings settings = getApplication().getSettings();
selectGPXFileLayer(new CallbackWithObject<GPXFileResult>() {
@Override
public boolean processResult(GPXFileResult result) {
settings.SHOW_FAVORITES.set(true);
if (result != null) {
getApplication().setGpxFileToDisplay(result);
updateGPXLayer();
mapView.refreshMap();
}
return true;
}
});
}
private void updateGPXLayer(){
GPXFileResult gpxFileToDisplay = getApplication().getGpxFileToDisplay();
if(gpxFileToDisplay == null){
gpxLayer.setTracks(null);
} else {
gpxLayer.setTracks(gpxFileToDisplay.locations);
}
}
public void selectGPXFileLayer(final CallbackWithObject<GPXFileResult> callbackWithObject) {
final List<String> list = new ArrayList<String>(); final List<String> list = new ArrayList<String>();
final OsmandSettings settings = getApplication().getSettings(); final OsmandSettings settings = getApplication().getSettings();
final File dir = settings.extendOsmandPath(ResourceManager.APP_DIR + SavingTrackHelper.TRACKS_PATH); final File dir = settings.extendOsmandPath(ResourceManager.APP_DIR + SavingTrackHelper.TRACKS_PATH);
@ -379,7 +402,6 @@ public class MapActivityLayers {
new Thread(new Runnable() { new Thread(new Runnable() {
@Override @Override
public void run() { public void run() {
Looper.prepare();
final GPXFileResult res = GPXUtilities.loadGPXFile(activity, f); final GPXFileResult res = GPXUtilities.loadGPXFile(activity, f);
if (res.error != null) { if (res.error != null) {
activity.runOnUiThread(new Runnable() { activity.runOnUiThread(new Runnable() {
@ -391,28 +413,13 @@ public class MapActivityLayers {
} }
dlg.dismiss(); dlg.dismiss();
if(useRouting){ activity.runOnUiThread(new Runnable() {
activity.runOnUiThread(new Runnable() { @Override
@Override public void run() {
public void run() { callbackWithObject.processResult(res);
activity.useGPXRouting(endForRouting, res);
}
});
} else {
settings.SHOW_FAVORITES.set(true);
List<FavouritePoint> pts = new ArrayList<FavouritePoint>();
for(WptPt p : res.wayPoints){
FavouritePoint pt = new FavouritePoint();
pt.setLatitude(p.lat);
pt.setLongitude(p.lon);
pt.setName(p.name);
pts.add(pt);
} }
getApplication().getFavorites().setFavoritePointsFromGPXFile(pts); });
gpxLayer.setTracks(res.locations);
}
mapView.refreshMap();
} }

View file

@ -6,11 +6,15 @@ import java.io.File;
import java.io.FileWriter; import java.io.FileWriter;
import java.io.PrintStream; import java.io.PrintStream;
import java.lang.Thread.UncaughtExceptionHandler; import java.lang.Thread.UncaughtExceptionHandler;
import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Locale; import java.util.Locale;
import net.osmand.Algoritms; import net.osmand.Algoritms;
import net.osmand.FavouritePoint;
import net.osmand.LogUtil; import net.osmand.LogUtil;
import net.osmand.GPXUtilities.GPXFileResult;
import net.osmand.GPXUtilities.WptPt;
import net.osmand.plus.FavouritesDbHelper; import net.osmand.plus.FavouritesDbHelper;
import net.osmand.plus.NavigationService; import net.osmand.plus.NavigationService;
import net.osmand.plus.OsmandSettings; import net.osmand.plus.OsmandSettings;
@ -53,6 +57,7 @@ public class OsmandApplication extends Application {
private List<String> startingWarnings; private List<String> startingWarnings;
private ProgressDialog progressDlg; private ProgressDialog progressDlg;
private Handler uiHandler; private Handler uiHandler;
private GPXFileResult gpxFileToDisplay;
private boolean applicationInitializing = false; private boolean applicationInitializing = false;
private Locale prefferedLocale = null; private Locale prefferedLocale = null;
@ -88,6 +93,27 @@ public class OsmandApplication extends Application {
} }
return poiFilters; return poiFilters;
} }
public void setGpxFileToDisplay(GPXFileResult gpxFileToDisplay) {
this.gpxFileToDisplay = gpxFileToDisplay;
if(gpxFileToDisplay == null){
getFavorites().setFavoritePointsFromGPXFile(null);
} else {
List<FavouritePoint> pts = new ArrayList<FavouritePoint>();
for (WptPt p : gpxFileToDisplay.wayPoints) {
FavouritePoint pt = new FavouritePoint();
pt.setLatitude(p.lat);
pt.setLongitude(p.lon);
pt.setName(p.name);
pts.add(pt);
}
getFavorites().setFavoritePointsFromGPXFile(pts);
}
}
public GPXFileResult getGpxFileToDisplay() {
return gpxFileToDisplay;
}
public FavouritesDbHelper getFavorites() { public FavouritesDbHelper getFavorites() {
if(favorites == null) { if(favorites == null) {

View file

@ -89,8 +89,6 @@ public class FavoritesLayer implements OsmandMapLayer, ContextMenuLayer.IContext
@Override @Override
public void onDraw(Canvas canvas, RectF latLonBounds, RectF tilesRect, boolean nightMode) { public void onDraw(Canvas canvas, RectF latLonBounds, RectF tilesRect, boolean nightMode) {
if (view.getZoom() >= startZoom) { if (view.getZoom() >= startZoom) {
// request to load // request to load
for (FavouritePoint o : favorites.getFavouritePoints()) { for (FavouritePoint o : favorites.getFavouritePoints()) {
if (o.getLatitude() >= latLonBounds.bottom && o.getLatitude() <= latLonBounds.top && o.getLongitude() >= latLonBounds.left if (o.getLatitude() >= latLonBounds.bottom && o.getLatitude() <= latLonBounds.top && o.getLongitude() >= latLonBounds.left