android.hardware.GeomagneticField Java Examples

The following examples show how to use android.hardware.GeomagneticField. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: Sensors.java    From Multiwii-Remote with Apache License 2.0 6 votes vote down vote up
@Override
public void onLocationChanged(Location location) {

	oldLocation = this.location;
	this.location = location;

	PhoneLatitude = location.getLatitude();
	PhoneLongitude = location.getLongitude();
	PhoneAltitude = location.getAltitude();
	PhoneSpeed = location.getSpeed() * 100f;
	PhoneAccuracy = location.getAccuracy() * 100f;

	//MapCurrentPosition = new LatLng(location.getLatitude(), location.getLongitude());

	geoField = new GeomagneticField(Double.valueOf(location.getLatitude()).floatValue(), Double.valueOf(location.getLongitude()).floatValue(), Double.valueOf(location.getAltitude()).floatValue(), System.currentTimeMillis());
	Declination = geoField.getDeclination();

	if (mGPSListener != null)
		mGPSListener.onSensorsStateGPSLocationChange();
}
 
Example #2
Source File: LocationUtils.java    From Forage with Mozilla Public License 2.0 5 votes vote down vote up
/**
 * Gets the magnetic declination at the specified location.
 *
 * @param location Current location.
 * @return The declination of the horizontal component of the magnetic
 * field from true north, in degrees (i.e. positive means the
 * magnetic field is rotated east that much from true north).
 */
public static double getMagneticDeclination(@NonNull Location location) {
    GeomagneticField geoField = new GeomagneticField(
            (float) location.getLatitude(),
            (float) location.getLongitude(),
            (float) location.getAltitude(),
            System.currentTimeMillis()
    );

    return geoField.getDeclination();
}
 
Example #3
Source File: LocationInformation.java    From trekarta with GNU General Public License v3.0 5 votes vote down vote up
private void updateLocation(double latitude, double longitude, int zoom) {
    mLatitude = latitude;
    mLongitude = longitude;
    mZoom = zoom;

    mCoordinateDegree.setText(StringFormatter.coordinates(0, " ", latitude, longitude));
    mCoordinateDegMin.setText(StringFormatter.coordinates(1, " ", latitude, longitude));
    mCoordinateDegMinSec.setText(StringFormatter.coordinates(2, " ", latitude, longitude));
    mCoordinateUtmUps.setText(StringFormatter.coordinates(3, " ", latitude, longitude));
    mCoordinateMgrs.setText(StringFormatter.coordinates(4, " ", latitude, longitude));

    if (BuildConfig.FULL_VERSION) {
        mSunriseSunset.setLocation(latitude, longitude);
        double sunrise = mSunriseSunset.compute(true);
        double sunset = mSunriseSunset.compute(false);

        if (sunrise == Double.MAX_VALUE || sunset == Double.MAX_VALUE) {
            mSunrise.setText(R.string.never_rises);
            mSunsetTitle.setVisibility(View.GONE);
            mSunset.setVisibility(View.GONE);
        } else if (sunrise == Double.MIN_VALUE || sunset == Double.MIN_VALUE) {
            mSunset.setText(R.string.never_sets);
            mSunriseTitle.setVisibility(View.GONE);
            mSunrise.setVisibility(View.GONE);
        } else {
            mSunrise.setText(mSunriseSunset.formatTime(sunrise));
            mSunset.setText(mSunriseSunset.formatTime(sunset));
            mSunriseTitle.setVisibility(View.VISIBLE);
            mSunrise.setVisibility(View.VISIBLE);
            mSunsetTitle.setVisibility(View.VISIBLE);
            mSunset.setVisibility(View.VISIBLE);
        }

        mOffset.setText(StringFormatter.timeO((int) (mSunriseSunset.getUtcOffset() * 60)));

        GeomagneticField mag = new GeomagneticField((float) latitude, (float) longitude, 0.0f, System.currentTimeMillis());
        mDeclination.setText(String.format(Locale.getDefault(), "%+.1f\u00B0", mag.getDeclination()));
    }
}
 
Example #4
Source File: CompassFragment.java    From android_maplibui with GNU Lesser General Public License v3.0 5 votes vote down vote up
public static float getDeclination(Location location, long timestamp) {
    if (location == null)
        return 0;

    GeomagneticField field = new GeomagneticField((float) location.getLatitude(), (float) location.getLongitude(),
            (float) location.getAltitude(), timestamp);

    return field.getDeclination();
}
 
