Java Code Examples for android.location.Location#bearingTo()

The following examples show how to use android.location.Location#bearingTo() . 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: CbScience.java    From PressureNet-SDK with MIT License 7 votes vote down vote up
public static double angle(double lat1, double long1, double lat2,
        double long2) {

	Location loc1 = new Location("network");
	loc1.setLatitude(lat1);
	loc1.setLongitude(long1);
	Location loc2 = new Location("network");
	loc2.setLatitude(lat2);
	loc2.setLongitude(long2);

    float brng = loc1.bearingTo(loc2);
    
    float readyForEnglish  = normalizeDegree(brng);
    
    log("cbscience bearingTo " + brng + ", eng " + readyForEnglish + " " + englishDirection(readyForEnglish));
    return readyForEnglish;
}
 
Example 2
Source File: KmlTrackWriter.java    From mytracks with Apache License 2.0 6 votes vote down vote up
/**
 * Gets the heading to a location.
 * 
 * @param trackId the track id containing the location
 * @param location the location
 */
private float getHeading(long trackId, Location location) {
  long trackPointId = myTracksProviderUtils.getTrackPointId(trackId, location);
  if (trackPointId == -1L) {
    return location.getBearing();
  }
  Cursor cursor = null;
  Location viewLocation;
  try {
    cursor = myTracksProviderUtils.getTrackPointCursor(trackId, trackPointId, 10, true);
    if (cursor == null || cursor.getCount() == 0) {
      return location.getBearing();
    }
    cursor.moveToPosition(cursor.getCount() - 1);
    viewLocation = myTracksProviderUtils.createTrackPoint(cursor);
  } finally {
    if (cursor != null) {
      cursor.close();
    }
  }  
  return viewLocation.bearingTo(location);
}
 
Example 3
Source File: Track.java    From GPSLogger with GNU General Public License v3.0 6 votes vote down vote up
public float getBearing() {
    if (End_Latitude != NOT_AVAILABLE) {
        if (((Start_Latitude == End_Latitude) && (Start_Longitude == End_Longitude)) || (Distance == 0))
            return NOT_AVAILABLE;
        Location EndLoc = new Location("TEMP");
        EndLoc.setLatitude(End_Latitude);
        EndLoc.setLongitude(End_Longitude);
        Location StartLoc = new Location("TEMP");
        StartLoc.setLatitude(Start_Latitude);
        StartLoc.setLongitude(Start_Longitude);
        float BTo = StartLoc.bearingTo(EndLoc);
        if (BTo < 0) BTo += 360f;
        return BTo;
    }
    return NOT_AVAILABLE;
}
 
Example 4
Source File: LiveTrackingFriend.java    From JayPS-AndroidApp with MIT License 6 votes vote down vote up
public boolean updateFromFriend(LiveTrackingFriend friend, Location lastlocation) {
    if ((id == "") || !friend.id.equals(this.id)) {
        Log.e(TAG, "updateFromFriend this " + this.toString());
        Log.e(TAG, "updateFromFriend friend "+friend.toString());
        return false;
    }
    dt = friend.ts-ts;
    //Log.d(TAG, "dt:"+ts+"->"+friend.ts+" "+dt+"s");
    ts = friend.ts;
    lat = friend.lat;
    lon = friend.lon;
    _location.setLatitude(lat);
    _location.setLongitude(lon);

    deltaDistance = lastlocation.distanceTo(_location);
    bearing = lastlocation.bearingTo(_location);
    return true;
}
 
Example 5
Source File: CompassPresenter.java    From Forage with Mozilla Public License 2.0 5 votes vote down vote up
private Pair<Float, Location> calculateAngle(Pair<Float, Location> pair) {
    float azimuth = pair.first;
    Location location = pair.second;

    azimuth += (float) LocationUtils.getMagneticDeclination(location);
    float bearing = location.bearingTo(target);

    return Pair.create(AngleUtils.normalize(azimuth - bearing), location);
}
 
Example 6
Source File: Navigator.java    From JayPS-AndroidApp with MIT License 5 votes vote down vote up
static float crossTrackError(Location p1, Location p2, Location p3) {
    // distance of a point from a great-circle path (sometimes called cross track error).
    // http://www.movable-type.co.uk/scripts/latlong.html#cross-track
    // var dXt = Math.asin(Math.sin(d13/R)*Math.sin(θ13-θ12)) * R;
    // http://stackoverflow.com/questions/1051723/distance-from-point-to-line-great-circle-function-not-working-right
    // The along-track distance, from the start point to the closest point on the path to the third point, is
    // var dAt = Math.acos(Math.cos(d13/R)/Math.cos(dXt/R)) * R;

    float d13 = p1.distanceTo(p3);
    float t13 = p1.bearingTo(p3);
    float t12 = p1.bearingTo(p2);
    float dXt = (float) (Math.asin(Math.sin(d13/R) * Math.sin((t13-t12) * DTR)) * R);
    float dAt = (float) (Math.acos(Math.cos(d13/R) / Math.cos(dXt / R)) * R);
    return dXt;
}
 
