Small refactoring

This commit is contained in:
Victor Shcherb 2011-08-24 00:32:25 +02:00
parent 53bc7a478c
commit c091d0474f
2 changed files with 37 additions and 32 deletions

View file

@ -35,6 +35,7 @@ import android.content.Context;
import android.content.DialogInterface; import android.content.DialogInterface;
import android.content.Intent; import android.content.Intent;
import android.content.res.Configuration; import android.content.res.Configuration;
import android.os.AsyncTask;
import android.os.Handler; import android.os.Handler;
import android.text.format.DateFormat; import android.text.format.DateFormat;
import android.util.Log; import android.util.Log;

View file

@ -6,7 +6,6 @@ import java.net.MalformedURLException;
import java.net.URL; import java.net.URL;
import java.net.URLConnection; import java.net.URLConnection;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collection;
import java.util.List; import java.util.List;
import java.util.Locale; import java.util.Locale;
@ -23,7 +22,6 @@ import net.osmand.osm.MapUtils;
import net.osmand.plus.R; import net.osmand.plus.R;
import net.osmand.plus.activities.RoutingHelper.RouteDirectionInfo; import net.osmand.plus.activities.RoutingHelper.RouteDirectionInfo;
import net.osmand.plus.activities.RoutingHelper.TurnType; import net.osmand.plus.activities.RoutingHelper.TurnType;
import net.osmand.plus.render.MapRenderRepositories;
import net.osmand.router.BicycleRouter; import net.osmand.router.BicycleRouter;
import net.osmand.router.BinaryRoutePlanner; import net.osmand.router.BinaryRoutePlanner;
import net.osmand.router.CarRouter; import net.osmand.router.CarRouter;
@ -180,35 +178,7 @@ public class RouteProvider {
try { try {
RouteCalculationResult res; RouteCalculationResult res;
if(gpxRoute != null && !gpxRoute.isEmpty()){ if(gpxRoute != null && !gpxRoute.isEmpty()){
// get the closest point to start and to end res = calculateGpxRoute(start, end, gpxRoute);
float minDist = Integer.MAX_VALUE;
int startI = 0;
int endI = gpxRoute.size();
if (start != null) {
for (int i = 0; i < gpxRoute.size(); i++) {
float d = gpxRoute.get(i).distanceTo(start);
if (d < minDist) {
startI = i;
minDist = d;
}
}
} else {
start = gpxRoute.get(0);
}
Location l = new Location("temp"); //$NON-NLS-1$
l.setLatitude(end.getLatitude());
l.setLongitude(end.getLongitude());
minDist = Integer.MAX_VALUE;
// get in reverse order taking into account cycle ways
for (int i = gpxRoute.size() - 1; i >= startI; i--) {
float d = gpxRoute.get(i).distanceTo(l);
if (d < minDist) {
endI = i + 1;
// slightly modify to allow last point to be added
minDist = d - 40;
}
}
res = new RouteCalculationResult(new ArrayList<Location>(gpxRoute.subList(startI, endI)), null, start, end, null);
addMissingTurnsToRoute(res, start, end, mode, ctx); addMissingTurnsToRoute(res, start, end, mode, ctx);
} else if (type == RouteService.YOURS) { } else if (type == RouteService.YOURS) {
res = findYOURSRoute(start, end, mode, fast); res = findYOURSRoute(start, end, mode, fast);
@ -235,6 +205,40 @@ public class RouteProvider {
} }
return new RouteCalculationResult(null); return new RouteCalculationResult(null);
} }
private RouteCalculationResult calculateGpxRoute(Location start, LatLon end, List<Location> gpxRoute) {
RouteCalculationResult res;
// get the closest point to start and to end
float minDist = Integer.MAX_VALUE;
int startI = 0;
int endI = gpxRoute.size();
if (start != null) {
for (int i = 0; i < gpxRoute.size(); i++) {
float d = gpxRoute.get(i).distanceTo(start);
if (d < minDist) {
startI = i;
minDist = d;
}
}
} else {
start = gpxRoute.get(0);
}
Location l = new Location("temp"); //$NON-NLS-1$
l.setLatitude(end.getLatitude());
l.setLongitude(end.getLongitude());
minDist = Integer.MAX_VALUE;
// get in reverse order taking into account cycle ways
for (int i = gpxRoute.size() - 1; i >= startI; i--) {
float d = gpxRoute.get(i).distanceTo(l);
if (d < minDist) {
endI = i + 1;
// slightly modify to allow last point to be added
minDist = d - 40;
}
}
res = new RouteCalculationResult(new ArrayList<Location>(gpxRoute.subList(startI, endI)), null, start, end, null);
return res;
}
protected String getString(Context ctx, int resId){ protected String getString(Context ctx, int resId){
if(ctx == null){ if(ctx == null){
@ -579,7 +583,7 @@ public class RouteProvider {
URLConnection connection = url.openConnection(); URLConnection connection = url.openConnection();
DocumentBuilder dom = DocumentBuilderFactory.newInstance().newDocumentBuilder(); DocumentBuilder dom = DocumentBuilderFactory.newInstance().newDocumentBuilder();
Document doc = dom.parse(new InputSource(new InputStreamReader(connection.getInputStream()))); Document doc = dom.parse(new InputSource(new InputStreamReader(connection.getInputStream())));
// TODO how to find that error occurred ? API gpx doesn't say nothing // TODO how to find that error occurred ? Gpx API doesn't say anything
NodeList list = doc.getElementsByTagName("wpt"); //$NON-NLS-1$ NodeList list = doc.getElementsByTagName("wpt"); //$NON-NLS-1$
for (int i = 0; i < list.getLength(); i++) { for (int i = 0; i < list.getLength(); i++) {
Element item = (Element) list.item(i); Element item = (Element) list.item(i);