Example #5
Source File: BearingToNorthProvider.java    From bearing-example with MIT License 5 votes vote down vote up
private GeomagneticField getGeomagneticField(Location location)
{
    GeomagneticField geomagneticField = new GeomagneticField(
            (float)location.getLatitude(),
            (float)location.getLongitude(),
            (float)location.getAltitude(),
            System.currentTimeMillis());
    return geomagneticField;
}
 
Example #6
Source File: SampleHeadingCompassUp.java    From osmdroid with Apache License 2.0 5 votes vote down vote up
@Override
public void onOrientationChanged(final float orientationToMagneticNorth, IOrientationProvider source) {
    //note, on devices without a compass this never fires...

    //only use the compass bit if we aren't moving, since gps is more accurate when we are moving
    if (gpsspeed < 0.01) {
        GeomagneticField gf = new GeomagneticField(lat, lon, alt, timeOfFix);
        trueNorth = orientationToMagneticNorth + gf.getDeclination();
        gf = null;
        synchronized (trueNorth) {
            if (trueNorth > 360.0f) {
                trueNorth = trueNorth - 360.0f;
            }
            float actualHeading = 0f;

            //this part adjusts the desired map rotation based on device orientation and compass heading
            float t = (360 - trueNorth - this.deviceOrientation);
            if (t < 0) {
                t += 360;
            }
            if (t > 360) {
                t -= 360;
            }
            actualHeading = t;
            //help smooth everything out
            t = (int) t;
            t = t / 5;
            t = (int) t;
            t = t * 5;
            mMapView.setMapOrientation(t);
            updateDisplay(actualHeading,false);
        }
    }
}
 
Example #7
Source File: Androzic.java    From Androzic with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void onLocationChanged(final Location location, final boolean continous, final boolean geoid, final float smoothspeed, final float avgspeed)
{
	Log.d(TAG, "Location arrived");

	final long lastLocationMillis = location.getTime();

	if (angleMagnetic && lastLocationMillis - lastMagnetic >= magInterval)
	{
		GeomagneticField mag = new GeomagneticField((float) location.getLatitude(), (float) location.getLongitude(), (float) location.getAltitude(), System.currentTimeMillis());
		magneticDeclination = mag.getDeclination();
		lastMagnetic = lastLocationMillis;
	}

	Androzic.this.location[0] = location.getLatitude();
	Androzic.this.location[1] = location.getLongitude();

	shouldEnableFollowing = shouldEnableFollowing || lastKnownLocation == null;

	lastKnownLocation = location;
	gpsEnabled = gpsEnabled || LocationManager.GPS_PROVIDER.equals(location.getProvider());
	gpsContinous = continous;
	gpsGeoid = geoid;

	if (overlayManager.accuracyOverlay != null && location.hasAccuracy())
	{
		overlayManager.accuracyOverlay.setAccuracy(location.getAccuracy());
	}
}
 
Example #8
Source File: SensorNotificationService.java    From android_9.0.0_r45 with Apache License 2.0 4 votes vote down vote up
@Override
public void onLocationChanged(Location location) {
    if (DBG) Slog.d(TAG, String.format(
            "Location is (%f, %f), h %f, acc %f, mocked %b",
            location.getLatitude(), location.getLongitude(),
            location.getAltitude(), location.getAccuracy(),
            location.isFromMockProvider()));

    // lat long == 0 usually means invalid location
    if (location.getLatitude() == 0 && location.getLongitude() == 0) {
        return;
    }

    // update too often, ignore
    if (SystemClock.elapsedRealtime() - mLocalGeomagneticFieldUpdateTime < 10 * MINUTE_IN_MS) {
        return;
    }

    long time = System.currentTimeMillis();
    // Mocked location should not be used. Except in test, only use mocked location
    // Wrong system clock also gives bad values so ignore as well.
    if (useMockedLocation() == location.isFromMockProvider() || time < MILLIS_2010_1_1) {
        return;
    }

    GeomagneticField field = new GeomagneticField(
            (float) location.getLatitude(), (float) location.getLongitude(),
            (float) location.getAltitude(), time);
    if (DBG) Slog.d(TAG, String.format(
            "Nominal mag field, norm %fuT, decline %f deg, incline %f deg",
            field.getFieldStrength() / 1000, field.getDeclination(), field.getInclination()));

    try {
        SensorAdditionalInfo info = SensorAdditionalInfo.createLocalGeomagneticField(
                    field.getFieldStrength() / 1000, // convert from nT to uT
                    (float)(field.getDeclination() * Math.PI / 180), // from degree to rad
                    (float)(field.getInclination() * Math.PI / 180)); // from degree to rad
        if (info != null) {
            mSensorManager.setOperationParameter(info);
            mLocalGeomagneticFieldUpdateTime = SystemClock.elapsedRealtime();
        }
    } catch (IllegalArgumentException e) {
        Slog.e(TAG, "Invalid local geomagnetic field, ignore.");
    }
}
 
