Search in sources :

Example 1 with GeomagneticField

use of android.hardware.GeomagneticField in project Osmand by osmandapp.

the class SampleLocationProvider method calcGeoMagneticCorrection.

private float calcGeoMagneticCorrection(float val) {
    if (previousCorrectionValue == 360 && getLastKnownLocation() != null) {
        net.osmand.Location l = getLastKnownLocation();
        GeomagneticField gf = new GeomagneticField((float) l.getLatitude(), (float) l.getLongitude(), (float) l.getAltitude(), System.currentTimeMillis());
        previousCorrectionValue = gf.getDeclination();
    }
    if (previousCorrectionValue != 360) {
        val += previousCorrectionValue;
    }
    return val;
}
Also used : GeomagneticField(android.hardware.GeomagneticField)

Example 2 with GeomagneticField

use of android.hardware.GeomagneticField in project Osmand by osmandapp.

the class OsmAndLocationProvider method calcGeoMagneticCorrection.

private float calcGeoMagneticCorrection(float val) {
    net.osmand.Location l = getLastKnownLocation();
    if (previousCorrectionValue == 360 && l != null) {
        GeomagneticField gf = new GeomagneticField((float) l.getLatitude(), (float) l.getLongitude(), (float) l.getAltitude(), System.currentTimeMillis());
        previousCorrectionValue = gf.getDeclination();
    }
    if (previousCorrectionValue != 360) {
        val += previousCorrectionValue;
    }
    return val;
}
Also used : GeomagneticField(android.hardware.GeomagneticField)

Example 3 with GeomagneticField

use of android.hardware.GeomagneticField in project Osmand by osmandapp.

the class RouteInfoWidgetsFactory method createBearingControl.

public TextInfoWidget createBearingControl(final MapActivity map) {
    final int bearingResId = R.drawable.widget_bearing_day;
    final int bearingNightResId = R.drawable.widget_bearing_night;
    final int relativeBearingResId = R.drawable.widget_relative_bearing_day;
    final int relativeBearingNightResId = R.drawable.widget_relative_bearing_night;
    final OsmandApplication ctx = map.getMyApplication();
    final OsmandPreference<Boolean> showRelativeBearing = ctx.getSettings().SHOW_RELATIVE_BEARING_OTHERWISE_REGULAR_BEARING;
    final TextInfoWidget bearingControl = new TextInfoWidget(map) {

        private int cachedDegrees;

        private float MIN_SPEED_FOR_HEADING = 1f;

        public LatLon getPointToNavigate() {
            TargetPoint p = map.getPointToNavigate();
            return p == null ? null : p.point;
        }

        @Override
        public boolean updateInfo(DrawSettings drawSettings) {
            boolean relative = showRelativeBearing.get();
            boolean modeChanged = setIcons(relative ? relativeBearingResId : bearingResId, relative ? relativeBearingNightResId : bearingNightResId);
            setContentTitle(relative ? R.string.map_widget_bearing : R.string.map_widget_magnetic_bearing);
            int b = getBearing(relative);
            if (degreesChanged(cachedDegrees, b) || modeChanged) {
                cachedDegrees = b;
                if (b != -1000) {
                    setText(String.valueOf(b) + "°" + (relative ? "" : " M"), null);
                } else {
                    setText(null, null);
                }
                return true;
            }
            return false;
        }

        public int getBearing(boolean relative) {
            int d = -1000;
            Location myLocation = getOsmandApplication().getLocationProvider().getLastKnownLocation();
            LatLon l = getPointToNavigate();
            if (l == null) {
                List<MapMarker> markers = getOsmandApplication().getMapMarkersHelper().getMapMarkers();
                if (markers.size() > 0) {
                    l = markers.get(0).point;
                }
            }
            if (myLocation != null && l != null) {
                Location dest = new Location("");
                dest.setLatitude(l.getLatitude());
                dest.setLongitude(l.getLongitude());
                dest.setBearing(myLocation.bearingTo(dest));
                GeomagneticField destGf = new GeomagneticField((float) dest.getLatitude(), (float) dest.getLongitude(), (float) dest.getAltitude(), System.currentTimeMillis());
                float bearingToDest = dest.getBearing() - destGf.getDeclination();
                if (relative) {
                    float b = -1000;
                    Float heading = getOsmandApplication().getLocationProvider().getHeading();
                    if ((myLocation.getSpeed() < MIN_SPEED_FOR_HEADING || !myLocation.hasBearing()) && heading != null) {
                        b = heading;
                    } else if (myLocation.hasBearing()) {
                        GeomagneticField myLocGf = new GeomagneticField((float) myLocation.getLatitude(), (float) myLocation.getLongitude(), (float) myLocation.getAltitude(), System.currentTimeMillis());
                        b = myLocation.getBearing() - myLocGf.getDeclination();
                    }
                    if (b > -1000) {
                        bearingToDest -= b;
                        if (bearingToDest > 180f) {
                            bearingToDest -= 360f;
                        } else if (bearingToDest < -180f) {
                            bearingToDest += 360f;
                        }
                        d = (int) bearingToDest;
                    }
                } else {
                    d = (int) bearingToDest;
                }
            }
            return d;
        }
    };
    bearingControl.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            showRelativeBearing.set(!showRelativeBearing.get());
            map.refreshMap();
        }
    });
    bearingControl.setText(null, null);
    bearingControl.setIcons(!showRelativeBearing.get() ? bearingResId : relativeBearingResId, !showRelativeBearing.get() ? bearingNightResId : relativeBearingNightResId);
    return bearingControl;
}
Also used : OsmandApplication(net.osmand.plus.OsmandApplication) MapMarker(net.osmand.plus.MapMarkersHelper.MapMarker) GeomagneticField(android.hardware.GeomagneticField) TargetPoint(net.osmand.plus.TargetPointsHelper.TargetPoint) ImageView(android.widget.ImageView) View(android.view.View) TextView(android.widget.TextView) OsmandMapTileView(net.osmand.plus.views.OsmandMapTileView) TargetPoint(net.osmand.plus.TargetPointsHelper.TargetPoint) Paint(android.graphics.Paint) DrawSettings(net.osmand.plus.views.OsmandMapLayer.DrawSettings) LatLon(net.osmand.data.LatLon) Location(net.osmand.Location)

