use of net.osmand.Location in project Osmand by osmandapp.
the class RouteLayer method onPrepareBufferImage.
@Override
public void onPrepareBufferImage(Canvas canvas, RotatedTileBox tileBox, DrawSettings settings) {
if (helper.getFinalLocation() != null && helper.getRoute().isCalculated()) {
boolean updatePaints = attrs.updatePaints(view, settings, tileBox);
attrs.isPaint3 = false;
attrs.isPaint2 = false;
if (updatePaints) {
paintIconAction.setColorFilter(new PorterDuffColorFilter(attrs.paint3.getColor(), Mode.MULTIPLY));
paintIcon.setColorFilter(new PorterDuffColorFilter(attrs.paint2.getColor(), Mode.MULTIPLY));
}
if (coloredArrowUp == null) {
Bitmap originalArrowUp = BitmapFactory.decodeResource(view.getResources(), R.drawable.map_route_direction_arrow, null);
coloredArrowUp = originalArrowUp;
// coloredArrowUp = Bitmap.createScaledBitmap(originalArrowUp, originalArrowUp.getWidth() * 3 / 4,
// originalArrowUp.getHeight() * 3 / 4, true);
}
int w = tileBox.getPixWidth();
int h = tileBox.getPixHeight();
Location lastProjection = helper.getLastProjection();
final RotatedTileBox cp;
if (lastProjection != null && tileBox.containsLatLon(lastProjection.getLatitude(), lastProjection.getLongitude())) {
cp = tileBox.copy();
cp.increasePixelDimensions(w / 2, h);
} else {
cp = tileBox;
}
final QuadRect latlonRect = cp.getLatLonBounds();
double topLatitude = latlonRect.top;
double leftLongitude = latlonRect.left;
double bottomLatitude = latlonRect.bottom;
double rightLongitude = latlonRect.right;
// double lat = 0;
// double lon = 0;
// this is buggy lat/lon should be 0 but in that case
// it needs to be fixed in case there is no route points in the view bbox
double lat = topLatitude - bottomLatitude + 0.1;
double lon = rightLongitude - leftLongitude + 0.1;
drawLocations(tileBox, canvas, topLatitude + lat, leftLongitude - lon, bottomLatitude - lat, rightLongitude + lon);
if (trackChartPoints != null) {
drawXAxisPoints(canvas, tileBox);
LatLon highlightedPoint = trackChartPoints.getHighlightedPoint();
if (highlightedPoint != null && highlightedPoint.getLatitude() >= latlonRect.bottom && highlightedPoint.getLatitude() <= latlonRect.top && highlightedPoint.getLongitude() >= latlonRect.left && highlightedPoint.getLongitude() <= latlonRect.right) {
float x = tileBox.getPixXFromLatLon(highlightedPoint.getLatitude(), highlightedPoint.getLongitude());
float y = tileBox.getPixYFromLatLon(highlightedPoint.getLatitude(), highlightedPoint.getLongitude());
canvas.drawBitmap(selectedPoint, x - selectedPoint.getWidth() / 2, y - selectedPoint.getHeight() / 2, paintIconSelected);
}
}
}
}
use of net.osmand.Location in project Osmand by osmandapp.
the class RouteLayer method calculateActionPoints.
private List<Location> calculateActionPoints(double topLatitude, double leftLongitude, double bottomLatitude, double rightLongitude, Location lastProjection, List<Location> routeNodes, int cd, Iterator<RouteDirectionInfo> it, int zoom) {
RouteDirectionInfo nf = null;
double DISTANCE_ACTION = 35;
if (zoom >= 17) {
DISTANCE_ACTION = 15;
} else if (zoom == 15) {
DISTANCE_ACTION = 70;
} else if (zoom < 15) {
DISTANCE_ACTION = 110;
}
double actionDist = 0;
Location previousAction = null;
List<Location> actionPoints = this.actionPoints;
actionPoints.clear();
int prevFinishPoint = -1;
for (int routePoint = 0; routePoint < routeNodes.size(); routePoint++) {
Location loc = routeNodes.get(routePoint);
if (nf != null) {
int pnt = nf.routeEndPointOffset == 0 ? nf.routePointOffset : nf.routeEndPointOffset;
if (pnt < routePoint + cd) {
nf = null;
}
}
while (nf == null && it.hasNext()) {
nf = it.next();
int pnt = nf.routeEndPointOffset == 0 ? nf.routePointOffset : nf.routeEndPointOffset;
if (pnt < routePoint + cd) {
nf = null;
}
}
boolean action = nf != null && (nf.routePointOffset == routePoint + cd || (nf.routePointOffset <= routePoint + cd && routePoint + cd <= nf.routeEndPointOffset));
if (!action && previousAction == null) {
// no need to check
continue;
}
boolean visible = leftLongitude <= loc.getLongitude() && loc.getLongitude() <= rightLongitude && bottomLatitude <= loc.getLatitude() && loc.getLatitude() <= topLatitude;
if (action && !visible && previousAction == null) {
continue;
}
if (!action) {
// previousAction != null
float dist = loc.distanceTo(previousAction);
actionDist += dist;
if (actionDist >= DISTANCE_ACTION) {
actionPoints.add(calculateProjection(1 - (actionDist - DISTANCE_ACTION) / dist, previousAction, loc));
actionPoints.add(null);
prevFinishPoint = routePoint;
previousAction = null;
actionDist = 0;
} else {
actionPoints.add(loc);
previousAction = loc;
}
} else {
// action point
if (previousAction == null) {
addPreviousToActionPoints(actionPoints, lastProjection, routeNodes, DISTANCE_ACTION, prevFinishPoint, routePoint, loc);
}
actionPoints.add(loc);
previousAction = loc;
prevFinishPoint = -1;
actionDist = 0;
}
}
if (previousAction != null) {
actionPoints.add(null);
}
return actionPoints;
}
use of net.osmand.Location in project Osmand by osmandapp.
the class GeocodingLookupService method geocode.
private boolean geocode(final LatLon latLon) {
Location loc = new Location("");
loc.setLatitude(latLon.getLatitude());
loc.setLongitude(latLon.getLongitude());
return app.getLocationProvider().getGeocodingResult(loc, new ResultMatcher<GeocodingResult>() {
@Override
public boolean publish(GeocodingResult object) {
String result = null;
if (object != null) {
String lang = SampleApplication.LANGUAGE;
boolean transliterate = SampleApplication.TRANSLITERATE;
String geocodingResult = "";
double relevantDistance = -1;
if (object.building != null) {
String bldName = object.building.getName(lang, transliterate);
if (!Algorithms.isEmpty(object.buildingInterpolation)) {
bldName = object.buildingInterpolation;
}
geocodingResult = object.street.getName(lang, transliterate) + " " + bldName + ", " + object.city.getName(lang, transliterate);
} else if (object.street != null) {
geocodingResult = object.street.getName(lang, transliterate) + ", " + object.city.getName(lang, transliterate);
relevantDistance = object.getDistanceP();
} else if (object.city != null) {
geocodingResult = object.city.getName(lang, transliterate);
} else if (object.point != null) {
RouteDataObject rd = object.point.getRoad();
String sname = rd.getName(lang, transliterate);
if (Algorithms.isEmpty(sname)) {
sname = "";
}
String ref = rd.getRef(lang, transliterate, true);
if (!Algorithms.isEmpty(ref)) {
if (!Algorithms.isEmpty(sname)) {
sname += ", ";
}
sname += ref;
}
geocodingResult = sname;
relevantDistance = object.getDistanceP();
}
result = geocodingResult;
if (relevantDistance == -1) {
relevantDistance = object.getDistance();
}
if (!Algorithms.isEmpty(result) && relevantDistance > 100) {
result = app.getString("shared_string_near") + " " + result;
}
}
lastFoundAddress = result;
searchDone = true;
return true;
}
@Override
public boolean isCancelled() {
return !hasAnyRequest(latLon);
}
});
}
use of net.osmand.Location in project Osmand by osmandapp.
the class MapMarkersWidgetsFactory method updateInfo.
public void updateInfo(LatLon customLocation, int zoom) {
if (customLocation != null) {
loc = customLocation;
} else {
Location l = map.getMyApplication().getLocationProvider().getLastStaleKnownLocation();
if (l != null) {
loc = new LatLon(l.getLatitude(), l.getLongitude());
} else {
loc = map.getMapLocation();
}
}
List<MapMarker> markers = helper.getMapMarkers();
if (zoom < 3 || markers.size() == 0 || !map.getMyApplication().getSettings().MARKERS_DISTANCE_INDICATION_ENABLED.get() || !map.getMyApplication().getSettings().MAP_MARKERS_MODE.get().isToolbar() || map.getMyApplication().getRoutingHelper().isFollowingMode() || map.getMyApplication().getRoutingHelper().isRoutePlanningMode() || MapRouteInfoMenu.isVisible() || addressTopBar.getVisibility() == View.VISIBLE || map.isTopToolbarActive() || !map.getContextMenu().shouldShowTopControls() || map.getMapLayers().getMapMarkersLayer().isInPlanRouteMode()) {
updateVisibility(false);
return;
}
Float heading = map.getMapViewTrackingUtilities().getHeading();
MapMarker marker = markers.get(0);
updateUI(loc, heading, marker, arrowImg, distText, okButton, addressText, true, customLocation != null);
if (markers.size() > 1 && map.getMyApplication().getSettings().DISPLAYED_MARKERS_WIDGETS_COUNT.get() == 2) {
marker = markers.get(1);
if (loc != null && customLocation == null) {
for (int i = 1; i < markers.size(); i++) {
MapMarker m = markers.get(i);
m.dist = (int) (MapUtils.getDistance(m.getLatitude(), m.getLongitude(), loc.getLatitude(), loc.getLongitude()));
if (m.dist < MIN_DIST_2ND_ROW_SHOW && marker.dist > m.dist) {
marker = m;
}
}
}
updateUI(loc, heading, marker, arrowImg2nd, distText2nd, okButton2nd, addressText2nd, false, customLocation != null);
updateVisibility(topBar2nd, true);
} else {
updateVisibility(topBar2nd, false);
}
updateVisibility(true);
}
use of net.osmand.Location in project Osmand by osmandapp.
the class ExternalApiHelper method processApiRequest.
public Intent processApiRequest(Intent intent) {
Intent result = new Intent();
OsmandApplication app = (OsmandApplication) mapActivity.getApplication();
try {
Uri uri = intent.getData();
String cmd = uri.getHost().toLowerCase();
if (API_CMD_SHOW_GPX.equals(cmd) || API_CMD_NAVIGATE_GPX.equals(cmd)) {
boolean navigate = API_CMD_NAVIGATE_GPX.equals(cmd);
String path = uri.getQueryParameter(PARAM_PATH);
boolean force = uri.getBooleanQueryParameter(PARAM_FORCE, false);
GPXFile gpx = null;
if (path != null) {
File f = new File(path);
if (f.exists()) {
gpx = GPXUtilities.loadGPXFile(mapActivity, f);
}
} else if (intent.getStringExtra(PARAM_DATA) != null) {
String gpxStr = intent.getStringExtra(PARAM_DATA);
if (!Algorithms.isEmpty(gpxStr)) {
gpx = GPXUtilities.loadGPXFile(mapActivity, new ByteArrayInputStream(gpxStr.getBytes()));
}
} else if (uri.getBooleanQueryParameter(PARAM_URI, false)) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
LOG.debug("uriString=" + intent.getClipData().getItemAt(0).getUri());
Uri gpxUri = intent.getClipData().getItemAt(0).getUri();
ParcelFileDescriptor gpxParcelDescriptor = mapActivity.getContentResolver().openFileDescriptor(gpxUri, "r");
if (gpxParcelDescriptor != null) {
FileDescriptor fileDescriptor = gpxParcelDescriptor.getFileDescriptor();
gpx = GPXUtilities.loadGPXFile(mapActivity, new FileInputStream(fileDescriptor));
} else {
finish = true;
resultCode = RESULT_CODE_ERROR_GPX_NOT_FOUND;
}
} else {
finish = true;
resultCode = RESULT_CODE_ERROR_GPX_NOT_FOUND;
}
} else {
finish = true;
resultCode = RESULT_CODE_ERROR_GPX_NOT_FOUND;
}
if (gpx != null) {
if (navigate) {
final RoutingHelper routingHelper = app.getRoutingHelper();
if (routingHelper.isFollowingMode() && !force) {
final GPXFile gpxFile = gpx;
AlertDialog dlg = mapActivity.getMapActions().stopNavigationActionConfirm();
dlg.setOnDismissListener(new DialogInterface.OnDismissListener() {
@Override
public void onDismiss(DialogInterface dialog) {
if (!routingHelper.isFollowingMode()) {
startNavigation(gpxFile, null, null, null, null, null);
}
}
});
} else {
startNavigation(gpx, null, null, null, null, null);
}
} else {
app.getSelectedGpxHelper().setGpxFileToDisplay(gpx);
}
resultCode = Activity.RESULT_OK;
} else {
finish = true;
resultCode = RESULT_CODE_ERROR_GPX_NOT_FOUND;
}
} else if (API_CMD_NAVIGATE.equals(cmd)) {
String profileStr = uri.getQueryParameter(PARAM_PROFILE);
final ApplicationMode profile = ApplicationMode.valueOfStringKey(profileStr, DEFAULT_PROFILE);
boolean validProfile = false;
for (ApplicationMode mode : VALID_PROFILES) {
if (mode == profile) {
validProfile = true;
break;
}
}
if (!validProfile) {
resultCode = RESULT_CODE_ERROR_INVALID_PROFILE;
} else {
String startName = uri.getQueryParameter(PARAM_START_NAME);
if (Algorithms.isEmpty(startName)) {
startName = "";
}
String destName = uri.getQueryParameter(PARAM_DEST_NAME);
if (Algorithms.isEmpty(destName)) {
destName = "";
}
final LatLon start;
final PointDescription startDesc;
String startLatStr = uri.getQueryParameter(PARAM_START_LAT);
String startLonStr = uri.getQueryParameter(PARAM_START_LON);
if (!Algorithms.isEmpty(startLatStr) && !Algorithms.isEmpty(startLonStr)) {
double lat = Double.parseDouble(uri.getQueryParameter(PARAM_START_LAT));
double lon = Double.parseDouble(uri.getQueryParameter(PARAM_START_LON));
start = new LatLon(lat, lon);
startDesc = new PointDescription(PointDescription.POINT_TYPE_LOCATION, startName);
} else {
start = null;
startDesc = null;
}
double destLat = Double.parseDouble(uri.getQueryParameter(PARAM_DEST_LAT));
double destLon = Double.parseDouble(uri.getQueryParameter(PARAM_DEST_LON));
final LatLon dest = new LatLon(destLat, destLon);
final PointDescription destDesc = new PointDescription(PointDescription.POINT_TYPE_LOCATION, destName);
boolean force = uri.getBooleanQueryParameter(PARAM_FORCE, false);
final RoutingHelper routingHelper = app.getRoutingHelper();
if (routingHelper.isFollowingMode() && !force) {
AlertDialog dlg = mapActivity.getMapActions().stopNavigationActionConfirm();
dlg.setOnDismissListener(new DialogInterface.OnDismissListener() {
@Override
public void onDismiss(DialogInterface dialog) {
if (!routingHelper.isFollowingMode()) {
startNavigation(null, start, startDesc, dest, destDesc, profile);
}
}
});
} else {
startNavigation(null, start, startDesc, dest, destDesc, profile);
}
}
} else if (API_CMD_RECORD_AUDIO.equals(cmd) || API_CMD_RECORD_VIDEO.equals(cmd) || API_CMD_RECORD_PHOTO.equals(cmd) || API_CMD_STOP_AV_REC.equals(cmd)) {
AudioVideoNotesPlugin plugin = OsmandPlugin.getEnabledPlugin(AudioVideoNotesPlugin.class);
if (plugin == null) {
resultCode = RESULT_CODE_ERROR_PLUGIN_INACTIVE;
finish = true;
} else {
if (API_CMD_STOP_AV_REC.equals(cmd)) {
plugin.stopRecording(mapActivity, false);
} else {
double lat = Double.parseDouble(uri.getQueryParameter(PARAM_LAT));
double lon = Double.parseDouble(uri.getQueryParameter(PARAM_LON));
if (API_CMD_RECORD_AUDIO.equals(cmd)) {
plugin.recordAudio(lat, lon, mapActivity);
} else if (API_CMD_RECORD_VIDEO.equals(cmd)) {
plugin.recordVideo(lat, lon, mapActivity, false);
} else if (API_CMD_RECORD_PHOTO.equals(cmd)) {
plugin.takePhoto(lat, lon, mapActivity, true, false);
}
}
resultCode = Activity.RESULT_OK;
}
} else if (API_CMD_GET_INFO.equals(cmd)) {
Location location = mapActivity.getMyApplication().getLocationProvider().getLastKnownLocation();
if (location != null) {
result.putExtra(PARAM_LAT, location.getLatitude());
result.putExtra(PARAM_LON, location.getLongitude());
}
final RoutingHelper routingHelper = app.getRoutingHelper();
if (routingHelper.isRouteCalculated()) {
int time = routingHelper.getLeftTime();
result.putExtra(PARAM_TIME_LEFT, time);
long eta = time + System.currentTimeMillis() / 1000;
result.putExtra(PARAM_ETA, eta);
result.putExtra(PARAM_DISTANCE_LEFT, routingHelper.getLeftDistance());
NextDirectionInfo ni = routingHelper.getNextRouteDirectionInfo(new NextDirectionInfo(), true);
if (ni.distanceTo > 0) {
updateTurnInfo("next_", result, ni);
ni = routingHelper.getNextRouteDirectionInfoAfter(ni, new NextDirectionInfo(), true);
if (ni.distanceTo > 0) {
updateTurnInfo("after_next", result, ni);
}
}
routingHelper.getNextRouteDirectionInfo(new NextDirectionInfo(), false);
if (ni.distanceTo > 0) {
updateTurnInfo("no_speak_next_", result, ni);
}
}
result.putExtra(PARAM_VERSION, VERSION_CODE);
finish = true;
resultCode = Activity.RESULT_OK;
} else if (API_CMD_ADD_FAVORITE.equals(cmd)) {
String name = uri.getQueryParameter(PARAM_NAME);
String desc = uri.getQueryParameter(PARAM_DESC);
String category = uri.getQueryParameter(PARAM_CATEGORY);
double lat = Double.parseDouble(uri.getQueryParameter(PARAM_LAT));
double lon = Double.parseDouble(uri.getQueryParameter(PARAM_LON));
String colorTag = uri.getQueryParameter(PARAM_COLOR);
boolean visible = uri.getBooleanQueryParameter(PARAM_VISIBLE, true);
if (name == null) {
name = "";
}
if (desc == null) {
desc = "";
}
if (category == null) {
category = "";
}
int color = 0;
if (!Algorithms.isEmpty(colorTag)) {
color = ColorDialogs.getColorByTag(colorTag);
if (color == 0) {
LOG.error("Wrong color tag: " + colorTag);
}
}
FavouritePoint fav = new FavouritePoint(lat, lon, name, category);
fav.setDescription(desc);
fav.setColor(color);
fav.setVisible(visible);
FavouritesDbHelper helper = app.getFavorites();
helper.addFavourite(fav);
showOnMap(lat, lon, fav, mapActivity.getMapLayers().getFavouritesLayer().getObjectName(fav));
resultCode = Activity.RESULT_OK;
} else if (API_CMD_ADD_MAP_MARKER.equals(cmd)) {
double lat = Double.parseDouble(uri.getQueryParameter(PARAM_LAT));
double lon = Double.parseDouble(uri.getQueryParameter(PARAM_LON));
String name = uri.getQueryParameter(PARAM_NAME);
PointDescription pd = new PointDescription(PointDescription.POINT_TYPE_MAP_MARKER, name != null ? name : "");
MapMarkersHelper markersHelper = app.getMapMarkersHelper();
markersHelper.addMapMarker(new LatLon(lat, lon), pd);
MapMarker marker = markersHelper.getFirstMapMarker();
if (marker != null) {
showOnMap(lat, lon, marker, mapActivity.getMapLayers().getMapMarkersLayer().getObjectName(marker));
}
resultCode = Activity.RESULT_OK;
} else if (API_CMD_START_GPX_REC.equals(cmd)) {
OsmandMonitoringPlugin plugin = OsmandPlugin.getEnabledPlugin(OsmandMonitoringPlugin.class);
if (plugin == null) {
resultCode = RESULT_CODE_ERROR_PLUGIN_INACTIVE;
finish = true;
} else {
plugin.startGPXMonitoring(null);
}
if (uri.getBooleanQueryParameter(PARAM_CLOSE_AFTER_COMMAND, true)) {
finish = true;
}
resultCode = Activity.RESULT_OK;
} else if (API_CMD_STOP_GPX_REC.equals(cmd)) {
OsmandMonitoringPlugin plugin = OsmandPlugin.getEnabledPlugin(OsmandMonitoringPlugin.class);
if (plugin == null) {
resultCode = RESULT_CODE_ERROR_PLUGIN_INACTIVE;
finish = true;
} else {
plugin.stopRecording();
}
if (uri.getBooleanQueryParameter(PARAM_CLOSE_AFTER_COMMAND, true)) {
finish = true;
}
resultCode = Activity.RESULT_OK;
} else if (API_CMD_SUBSCRIBE_VOICE_NOTIFICATIONS.equals(cmd)) {
// not implemented yet
resultCode = RESULT_CODE_ERROR_NOT_IMPLEMENTED;
}
} catch (Exception e) {
LOG.error("Error processApiRequest:", e);
resultCode = RESULT_CODE_ERROR_UNKNOWN;
}
return result;
}
Aggregations