Example usage for android.location Location bearingTo

List of usage examples for android.location Location bearingTo

Introduction

In this page you can find the example usage for android.location Location bearingTo.

Prototype

public float bearingTo(Location dest) 

Source Link

Document

Returns the approximate initial bearing in degrees East of true North when traveling along the shortest path between this location and the given location.

Usage

From source file:com.keithcassidy.finishline.LineCrossHandler.java

private void HandleLineCrossing(Location location) {
    //does this location and bearing intersect with finish line
    DistanceIntersection diToFinish = LocationUtils.distanceToFinish(location, buoy1, buoy2,
            finishLineExtension);//from w w w  .j av  a 2  s  .  c o  m
    sendLocalBroadcast(Constants.FINISHLINE_DISTANCE_MESSAGE, diToFinish.distance);

    if (isRacing) {
        //sound appropriate to distance
        startBeepTimer(PlaySounds.getPeriodFromDistance((int) diToFinish.distance));

        //update latest time (used for deciding if service should restart after a crash/reboot
        PreferencesUtils.setLastRaceStopTime(context, location.getTime());

        if (lastLocation != null && lastLocation.distanceTo(location) > LocationUtils.EPSILON) {
            Log.d(TAG, "last location time " + lastLocation.getTime());

            Location locationLookingBack = new Location(location);
            locationLookingBack.setBearing(locationLookingBack.bearingTo(lastLocation));

            DistanceIntersection diBackToFinish = LocationUtils.distanceToFinish(locationLookingBack, buoy1,
                    buoy2, finishLineExtension);
            if (!Double.isInfinite(diBackToFinish.distance) && diBackToFinish.distance >= 0
                    && diBackToFinish.intersection != null && (location
                            .distanceTo(diBackToFinish.intersection) <= location.distanceTo(lastLocation))) {
                long timeOfCrossing = lastLocation.getTime()
                        + (long) ((location.getTime() - lastLocation.getTime())
                                * (lastLocation.distanceTo(diBackToFinish.intersection)
                                        / location.distanceTo(lastLocation)));

                diBackToFinish.intersection.setTime(timeOfCrossing);
                diBackToFinish.intersection.setBearing(lastLocation.bearingTo(location));
                finishLineDataStorage.addCrossing(diBackToFinish.intersection);

                sendLocalBroadcast(Constants.FINISHLINE_CROSSED_MESSAGE, true);

                PlaySounds.playLineCross(context);
            }

        }

        lastLocation = location;
    }
    //lastDistanceToFinish = diToFinish.distance;

    /*   
    if (lastLocation != null && isRacing) 
    {
            
       //test to see if we crossed the line
       if( !Double.isInfinite(lastDistanceToFinish) && lastDistanceToFinish > 0 && diToFinish.distance <= 0 )
       {
    long timeOfCrossing = lastLocation.getTime() + (long)((location.getTime() - lastLocation.getTime()) * 
          (lastDistanceToFinish / (Math.abs(diToFinish.distance) + lastDistanceToFinish)) ) ;
            
    Location buoyStart = new Location("na");
    buoyStart.setLatitude(buoy1.Position.latitude);
    buoyStart.setLongitude(buoy1.Position.longitude);
            
    Location buoyEnd = new Location("na");
    buoyEnd.setLatitude(buoy2.Position.latitude);
    buoyEnd.setLongitude(buoy2.Position.longitude);
    buoyEnd.setBearing(buoyEnd.bearingTo(buoyStart));
            
    buoyStart.setBearing(buoyStart.bearingTo(buoyEnd));
            
    //Location locationCrossing = LocationUtils.intersectionOfTwoPaths(lastLocation, buoyStart);
    if( diToFinish.intersection != null )
    {
       diToFinish.intersection.setTime(timeOfCrossing);
       diToFinish.intersection.setBearing(lastLocation.getBearing());
       finishLineDataStorage.addCrossing(diToFinish.intersection);
            
       sendLocalBroadcast(Constants.FINISHLINE_CROSSED_MESSAGE, true);
            
       PlaySounds.playLineCross(context);
    }
       }
            
    }
     */
}

From source file:dtu.ds.warnme.app.location.FollowMeLocationSource.java

private void checkEventsProximity(Location location) {
    if (cachedEvents == null || cachedEvents.isEmpty()) {
        return;/*from w  ww . j  a  v a  2 s. c  o m*/
    }

    Float closestDistance = Float.MAX_VALUE;
    Event closestEvent = null;

    for (Event event : cachedEvents) {
        Float distance = location.distanceTo(event.getLocation());
        Float bearing = location.bearingTo(event.getLocation());

        if (distance < closestDistance && bearing < 90f) {
            closestDistance = distance;
            closestEvent = event;
        }
    }

    if (closestEvent != null && closestDistance < 500f) {
        locationSourceListener.onApproachingEvent(closestEvent);
    }
}

