use of net.osmand.Location in project Osmand by osmandapp.
the class QuickSearchListAdapter method updateCompassVisibility.
private void updateCompassVisibility(View view, QuickSearchListItem listItem) {
View compassView = view.findViewById(R.id.compass_layout);
Location ll = app.getLocationProvider().getLastKnownLocation();
boolean showCompass = location != null && listItem.getSearchResult().location != null;
if ((ll != null || useMapCenter) && showCompass) {
updateDistanceDirection(view, listItem);
compassView.setVisibility(View.VISIBLE);
} else {
if (!showCompass) {
compassView.setVisibility(View.GONE);
} else {
compassView.setVisibility(View.INVISIBLE);
}
}
}
use of net.osmand.Location in project Osmand by osmandapp.
the class RouteCalculationResult method getDistanceToFinish.
public int getDistanceToFinish(Location fromLoc) {
if (listDistance != null && currentRoute < listDistance.length) {
int dist = listDistance[currentRoute];
Location l = locations.get(currentRoute);
if (fromLoc != null) {
dist += fromLoc.distanceTo(l);
}
return dist;
}
return 0;
}
use of net.osmand.Location in project Osmand by osmandapp.
the class RouteCalculationResult method introduceFirstPointAndLastPoint.
/**
* PREPARATION
* If beginning is too far from start point, then introduce GO Ahead
* @param end
*/
private static void introduceFirstPointAndLastPoint(List<Location> locations, List<RouteDirectionInfo> directions, List<RouteSegmentResult> segs, Location start, LatLon end) {
if (!locations.isEmpty() && locations.get(0).distanceTo(start) > 50) {
// add start point
locations.add(0, start);
if (segs != null) {
segs.add(0, segs.get(0));
}
if (directions != null && !directions.isEmpty()) {
for (RouteDirectionInfo i : directions) {
i.routePointOffset++;
}
RouteDirectionInfo info = new RouteDirectionInfo(directions.get(0).getAverageSpeed(), TurnType.straight());
info.routePointOffset = 0;
// info.setDescriptionRoute(ctx.getString( R.string.route_head));//; //$NON-NLS-1$
directions.add(0, info);
}
checkForDuplicatePoints(locations, directions);
}
RouteDirectionInfo lastDirInf = directions.size() > 0 ? directions.get(directions.size() - 1) : null;
if ((lastDirInf == null || lastDirInf.routePointOffset < locations.size() - 1) && locations.size() - 1 > 0) {
int type = TurnType.C;
Location prevLast = locations.get(locations.size() - 2);
float lastBearing = prevLast.bearingTo(locations.get(locations.size() - 1));
float[] compute = new float[2];
Location.distanceBetween(prevLast.getLatitude(), prevLast.getLongitude(), end.getLatitude(), end.getLongitude(), compute);
float bearingToEnd = compute[1];
double diff = MapUtils.degreesDiff(lastBearing, bearingToEnd);
if (Math.abs(diff) > 10) {
type = diff > 0 ? TurnType.KL : TurnType.KR;
}
// Wrong AvgSpeed for the last turn can cause significantly wrong total travel time if calculated route ends on a GPX route segment (then last turn is where GPX is joined again)
RouteDirectionInfo info = new RouteDirectionInfo(lastDirInf != null ? lastDirInf.getAverageSpeed() : 1, TurnType.valueOf(type, false));
info.distance = 0;
info.afterLeftTime = 0;
info.routePointOffset = locations.size() - 1;
directions.add(info);
}
}
use of net.osmand.Location in project Osmand by osmandapp.
the class RouteCalculationResult method getLeftTime.
public int getLeftTime(Location fromLoc) {
int time = 0;
if (currentDirectionInfo < directions.size()) {
RouteDirectionInfo current = directions.get(currentDirectionInfo);
time = current.afterLeftTime;
int distanceToNextTurn = listDistance[currentRoute];
if (currentDirectionInfo + 1 < directions.size()) {
distanceToNextTurn -= listDistance[directions.get(currentDirectionInfo + 1).routePointOffset];
}
Location l = locations.get(currentRoute);
if (fromLoc != null) {
distanceToNextTurn += fromLoc.distanceTo(l);
}
time += distanceToNextTurn / current.getAverageSpeed();
}
return time;
}
use of net.osmand.Location in project Osmand by osmandapp.
the class RouteProvider method findOSRMRoute.
protected RouteCalculationResult findOSRMRoute(RouteCalculationParams params) throws MalformedURLException, IOException, JSONException {
// http://router.project-osrm.org/route/v1/driving/4.83,52.28;4.95,52.28
List<Location> res = new ArrayList<Location>();
StringBuilder uri = new StringBuilder();
// possibly hide that API key because it is privacy of osmand
// A6421860EBB04234AB5EF2D049F2CD8F key is compromised
String scheme = "";
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.GINGERBREAD) {
scheme = "https";
} else {
scheme = "http";
}
// $NON-NLS-1$
uri.append(scheme + "://router.project-osrm.org/route/v1/driving/");
uri.append(String.valueOf(params.start.getLongitude()));
uri.append(",").append(String.valueOf(params.start.getLatitude()));
if (params.intermediates != null && params.intermediates.size() > 0) {
for (LatLon il : params.intermediates) {
appendOSRMLoc(uri, il);
}
}
appendOSRMLoc(uri, params.end);
// to get more waypoints, add overview=full option
// uri.append("?overview=full")
log.info("URL route " + uri);
URLConnection connection = NetworkUtils.getHttpURLConnection(uri.toString());
connection.setRequestProperty("User-Agent", Version.getFullVersion(params.ctx));
StringBuilder content = new StringBuilder();
BufferedReader rs = new BufferedReader(new InputStreamReader(connection.getInputStream()));
String s;
while ((s = rs.readLine()) != null) {
content.append(s);
}
JSONObject obj = new JSONObject(content.toString());
try {
rs.close();
} catch (IOException e) {
}
List<LatLon> route = GeoPolylineParserUtil.parse(obj.getJSONArray("routes").getJSONObject(0).getString("geometry"), GeoPolylineParserUtil.PRECISION_5);
if (route.isEmpty()) {
return new RouteCalculationResult("Route is empty");
}
for (LatLon pt : route) {
WptPt wpt = new WptPt();
wpt.lat = pt.getLatitude();
wpt.lon = pt.getLongitude();
res.add(createLocation(wpt));
}
params.intermediates = null;
return new RouteCalculationResult(res, null, params, null, true);
}
Aggregations