Example 7
Source File: QiblaPresenter.java    From android with Apache License 2.0 4 votes vote down vote up
private void showAzimuth(Location location) {
    if (mView == null) return;
    float azimuth = location.bearingTo(mKaabaLocation);
    azimuth = azimuth < 0 ? azimuth + 360 : azimuth;
    mView.showAzimuth(azimuth);
}
 
Example 8
Source File: MovingMarkerActivity.java    From Airbnb-Android-Google-Map-View with MIT License 4 votes vote down vote up
private float bearingBetweenLatLngs(LatLng begin, LatLng end) {
    Location beginL = convertLatLngToLocation(begin);
    Location endL = convertLatLngToLocation(end);

    return beginL.bearingTo(endL);
}
 
Example 9
Source File: GoogleMapUtis.java    From Airbnb-Android-Google-Map-View with MIT License 4 votes vote down vote up
public static float bearingBetweenLatLngs(LatLng begin,LatLng end) {
	Location beginL= convertLatLngToLocation(begin);
	Location endL= convertLatLngToLocation(end);
	return beginL.bearingTo(endL);
}
 
Example 10
Source File: Navigator.java    From JayPS-AndroidApp with MIT License 4 votes vote down vote up
public void onLocationChanged(Location location) {
    if (_nbPointsIni == 0) {
        return;
    }
    Log.d(TAG, "onLocationChanged: lat:"+location.getLatitude()+",lon:"+location.getLongitude() + " nbPointsSimpl:" + _nbPointsSimpl);
    int closestPoint = -1;

    if (_nextIndex < 0 ||  _nextIndex >= _nbPointsSimpl) {
        Log.d(TAG, "Next point not yet defined");
        closestPoint = searchClosestPoint(location, null, 0, 0, -1, -1);
        selectNewNextPoint(location, closestPoint);
    }

    if (_nextIndex >= 0) {
        float distToNextIndex = location.distanceTo(_pointsSimpl[_nextIndex]);
        if (distToNextIndex < 50) {
            Log.d(TAG, "Reach next point #" + _nextIndex + " d:" + distToNextIndex);
            if (_nextIndex == _nbPointsSimpl - 1) {
                Log.d(TAG, "Destination reached!");
                _nextDistance = 0;
                _nextIndex = closestPoint = _nbPointsSimpl; // special meaning
            } else {
                closestPoint = searchClosestPoint(location, null, 0, _nextIndex + 1, -1, -1);
                if (closestPoint >= 0) {
                    float distanceOnRoute = _pointsSimpl[closestPoint].distance - _pointsSimpl[_nextIndex].distance;
                    float distanceDirect = _pointsSimpl[closestPoint].distanceTo(_pointsSimpl[_nextIndex]);
                    Log.d(TAG, "1. #" + _nextIndex + "=>#" + closestPoint + " distanceOnRoute:" + distanceOnRoute + " distanceDirect:" + distanceDirect);
                    if (distanceOnRoute > 2 * distanceDirect && distanceOnRoute > 300) {
                        Log.d(TAG, "closestPoint too far ahead");
                        int closestPoint2 = searchClosestPoint(location, null, 0, _nextIndex + 1, -1, closestPoint - 1);
                        if (closestPoint2 >= 0) {
                            distanceOnRoute = _pointsSimpl[closestPoint2].distance - _pointsSimpl[_nextIndex].distance;
                            distanceDirect = _pointsSimpl[closestPoint2].distanceTo(_pointsSimpl[_nextIndex]);
                            Log.d(TAG, "2. #" + _nextIndex + "=>#" + closestPoint2 + " distanceOnRoute:" + distanceOnRoute + " distanceDirect:" + distanceDirect);
                            Log.d(TAG,"Replace #" + closestPoint + " by #" + closestPoint2);
                            closestPoint = closestPoint2;
                        }
                    }
                }
            }
        } else {
            // continue to look for current next point
            closestPoint = searchClosestPoint(location, _lastSeenLoc, _lastSeenDist, 0, _nextIndex, -1);
        }
    }

    if (_nextIndex != closestPoint) {
        selectNewNextPoint(location, closestPoint);
    }
    // compute distance to next point
    if (_nextIndex >= 0 && _nextIndex < _nbPointsSimpl) {
        _nextDistance = location.distanceTo(_pointsSimpl[_nextIndex]);
        _nextBearing = (location.bearingTo(_pointsSimpl[_nextIndex]) + 360) % 360;
        _error = _nextIndex > 0 ? crossTrackError(_pointsSimpl[_nextIndex-1], _pointsSimpl[_nextIndex], location) : 0;
        Log.d(TAG, _nextIndex + "[" + _pointsSimpl[_nextIndex].index + "] dist:" + _nextDistance + " bearing:" + _nextBearing + " error:" + _error);
    } else {
        _nextDistance = 0;
        Log.d(TAG, "No _nextIndex (" + _nextIndex + ")");
    }
}