Example #9
Source File: OrientationManager.java    From SpeedHud with Apache License 2.0 4 votes vote down vote up
/**
 * Updates the cached instance of the geomagnetic field after a location change.
 */
private void updateGeomagneticField() {
    mGeomagneticField = new GeomagneticField((float) mLocation.getLatitude(),
            (float) mLocation.getLongitude(), (float) mLocation.getAltitude(),
            mLocation.getTime());
}
 
Example #10
Source File: OrientationManager.java    From PTVGlass with MIT License 4 votes vote down vote up
/**
 * Updates the cached instance of the geomagnetic field after a location change.
 */
private void updateGeomagneticField() {
    mGeomagneticField = new GeomagneticField((float) mLocation.getLatitude(),
            (float) mLocation.getLongitude(), (float) mLocation.getAltitude(),
            mLocation.getTime());
}
 
Example #11
Source File: OrientationManager.java    From PTVGlass with MIT License 4 votes vote down vote up
/**
 * Updates the cached instance of the geomagnetic field after a location change.
 */
private void updateGeomagneticField() {
    mGeomagneticField = new GeomagneticField((float) mLocation.getLatitude(),
            (float) mLocation.getLongitude(), (float) mLocation.getAltitude(),
            mLocation.getTime());
}
 
Example #12
Source File: WaypointProject.java    From Androzic with GNU General Public License v3.0 4 votes vote down vote up
public void onClick(View v)
    {
    	try
    	{
    		Androzic application = Androzic.getApplication();
    		Waypoint waypoint = new Waypoint();
    		View view = getView();
    		waypoint.name = ((TextView) view.findViewById(R.id.name_text)).getText().toString();
    		double distance = Integer.parseInt(((TextView) view.findViewById(R.id.distance_text)).getText().toString());
    		double bearing = Double.parseDouble(((TextView) view.findViewById(R.id.bearing_text)).getText().toString());
    		int src = ((Spinner) view.findViewById(R.id.source_spinner)).getSelectedItemPosition();
    		int df = ((Spinner) view.findViewById(R.id.distance_spinner)).getSelectedItemPosition();
    		int bf = ((Spinner) view.findViewById(R.id.bearing_spinner)).getSelectedItemPosition();
    		double[] loc;
    		if (src > 0)
    		{
    			 loc = new double[2];
    			 loc[0] = waypoints.get(src-1).latitude;
    			 loc[1] = waypoints.get(src-1).longitude;
    		}
    		else
    		{
				loc = application.getLocation();
    		}

    		if (df == 0)
    		{
    			distance = distance / StringFormatter.distanceFactor * 1000;
    		}
    		else
    		{
    			distance = distance / StringFormatter.distanceShortFactor;
    		}
      bearing = bearing * StringFormatter.angleFactor;
    		if (bf == 1)
    		{
    			GeomagneticField mag = new GeomagneticField((float) loc[0], (float) loc[1], 0.0f, System.currentTimeMillis());
    			bearing += mag.getDeclination();
       if (bearing > 360d)
        bearing -= 360d;
    		}
    		double[] prj = Geo.projection(loc[0], loc[1], distance, bearing);
    		waypoint.latitude = prj[0];
    		waypoint.longitude = prj[1];
    		waypoint.date = Calendar.getInstance().getTime();
    		application.addWaypoint(waypoint);

    		getTargetFragment().onActivityResult(getTargetRequestCode(), Activity.RESULT_OK, null);
dismiss();
    	}
    	catch (Exception e)
    	{
			Toast.makeText(getActivity(), "Invalid input", Toast.LENGTH_LONG).show();
			e.printStackTrace();
    	}
    }
 
Example #13
Source File: Androzic.java    From Androzic with GNU General Public License v3.0 4 votes vote down vote up
public double getDeclination(double lat, double lon)
{
	GeomagneticField mag = new GeomagneticField((float) lat, (float) lon, 0.0f, System.currentTimeMillis());
	return mag.getDeclination();
}