Example 4 with GeomagneticField

use of android.hardware.GeomagneticField in project satstat by mvglasow.

the class GpsSectionFragment method onLocationChanged.

/**
 * Called by {@link MainActivity} when a new location is found by the GPS location provider.
 * Stores the location and updates GPS display and map view.
 */
public void onLocationChanged(Location location) {
    if (location.hasAccuracy()) {
        Float getAcc = (float) 0.0;
        if (mainActivity.prefUnitType) {
            getAcc = (float) (location.getAccuracy());
        } else {
            getAcc = (float) (location.getAccuracy() * (float) 3.28084);
        }
        gpsAccuracy.setText(String.format("%.0f", getAcc));
        gpsAccuracyUnit.setText(getString(((mainActivity.prefUnitType) ? R.string.unit_meter : R.string.unit_feet)));
    } else {
        gpsAccuracy.setText(getString(R.string.value_none));
        gpsAccuracyUnit.setText("");
    }
    if (mainActivity.prefCoord == Const.KEY_PREF_COORD_DECIMAL) {
        gpsCoordLayout.setVisibility(View.GONE);
        gpsLatLayout.setVisibility(View.VISIBLE);
        gpsLonLayout.setVisibility(View.VISIBLE);
        gpsLat.setText(String.format("%.5f%s", location.getLatitude(), getString(R.string.unit_degree)));
        gpsLon.setText(String.format("%.5f%s", location.getLongitude(), getString(R.string.unit_degree)));
    } else if (mainActivity.prefCoord == Const.KEY_PREF_COORD_MIN) {
        gpsCoordLayout.setVisibility(View.GONE);
        gpsLatLayout.setVisibility(View.VISIBLE);
        gpsLonLayout.setVisibility(View.VISIBLE);
        double dec = location.getLatitude();
        double deg = (int) dec;
        double min = Math.abs(60.0 * (dec - deg));
        gpsLat.setText(String.format("%.0f%s %.3f'", deg, getString(R.string.unit_degree), min + /*rounding*/
        0.0005));
        dec = location.getLongitude();
        deg = (int) dec;
        min = Math.abs(60.0 * (dec - deg));
        gpsLon.setText(String.format("%.0f%s %.3f'", deg, getString(R.string.unit_degree), min + /*rounding*/
        0.0005));
    } else if (mainActivity.prefCoord == Const.KEY_PREF_COORD_SEC) {
        gpsCoordLayout.setVisibility(View.GONE);
        gpsLatLayout.setVisibility(View.VISIBLE);
        gpsLonLayout.setVisibility(View.VISIBLE);
        double dec = location.getLatitude();
        double deg = (int) dec;
        double tmp = 60.0 * (dec - deg);
        double min = (int) Math.abs(tmp);
        double sec = Math.abs(60.0 * (tmp - min));
        gpsLat.setText(String.format("%.0f%s %.0f' %.1f\"", deg, getString(R.string.unit_degree), min, sec + /*rounding*/
        0.05));
        dec = location.getLongitude();
        deg = (int) dec;
        tmp = 60.0 * (dec - deg);
        min = (int) Math.abs(tmp);
        sec = Math.abs(60.0 * (tmp - min));
        gpsLon.setText(String.format("%.0f%s %.0f' %.1f\"", deg, getString(R.string.unit_degree), min, sec + /*rounding*/
        0.05));
    } else if (mainActivity.prefCoord == Const.KEY_PREF_COORD_MGRS) {
        gpsLatLayout.setVisibility(View.GONE);
        gpsLonLayout.setVisibility(View.GONE);
        gpsCoordLayout.setVisibility(View.VISIBLE);
        gpsCoord.setText(new LatLng(location.getLatitude(), location.getLongitude()).toMGRSRef().toString(MGRSRef.PRECISION_1M));
    } else if (mainActivity.prefCoord == Const.KEY_PREF_COORD_UTM) {
        gpsLatLayout.setVisibility(View.GONE);
        gpsLonLayout.setVisibility(View.GONE);
        gpsCoordLayout.setVisibility(View.VISIBLE);
        gpsCoord.setText(UTM.lat_lon_to_utm(location.getLatitude(), location.getLongitude(), this.getContext()));
    }
    if (mainActivity.prefUtc)
        df.setTimeZone(TimeZone.getTimeZone("UTC"));
    else
        df.setTimeZone(TimeZone.getDefault());
    gpsTime.setText(df.format(new Date(location.getTime())));
    if (location.hasAltitude()) {
        Float getAltitude = (float) 0.0;
        if (mainActivity.prefUnitType) {
            getAltitude = (float) (location.getAltitude());
        } else {
            getAltitude = (float) (location.getAltitude() * (float) 3.28084);
        }
        gpsAlt.setText(String.format("%.0f", getAltitude));
        gpsAltUnit.setText(getString(((mainActivity.prefUnitType) ? R.string.unit_meter : R.string.unit_feet)));
        orDeclination.setText(String.format("%.0f%s", new GeomagneticField((float) location.getLatitude(), (float) location.getLongitude(), (float) (getAltitude), location.getTime()).getDeclination(), getString(R.string.unit_degree)));
    } else {
        gpsAlt.setText(getString(R.string.value_none));
        gpsAltUnit.setText("");
        orDeclination.setText(getString(R.string.value_none));
    }
    if (location.hasBearing()) {
        gpsBearing.setText(String.format("%.0f%s", location.getBearing(), getString(R.string.unit_degree)));
        gpsOrientation.setText(MainActivity.formatOrientation(this.getContext(), location.getBearing()));
    } else {
        gpsBearing.setText(getString(R.string.value_none));
        gpsOrientation.setText(getString(R.string.value_none));
    }
    if (location.hasSpeed()) {
        Float getSpeed = (float) 0.0;
        if (mainActivity.prefKnots) {
            getSpeed = (float) (location.getSpeed() * 1.943844f);
        } else if (mainActivity.prefUnitType) {
            getSpeed = (float) (location.getSpeed() * 3.6f);
        } else {
            getSpeed = (float) (location.getSpeed() * 2.23694f);
        }
        gpsSpeed.setText(String.format("%.0f", getSpeed));
        gpsSpeedUnit.setText(getString(((mainActivity.prefKnots) ? R.string.unit_kn : (mainActivity.prefUnitType) ? R.string.unit_km_h : R.string.unit_mph)));
    } else {
        gpsSpeed.setText(getString(R.string.value_none));
        gpsSpeedUnit.setText("");
    }
// note: getting number of sats in fix by looking for "satellites"
// in location's extras doesn't seem to work, always returns 0 sats
}
Also used : GeomagneticField(android.hardware.GeomagneticField) LatLng(uk.me.jstott.jcoord.LatLng) Date(java.util.Date)

Aggregations

GeomagneticField (android.hardware.GeomagneticField)4 Paint (android.graphics.Paint)1 View (android.view.View)1 ImageView (android.widget.ImageView)1 TextView (android.widget.TextView)1 Date (java.util.Date)1 Location (net.osmand.Location)1 LatLon (net.osmand.data.LatLon)1 MapMarker (net.osmand.plus.MapMarkersHelper.MapMarker)1 OsmandApplication (net.osmand.plus.OsmandApplication)1 TargetPoint (net.osmand.plus.TargetPointsHelper.TargetPoint)1 DrawSettings (net.osmand.plus.views.OsmandMapLayer.DrawSettings)1 OsmandMapTileView (net.osmand.plus.views.OsmandMapTileView)1 LatLng (uk.me.jstott.jcoord.LatLng)1