From source file:com.tagaugmentedreality.NearByTagsList.java

public void prepareTagsList(String latitude, String longitude) {
    tagListView = (ListView) view.findViewById(R.id.tagListView);
    // tagListView.setVisibility(View.GONE);

    /*//from  www.ja  v  a2s .c om
     * I am building the tags list
     */

    for (int i = 0; i < TagListCache.getTagsList().size(); i++) {
        TagListCache.getTagsList().get(i).destroyTag();
        TagListCache.getTagsList().remove(i);
    } // end for

    TagListCache.getTagsList().clear();

    TagCache tag;
    OpenGLModelCache openGLModel;

    Location tagLocation = new Location(" ");
    Location deviceLocation = new Location(" ");
    deviceLocation.setLatitude(Utilities.LATITUDE);
    deviceLocation.setLongitude(Utilities.LONGITUDE);
    Float bearing = 0.0f;
    Float distance = 0.0f;
    BigDecimal bearingRounded;
    /*
     * TagCache(String id, OpenGLModelCache openGLModel, String title,
     * String comment, Double latitude, Double longitude, String
     * tagImageLink, String accuracy, String distance, Float direction,
     * String firstName, String lastName, String userFacebookId, String
     * city, String province, String country, boolean grouped )
     */

    // tag1

    tag = new TagCache("1", null, "Tag1", "Tag1 is here.", Double.parseDouble(latitude),
            Double.parseDouble(longitude), "", "10", "10", 0.0f, "Lahore", "Punjab", "Pakistan", false);

    openGLModel = new OpenGLModelCache();
    openGLModel.resetVector();

    /**
     * I am getting the bearing of the tag with the device right here, by
     * using the latitude and longitude of both the tag and the device. Then
     * i am saving the value as the tag's heading.
     */
    tagLocation.setLatitude(Double.parseDouble("31.459379"));
    tagLocation.setLongitude(Double.parseDouble("74.368984"));
    distance = deviceLocation.distanceTo(tagLocation);
    bearing = deviceLocation.bearingTo(tagLocation);

    if (bearing < 0) {
        bearing = (-1) * bearing;
        bearing = 180 + (180 - bearing);
    } // end if

    bearingRounded = new BigDecimal(bearing).setScale(0, BigDecimal.ROUND_HALF_UP);
    bearing = Float.parseFloat(bearingRounded.toString());

    Log.e(Utilities.TAG, "bearing: " + bearing.toString());

    openGLModel.setHeading(bearing);// dataObject.getString("direction")));
    tag.setDirection(bearing);
    tag.setOpenGLModel(openGLModel);
    tag.setDistance(distance.toString());
    TagListCache.getTagsList().add(tag);

    // tag2

    tag = new TagCache("2", null, "Tag2", "Tag2 is here.", Double.parseDouble(latitude),
            Double.parseDouble(longitude), "", "10", "10", 0.0f, "Lahore", "Punjab", "Pakistan", false);

    openGLModel = new OpenGLModelCache();
    openGLModel.resetVector();

    /**
     * I am getting the bearing of the tag with the device right here, by
     * using the latitude and longitude of both the tag and the device. Then
     * i am saving the value as the tag's heading.
     */
    tagLocation.setLatitude(Double.parseDouble("31.459379"));
    tagLocation.setLongitude(Double.parseDouble("74.368984"));
    distance = deviceLocation.distanceTo(tagLocation);
    bearing = deviceLocation.bearingTo(tagLocation);

    if (bearing < 0) {
        bearing = (-1) * bearing;
        bearing = 180 + (180 - bearing);
    } // end if

    bearingRounded = new BigDecimal(bearing).setScale(0, BigDecimal.ROUND_HALF_UP);
    bearing = Float.parseFloat(bearingRounded.toString());

    Log.e(Utilities.TAG, "bearing: " + bearing.toString());

    openGLModel.setHeading(bearing);// dataObject.getString("direction")));
    tag.setDirection(bearing);
    tag.setOpenGLModel(openGLModel);
    tag.setDistance(distance.toString());
    TagListCache.getTagsList().add(tag);

    // tag3

    tag = new TagCache("3", null, "Tag3", "Tag3 is here.", Double.parseDouble(latitude),
            Double.parseDouble(longitude), "", "10", "10", 0.0f, "Lahore", "Punjab", "Pakistan", false);

    openGLModel = new OpenGLModelCache();
    openGLModel.resetVector();

    /**
     * I am getting the bearing of the tag with the device right here, by
     * using the latitude and longitude of both the tag and the device. Then
     * i am saving the value as the tag's heading.
     */
    tagLocation.setLatitude(Double.parseDouble("31.458804"));
    tagLocation.setLongitude(Double.parseDouble("74.368874"));
    distance = deviceLocation.distanceTo(tagLocation);
    bearing = deviceLocation.bearingTo(tagLocation);

    if (bearing < 0) {
        bearing = (-1) * bearing;
        bearing = 180 + (180 - bearing);
    } // end if

    bearingRounded = new BigDecimal(bearing).setScale(0, BigDecimal.ROUND_HALF_UP);
    bearing = Float.parseFloat(bearingRounded.toString());

    Log.e(Utilities.TAG, "bearing: " + bearing.toString());

    openGLModel.setHeading(bearing);// dataObject.getString("direction")));
    tag.setDirection(bearing);
    tag.setOpenGLModel(openGLModel);
    tag.setDistance(distance.toString());
    TagListCache.getTagsList().add(tag);

    // tag4

    tag = new TagCache("4", null, "Tag4", "Tag4 is here.", Double.parseDouble(latitude),
            Double.parseDouble(longitude), "", "10", "10", 0.0f, "Lahore", "Punjab", "Pakistan", false);

    openGLModel = new OpenGLModelCache();
    openGLModel.resetVector();

    /**
     * I am getting the bearing of the tag with the device right here, by
     * using the latitude and longitude of both the tag and the device. Then
     * i am saving the value as the tag's heading.
     */
    tagLocation.setLatitude(Double.parseDouble("31.458651"));
    tagLocation.setLongitude(Double.parseDouble("74.369171"));
    distance = deviceLocation.distanceTo(tagLocation);
    bearing = deviceLocation.bearingTo(tagLocation);

    if (bearing < 0) {
        bearing = (-1) * bearing;
        bearing = 180 + (180 - bearing);
    } // end if

    bearingRounded = new BigDecimal(bearing).setScale(0, BigDecimal.ROUND_HALF_UP);
    bearing = Float.parseFloat(bearingRounded.toString());

    Log.e(Utilities.TAG, "bearing: " + bearing.toString());

    openGLModel.setHeading(bearing);// dataObject.getString("direction")));
    tag.setDirection(bearing);
    tag.setOpenGLModel(openGLModel);
    tag.setDistance(distance.toString());
    TagListCache.getTagsList().add(tag);

    /*
     * I am building the combined tags list to be used by the Browser
     */

    temporaryList.clear();
    CombinedTagListCache.getCombinedList().clear();
    CombinedTagCache combinedTag;
    int counter = 1;

    for (int i = 0; i < TagListCache.getTagsList().size(); ++i) {
        temporaryList.add(TagListCache.getTagsList().get(i).getTitle());

        /**
         * I am creating the combined tags list on the basis that each tag
         * in the group is separated (direction of each tag is away from
         * each other by the value given by
         * Utilities.combinedDirectionCriteria degrees) by
         * Utilities.combinedDirectionCriteria degrees. I am grouping
         * together tags because there is limited real estate in the
         * Augmented Reality View Screen.
         */
        combinedTag = new CombinedTagCache();

        if (!TagListCache.getTagsList().get(i).isGrouped()) {
            TagListCache.getTagsList().get(i).setGrouped(true);
            combinedTag.setId(((Integer) counter).toString());
            combinedTag.getTagList().add(TagListCache.getTagsList().get(i));
            combinedTag.setOpenGLModel(TagListCache.getTagsList().get(i).getOpenGLModel());
            combinedTag.setDirection(TagListCache.getTagsList().get(i).getDirection());
            combinedTag.setLatitude(TagListCache.getTagsList().get(i).getLatitude());
            combinedTag.setLongitude(TagListCache.getTagsList().get(i).getLongitude());

            CombinedTagListCache.getCombinedList().add(combinedTag);

            for (int j = 0; j < TagListCache.getTagsList().size(); j++) {
                if (!TagListCache.getTagsList().get(j).isGrouped()) {
                    if (Math.abs(TagListCache.getTagsList().get(i).getDirection() - TagListCache.getTagsList()
                            .get(j).getDirection()) < Utilities.COMBINED_DIRECTION_CRITERIA) {
                        combinedTag.getTagList().add(TagListCache.getTagsList().get(j));
                        TagListCache.getTagsList().get(j).setGrouped(true);
                    } // end if
                } // end if
            } // end for

            counter++;
        } // end if

    } // end for

    Log.e("combined tags size: ", "" + CombinedTagListCache.getCombinedList().size());

    for (int i = 0; i < CombinedTagListCache.getCombinedList().size(); i++) {
        Log.e("combined: ", "" + CombinedTagListCache.getCombinedList().get(i).getId());

        for (int j = 0; j < CombinedTagListCache.getCombinedList().get(i).getTagList().size(); j++) {

            Log.e("combined tag: ",
                    "" + CombinedTagListCache.getCombinedList().get(i).getTagList().get(j).getDirection());

        } // end for

    } // end for

    adapter.notifyDataSetChanged();

    // tagListView.setVisibility(View.VISIBLE);

    if (null != Lists.getActionProgressBar()) {
        Lists.getActionProgressBar().collapseActionView();
        Lists.getActionProgressBar().setActionView(null);

    } // end if

}