Java Code Examples for android.location.Location#getAltitude()
The following examples show how to use
android.location.Location#getAltitude() .
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: LocationHelper.java From ar-location-based-android with MIT License | 7 votes |
public static float[] WSG84toECEF(Location location) { double radLat = Math.toRadians(location.getLatitude()); double radLon = Math.toRadians(location.getLongitude()); float clat = (float) Math.cos(radLat); float slat = (float) Math.sin(radLat); float clon = (float) Math.cos(radLon); float slon = (float) Math.sin(radLon); float N = (float) (WGS84_A / Math.sqrt(1.0 - WGS84_E2 * slat * slat)); float x = (float) ((N + location.getAltitude()) * clat * clon); float y = (float) ((N + location.getAltitude()) * clat * slon); float z = (float) ((N * (1.0 - WGS84_E2) + location.getAltitude()) * slat); return new float[] {x , y, z}; }
Example 2
Source File: GnssLocationProvider.java From android_9.0.0_r45 with Apache License 2.0 | 6 votes |
private void injectBestLocation(Location location) { int gnssLocationFlags = LOCATION_HAS_LAT_LONG | (location.hasAltitude() ? LOCATION_HAS_ALTITUDE : 0) | (location.hasSpeed() ? LOCATION_HAS_SPEED : 0) | (location.hasBearing() ? LOCATION_HAS_BEARING : 0) | (location.hasAccuracy() ? LOCATION_HAS_HORIZONTAL_ACCURACY : 0) | (location.hasVerticalAccuracy() ? LOCATION_HAS_VERTICAL_ACCURACY : 0) | (location.hasSpeedAccuracy() ? LOCATION_HAS_SPEED_ACCURACY : 0) | (location.hasBearingAccuracy() ? LOCATION_HAS_BEARING_ACCURACY : 0); double latitudeDegrees = location.getLatitude(); double longitudeDegrees = location.getLongitude(); double altitudeMeters = location.getAltitude(); float speedMetersPerSec = location.getSpeed(); float bearingDegrees = location.getBearing(); float horizontalAccuracyMeters = location.getAccuracy(); float verticalAccuracyMeters = location.getVerticalAccuracyMeters(); float speedAccuracyMetersPerSecond = location.getSpeedAccuracyMetersPerSecond(); float bearingAccuracyDegrees = location.getBearingAccuracyDegrees(); long timestamp = location.getTime(); native_inject_best_location(gnssLocationFlags, latitudeDegrees, longitudeDegrees, altitudeMeters, speedMetersPerSec, bearingDegrees, horizontalAccuracyMeters, verticalAccuracyMeters, speedAccuracyMetersPerSecond, bearingAccuracyDegrees, timestamp); }
Example 3
Source File: SampleHeadingCompassUp.java From osmdroid with Apache License 2.0 | 6 votes |
@Override public void onLocationChanged(Location location) { if (mMapView == null) return; gpsbearing = location.getBearing(); gpsspeed = location.getSpeed(); lat = (float) location.getLatitude(); lon = (float) location.getLongitude(); alt = (float) location.getAltitude(); //meters timeOfFix = location.getTime(); //use gps bearing instead of the compass float t = (360 - gpsbearing - this.deviceOrientation); if (t < 0) { t += 360; } if (t > 360) { t -= 360; } //help smooth everything out t = (int) t; t = t / 5; t = (int) t; t = t * 5; if (gpsspeed >= 0.01) { mMapView.setMapOrientation(t); //otherwise let the compass take over } updateDisplay(location.getBearing(), true); }
Example 4
Source File: GpsTraceService.java From MobileGuard with MIT License | 6 votes |
@Override public void onLocationChanged(Location location) { // get location info double altitude = location.getAltitude(); double longitude = location.getLongitude(); double latitude = location.getLatitude(); StringBuilder buffer = new StringBuilder(); buffer.append("altitude:" + altitude + "\n"); buffer.append("longitude:" + longitude + "\n"); buffer.append("latitude:" + latitude + "\n"); // get safe phone number String safePhone = ConfigUtils.getString(GpsTraceService.this, Constant.KEY_SAFE_PHONE, ""); if(TextUtils.isEmpty(safePhone)) { Log.e(TAG, "safe phone is empty"); return; } // send location info to safe phone number SmsManager smsManager = SmsManager.getDefault(); smsManager.sendTextMessage(safePhone, null, buffer.toString(), null, null); System.out.println("success send a sms to " + safePhone + ":\n" + buffer.toString()); // stop service stopSelf(); }
Example 5
Source File: LocationEntity.java From background_location_updates with Apache License 2.0 | 6 votes |
public static LocationEntity fromAndroidLocation(Location location) { Double vAcc = null, cAcc = null, speedAcc = null; if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { vAcc = (double) location.getVerticalAccuracyMeters(); cAcc = (double) location.getBearingAccuracyDegrees(); speedAcc = (double) location.getSpeedAccuracyMetersPerSecond(); } return new LocationEntity( (double) location.getAccuracy(), vAcc, location.getLongitude(), location.getLatitude(), location.getAltitude(), (double )location.getSpeed(), location.getTime(), 0, (double) location.getBearing(), cAcc, speedAcc, location.getProvider() ); }
Example 6
Source File: Kalman.java From DejaVu with GNU General Public License v3.0 | 5 votes |
public synchronized void update(Location location) { if (location == null) return; // Reusable final double accuracy = location.getAccuracy(); double position, noise; long timeMs = location.getTime(); predict(timeMs); mTimeOfUpdate = timeMs; samples++; // Latitude position = location.getLatitude(); noise = accuracy * BackendService.METER_TO_DEG; mLatTracker.update(position, noise); // Longitude position = location.getLongitude(); noise = accuracy * Math.cos(Math.toRadians(location.getLatitude())) * BackendService.METER_TO_DEG ; mLonTracker.update(position, noise); // Altitude if (location.hasAltitude()) { position = location.getAltitude(); noise = accuracy; if (mAltTracker == null) { mAltTracker = new Kalman1Dim(ALTITUDE_NOISE, timeMs); mAltTracker.setState(position, 0.0, noise); } else { mAltTracker.update(position, noise); } } }
Example 7
Source File: KmlTrackWriterTest.java From mytracks with Apache License 2.0 | 5 votes |
/** * Asserts that the given tag has a list of "gx:coord" subtags matching the * expected locations. * * @param tag the parent tag * @param locations list of expected locations */ private void assertTagHasPoints(Element tag, Location... locations) { List<Element> coordTags = getChildElements(tag, "gx:coord", locations.length); for (int i = 0; i < locations.length; i++) { Location location = locations[i]; String expected = location.getLongitude() + " " + location.getLatitude() + " " + location.getAltitude(); String actual = coordTags.get(i).getFirstChild().getTextContent(); assertEquals(expected, actual); } }
Example 8
Source File: KmlTrackWriterTest.java From mytracks with Apache License 2.0 | 5 votes |
/** * Asserts that the given tag is a placemark with the given properties. * * @param tag the tag * @param name the expected placemark name * @param description the expected placemark description * @param location the expected placemark location */ private void assertTagIsPlacemark( Element tag, String name, String description, Location location) { assertEquals(name, getChildTextValue(tag, "name")); if (description != null && !description.equals("")) { assertEquals(description, getChildTextValue(tag, "description")); } Element pointTag = getChildElement(tag, "Point"); String expected = location.getLongitude() + "," + location.getLatitude() + "," + location.getAltitude(); String actual = getChildTextValue(pointTag, "coordinates"); assertEquals(expected, actual); }
Example 9
Source File: SmsSenderService.java From Locate-driver with GNU General Public License v3.0 | 5 votes |
public void sendLocationMessage(String phoneNumber, Location location) { //Log.d(TAG, "sendLocationMessage()" + location.getAccuracy()); Resources r = context.getResources(); Boolean fused = isLocationFused(location); DecimalFormat latAndLongFormat = new DecimalFormat("#.######"); String text = r.getString(fused ? R.string.approximate : R.string.accurate) + " location:\n"; text += r.getString(R.string.accuracy) + " " + Math.round(location.getAccuracy()) + "m\n"; text += r.getString(R.string.latitude) + " " + latAndLongFormat.format(location.getLatitude()) + "\n"; text += r.getString(R.string.longitude) + " " + latAndLongFormat.format(location.getLongitude()) + ""; if (location.hasSpeed()) { if (speedType == 0) { text += "\n" + r.getString(R.string.speed) + " " + ((int) convertMPStoKMH(location.getSpeed())) + "KM/H"; } else { text += "\n" + r.getString(R.string.speed) + " " + ((int) convertMPStoMPH(location.getSpeed())) + "MPH"; } } if (location.hasAltitude() && location.getAltitude() != 0) { text += "\n" + r.getString(R.string.altitude) + " " + ((int) location.getAltitude()) + "m"; } SmsSenderService.this.sendSMS(phoneNumber, text); }
Example 10
Source File: LocationUtils.java From Forage with Mozilla Public License 2.0 | 5 votes |
/** * 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 11
Source File: LocationSensor.java From appinventor-extensions with Apache License 2.0 | 5 votes |
@Override // This sets fields longitude, latitude, altitude, hasLocationData, and // hasAltitude, then calls LocationSensor.LocationChanged(), all in the // enclosing class LocationSensor. public void onLocationChanged(final Location location) { lastLocation = location; longitude = location.getLongitude(); latitude = location.getLatitude(); speed = location.getSpeed(); // If the current location doesn't have altitude information, the prior // altitude reading is retained. if (location.hasAltitude()) { hasAltitude = true; altitude = location.getAltitude(); } // By default Location.latitude == Location.longitude == 0. // So we want to ignore that case rather than generating a changed event. if (longitude != UNKNOWN_VALUE || latitude != UNKNOWN_VALUE) { hasLocationData = true; final double argLatitude = latitude; final double argLongitude = longitude; final double argAltitude = altitude; final float argSpeed = speed; androidUIHandler.post(new Runnable() { @Override public void run() { LocationChanged(argLatitude, argLongitude, argAltitude, argSpeed); for (LocationSensorListener listener : listeners) { listener.onLocationChanged(location); } } }); } }
Example 12
Source File: Kalman.java From DejaVu with GNU General Public License v3.0 | 5 votes |
/** * * @param location */ public Kalman(Location location, double coordinateNoise) { final double accuracy = location.getAccuracy(); final double coordinateNoiseDegrees = coordinateNoise * BackendService.METER_TO_DEG; double position, noise; long timeMs = location.getTime(); // Latitude position = location.getLatitude(); noise = accuracy * BackendService.METER_TO_DEG; mLatTracker = new Kalman1Dim(coordinateNoiseDegrees, timeMs); mLatTracker.setState(position, 0.0, noise); // Longitude position = location.getLongitude(); noise = accuracy * Math.cos(Math.toRadians(location.getLatitude())) * BackendService.METER_TO_DEG; mLonTracker = new Kalman1Dim(coordinateNoiseDegrees, timeMs); mLonTracker.setState(position, 0.0, noise); // Altitude if (location.hasAltitude()) { position = location.getAltitude(); noise = accuracy; mAltTracker = new Kalman1Dim(ALTITUDE_NOISE, timeMs); mAltTracker.setState(position, 0.0, noise); } mTimeOfUpdate = timeMs; samples = 1; }
Example 13
Source File: BearingToNorthProvider.java From bearing-example with MIT License | 5 votes |
private GeomagneticField getGeomagneticField(Location location) { GeomagneticField geomagneticField = new GeomagneticField( (float)location.getLatitude(), (float)location.getLongitude(), (float)location.getAltitude(), System.currentTimeMillis()); return geomagneticField; }
Example 14
Source File: ControllerFragment.java From Bluefruit_LE_Connect_Android_V2 with MIT License | 5 votes |
private void setLastLocation(@Nullable Location location) { if (location != null) { SensorData sensorData = mSensorData[kSensorType_Location]; float[] values = new float[3]; values[0] = (float) location.getLatitude(); values[1] = (float) location.getLongitude(); values[2] = (float) location.getAltitude(); sensorData.values = values; } mControllerAdapter.notifySensorChanged(kSensorType_Location); }
Example 15
Source File: SensorNotificationService.java From android_9.0.0_r45 with Apache License 2.0 | 4 votes |
@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 16
Source File: GPSTesterActivityController.java From android-gps-test-tool with Apache License 2.0 | 4 votes |
private static void writeResultToTable( Location location, LocationManager locationManager, String elapsedTimeGPSProvider, Boolean isNetworkAvailable){ _gpsLatitude = location.getLatitude(); _gpsLongitude = location.getLongitude(); _gpsAccuracy = location.getAccuracy(); final double speed = location.getSpeed(); final double altitude = location.getAltitude(); String bestAvailableText = ""; boolean networkProviderEnabled = false; boolean gpsProviderEnabled = false; try{ networkProviderEnabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER); gpsProviderEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER); } catch(Exception exc){ Log.d("GPSTester","WriteResultToTable(): " + exc.getMessage()); } if(_networkAccuracy > _gpsAccuracy && _gpsAccuracy > 0.0 && gpsProviderEnabled == true){ _bestAvailableType = BestAvailableType.GPS; bestAvailableText = "<b><font color='yellow'>Best Accuracy</font> = <font color='red'>GPS</b></font>" + "<br><b>GPS Accuracy:</b> " + DECIMAL_FORMAT_4.format(_gpsAccuracy) + " meters" + "<br><b>GPS Lat/Lon:</b> " + _gpsLatitude + ", " + _gpsLongitude; } else if(_gpsAccuracy > _networkAccuracy && _networkAccuracy > 0.0 && networkProviderEnabled == true){ _bestAvailableType = BestAvailableType.NETWORK; bestAvailableText = "<b><font color='yellow'><b><font color='yellow'>Best Accuracy</font> = <font color='red'>Network</b></font></b></font>" + "<br><b>Network Accuracy:<b/> " + DECIMAL_FORMAT_4.format(_networkAccuracy) + " meters" + "<br><b>Network Lat/Lon:<b/> " + _networkLatitude + ", " + _networkLongitude; } else if(_gpsAccuracy == 0.0 && _networkAccuracy > 0.0 && networkProviderEnabled == true){ _bestAvailableType = BestAvailableType.NETWORK; bestAvailableText = "<b><font color='yellow'><b><font color='yellow'>Best Accuracy</font> = <font color='red'>Network</b></font></b></font>" + "<br><b>Network Accuracy:<b/> " + DECIMAL_FORMAT_4.format(_networkAccuracy) + " meters" + "<br><b>Network Lat/Lon:<b/> " + _networkLatitude + ", " + _networkLongitude; } else if(_networkAccuracy == 0.0 && _gpsAccuracy > 0.0 && gpsProviderEnabled == true){ _bestAvailableType = BestAvailableType.GPS; bestAvailableText = "<b><font color='yellow'>Best Accuracy</font> = <font color='red'>GPS</b></font>" + "<br><b>GPS Accuracy:</b> " + DECIMAL_FORMAT_4.format(_gpsAccuracy) + " meters" + "<br><b>GPS Lat/Lon:</b> " + _gpsLatitude + ", " + _gpsLongitude; } else{ _bestAvailableType = BestAvailableType.NULL; bestAvailableText = "<b><font color='yellow'>Best Accuracy = N/A</b></font>" + "<br><b>Lat/Lon:</b> N/A" + "<br><b>Accuracy:</b> N/A"; } setBestAvailableImageView(_bestAvailableType); String elapsedTimeSinceLastGPS = _elapsedTimer.calculateTimeDifference(_initialGPSTime, _elapsedTimer.getElapsedtime()); _initialGPSTime = _elapsedTimer.getElapsedtime(); final String gpsLocationText = "<b><font color='yellow'>GPS Provider</b></font>" + "<br><b>Timestamp:</b> " + _elapsedTimer.convertMillisToMDYHMSS(location.getTime()) + "<br><b>1st update elapsed time:</b> " + elapsedTimeGPSProvider + "<br><b>Since last update:</b> " + elapsedTimeSinceLastGPS + "<br><b>Lat/Lon:</b> " + _gpsLatitude + ", " + _gpsLongitude + "<br><b>DMSS:</b> " + Location.convert(_gpsLatitude, Location.FORMAT_SECONDS) + ", " + Location.convert(_gpsLongitude, Location.FORMAT_SECONDS) + "<br><b>Accuracy:</b> " + DECIMAL_FORMAT_4.format(_gpsAccuracy) + " meters" + "<br><b>Speed:</b> " + DECIMAL_FORMAT.format((speed * 2.2369)) + " mph" + ", " + DECIMAL_FORMAT.format(((speed * 3600)/1000)) + " km/h" + "<br><b>Altitude:</b> " + DECIMAL_FORMAT.format(altitude) + " m, " + DECIMAL_FORMAT.format(altitude * 3.2808) + " ft" + "<br><b>Bearing:</b> " + location.getBearing() + " deg"; _gpsLocationTextView.setText(Html.fromHtml(gpsLocationText)); _bestAvailableInfoTextView.setText(Html.fromHtml(bestAvailableText)); //If true then we can draw on the map. Offline maps not available in this version if(isNetworkAvailable == true){ final int redMapGraphicSize = Integer.valueOf(_preferences.getString("pref_key_gpsGraphicSize", "10")); // Called when a new location is found by the network location provider. if(_preferences.getBoolean("pref_key_centerOnGPSCoords", true) == true){ _map.centerAt(_gpsLatitude, _gpsLongitude, true); } if(_preferences.getBoolean("pref_key_accumulateMapPoints", true) == false){ // _map.clearPointsGraphicLayer(); _graphicsLayer.removeAll(); } addGraphicLatLon(_gpsLatitude, _gpsLongitude, null, SimpleMarkerSymbol.STYLE.CIRCLE,Color.RED,redMapGraphicSize,_graphicsLayer,_map); } }
Example 17
Source File: StatsUtils.java From mytracks with Apache License 2.0 | 4 votes |
/** * Sets the location values. * * @param context the context * @param activity the activity for finding views. If null, the view cannot be * null * @param view the containing view for finding views. If null, the activity * cannot be null * @param location the location * @param isRecording true if recording */ public static void setLocationValues( Context context, Activity activity, View view, Location location, boolean isRecording) { boolean metricUnits = PreferencesUtils.isMetricUnits(context); boolean reportSpeed = PreferencesUtils.isReportSpeed(context); // Set speed/pace View speed = getView(activity, view, R.id.stats_speed); speed.setVisibility(isRecording ? View.VISIBLE : View.INVISIBLE); if (isRecording) { double value = isRecording && location != null && location.hasSpeed() ? location.getSpeed() : Double.NaN; setSpeed(context, speed, R.string.stats_speed, R.string.stats_pace, value, metricUnits, reportSpeed); } // Set elevation boolean showGradeElevation = PreferencesUtils.getBoolean( context, R.string.stats_show_grade_elevation_key, PreferencesUtils.STATS_SHOW_GRADE_ELEVATION_DEFAULT) && isRecording; View elevation = getView(activity, view, R.id.stats_elevation); elevation.setVisibility(showGradeElevation ? View.VISIBLE : View.GONE); if (showGradeElevation) { double altitude = location != null && location.hasAltitude() ? location.getAltitude() : Double.NaN; setElevationValue(context, elevation, -1, altitude, metricUnits); } // Set coordinate boolean showCoordinate = PreferencesUtils.getBoolean( context, R.string.stats_show_coordinate_key, PreferencesUtils.STATS_SHOW_COORDINATE_DEFAULT) && isRecording; View coordinateSeparator = getView(activity, view, R.id.stats_coordinate_separator); View coordinateContainer = getView(activity, view, R.id.stats_coordinate_container); if (coordinateSeparator != null) { coordinateSeparator.setVisibility(showCoordinate ? View.VISIBLE : View.GONE); } coordinateContainer.setVisibility(showCoordinate ? View.VISIBLE : View.GONE); if (showCoordinate) { double latitude = location != null ? location.getLatitude() : Double.NaN; double longitude = location != null ? location.getLongitude() : Double.NaN; setCoordinateValue( context, getView(activity, view, R.id.stats_latitude), R.string.stats_latitude, latitude); setCoordinateValue(context, getView(activity, view, R.id.stats_longitude), R.string.stats_longitude, longitude); } }
Example 18
Source File: Position.java From OpenMapKitAndroid with BSD 3-Clause "New" or "Revised" License | 4 votes |
public Position(Location location) { this.mStorage[LAT_IDX] = location.getLatitude(); this.mStorage[LON_IDX] = location.getLongitude(); this.mStorage[ALT_IDX] = location.getAltitude(); }
Example 19
Source File: LocationsMatcher.java From mytracks with Apache License 2.0 | 4 votes |
private boolean matchLocation(Location location1, Location location2) { return (location1.getTime() == location2.getTime()) && (location1.getLatitude() == location2.getLatitude()) && (location1.getLongitude() == location2.getLongitude()) && (location1.getAltitude() == location2.getAltitude()); }
Example 20
Source File: LatLng.java From OpenMapKitAndroid with BSD 3-Clause "New" or "Revised" License | 2 votes |
/** * Transform a Location into a LatLng point * @param aLocation */ public LatLng(final Location aLocation) { this(aLocation.getLatitude(), aLocation.getLongitude(), aLocation.getAltitude()); }