Android Open Source - capture-the-flag Location Utils






From Project

Back to project page capture-the-flag.

License

The source code is released under:

Copyright ? 2014 Microsoft Mobile Oy. All rights reserved. Microsoft is a registered trademark of Microsoft Corporation. Nokia and HERE are trademarks and/or registered trademarks of Nokia Corporati...

If you think the Android project capture-the-flag listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

/*
 * Copyright (c) 2014 Microsoft Mobile. All rights reserved.
 * See the license text file provided with this project for more information.
 */// w w  w .ja  v  a 2 s . co m

package com.nokia.example.capturetheflag.location;

import android.util.Log;

import com.here.android.common.GeoCoordinate;

/**
 * See http://www.movable-type.co.uk/scripts/latlong.html for the basis of the
 * algorithms implemented by this class.
 */
public class LocationUtils {
    private static final String TAG = "CtF/LocationUtils";
    private static final double EARTH_RADIUS_IN_METERS = 6371000.0;

    /**
     * Calculates the target coordinate from the given coordinate with the given
     * bearing (in radians) and the distance (in meters).
     *
     * @param fromLatitudeInRads  Source latitude in radians.
     * @param fromLongitudeInRads Source longitude in radians.
     * @param toBearingInRads     The bearing in radians.
     * @param distanceInMeters
     * @return The target coordinates in degrees.
     */
    public static double[] calculateTargetCoordinate(
            final double fromLatitudeInRads, final double fromLongitudeInRads,
            final double toBearingInRads, final double distanceInMeters) {
        Log.d(TAG,
                "From (" + Math.toDegrees(fromLatitudeInRads) + ", "
                        + Math.toDegrees(fromLongitudeInRads)
                        + ") with bearing " + Math.toDegrees(toBearingInRads)
                        + " (degrees) distance of " + distanceInMeters
                        + " meters");

        final double distance = distanceInMeters / EARTH_RADIUS_IN_METERS;

        double toLatitude = Math.asin(Math.sin(fromLatitudeInRads)
                * Math.cos(distance) + Math.cos(fromLatitudeInRads)
                * Math.sin(distance) * Math.cos(toBearingInRads));

        final double a = Math.atan2(
                Math.sin(toBearingInRads) * Math.sin(distance)
                        * Math.cos(fromLatitudeInRads), Math.cos(distance)
                        - Math.sin(fromLatitudeInRads) * Math.sin(toLatitude));
        double toLongitude = fromLongitudeInRads + a;
        toLongitude = (toLongitude + 3 * Math.PI) % (2 * Math.PI) - Math.PI;

        toLatitude = Math.toDegrees(toLatitude);
        toLongitude = Math.toDegrees(toLongitude);

        return new double[]{toLatitude, toLongitude};
    }

    /**
     * For convenience.
     */
    public static double[] calculateTargetCoordinate(
            GeoCoordinate fromCoordinate, final double toBearingInRads,
            final double distanceInMeters) {
        final double fromLatitudeInRads = Math.toRadians(fromCoordinate
                .getLatitude());
        final double fromLongitudeInRads = Math.toRadians(fromCoordinate
                .getLongitude());
        return calculateTargetCoordinate(fromLatitudeInRads,
                fromLongitudeInRads, toBearingInRads, distanceInMeters);
    }

    /**
     * For convenience.
     */
    public static double[] calculateTargetCoordinateInputInDegrees(
            final double fromLatitude, final double fromLongitude,
            final double toBearingInDegrees, final double distanceInMeters) {
        return calculateTargetCoordinate(Math.toRadians(fromLatitude),
                Math.toRadians(fromLongitude),
                Math.toRadians(toBearingInDegrees), distanceInMeters);
    }

    /**
     * Calculates the distance (in meters) between the two given coordinates.
     *
     * @param latitude1InRads
     * @param longitude1InRads
     * @param latitude2InRads
     * @param longitude2InRads
     * @return The distance between the two given points in meters.
     */
    public static double calculateDistanceInMeters(
            final double latitude1InRads, final double longitude1InRads,
            final double latitude2InRads, final double longitude2InRads) {
        double latitudeDelta = latitude2InRads - latitude1InRads;
        double longitudeDelta = longitude2InRads - longitude1InRads;

        double a = Math.sin(latitudeDelta / 2) * Math.sin(latitudeDelta / 2)
                + Math.sin(longitudeDelta / 2) * Math.sin(longitudeDelta / 2)
                * Math.cos(latitude1InRads) * Math.cos(latitude2InRads);
        double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));

        return EARTH_RADIUS_IN_METERS * c;
    }

    /**
     * For convenience.
     */
    public static double calculateDistanceInMetersInputInDegrees(
            final double latitude1, final double longitude1,
            final double latitude2, final double longitude2) {
        return calculateDistanceInMeters(Math.toRadians(latitude1),
                Math.toRadians(longitude1), Math.toRadians(latitude2),
                Math.toRadians(longitude2));
    }

    /**
     * Calculates the bearing (in radians) from the source coordinate to the
     * destination coordinate.
     *
     * @param fromLatitudeInRads
     * @param fromLongitudeInRads
     * @param toLatitudeInRads
     * @param toLongitudeInRads
     * @return The bearing in radians.
     */
    public static double calculateBearing(final double fromLatitudeInRads,
                                          final double fromLongitudeInRads, final double toLatitudeInRads,
                                          final double toLongitudeInRads) {
        final double latitudeDelta = toLatitudeInRads - fromLatitudeInRads;
        final double longitudeDelta = toLongitudeInRads - fromLongitudeInRads;

        final double y = Math.sin(longitudeDelta) * Math.cos(latitudeDelta);
        final double x = Math.cos(fromLatitudeInRads)
                * Math.sin(toLatitudeInRads) - Math.sin(fromLatitudeInRads)
                * Math.cos(toLatitudeInRads) * Math.cos(longitudeDelta);

        return Math.atan2(y, x);
    }

    /**
     * For convenience.
     */
    public static double calculateBearing(GeoCoordinate from, GeoCoordinate to) {
        final double fromLatitudeInRads = Math.toRadians(from.getLatitude());
        final double fromLongitudeInRads = Math.toRadians(from.getLongitude());
        final double toLatitudeInRads = Math.toRadians(to.getLatitude());
        final double toLongitudeInRads = Math.toRadians(to.getLongitude());
        return calculateBearing(fromLatitudeInRads, fromLongitudeInRads,
                toLatitudeInRads, toLongitudeInRads);
    }

    /**
     * For convenience. The input is expected in degrees, but note that the
     * output (bearing) is in radians.
     */
    public static double calculateBearingInputInDegrees(
            final double fromLatitudeInDegs, final double fromLongitudeInDegs,
            final double toLatitudeInDegs, final double toLongitudeInDegs) {
        return calculateBearing(Math.toRadians(fromLatitudeInDegs),
                Math.toRadians(fromLongitudeInDegs),
                Math.toRadians(toLatitudeInDegs),
                Math.toRadians(toLongitudeInDegs));
    }
}




Java Source Code List

com.nokia.example.capturetheflag.AboutActivity.java
com.nokia.example.capturetheflag.Controller.java
com.nokia.example.capturetheflag.CreateGameFragment.java
com.nokia.example.capturetheflag.GameEndedDialogFragment.java
com.nokia.example.capturetheflag.GameMenuFragment.java
com.nokia.example.capturetheflag.HelpActivity.java
com.nokia.example.capturetheflag.JoinGameFragment.java
com.nokia.example.capturetheflag.MainActivity.java
com.nokia.example.capturetheflag.PauseDialog.java
com.nokia.example.capturetheflag.PurchasePremiumFragment.java
com.nokia.example.capturetheflag.ServerSettingsDialog.java
com.nokia.example.capturetheflag.Settings.java
com.nokia.example.capturetheflag.location.LocationManagerBase.java
com.nokia.example.capturetheflag.location.LocationManagerFactory.java
com.nokia.example.capturetheflag.location.LocationManagerInterface.java
com.nokia.example.capturetheflag.location.LocationManagerListener.java
com.nokia.example.capturetheflag.location.LocationUtils.java
com.nokia.example.capturetheflag.location.google.LocationManagerGoogle.java
com.nokia.example.capturetheflag.location.here.LocationManagerHere.java
com.nokia.example.capturetheflag.map.GameMapFactory.java
com.nokia.example.capturetheflag.map.GameMapInterface.java
com.nokia.example.capturetheflag.map.GameMapUtils.java
com.nokia.example.capturetheflag.map.MarkerFactoryBase.java
com.nokia.example.capturetheflag.map.google.GameMapGoogle.java
com.nokia.example.capturetheflag.map.google.MarkerFactoryGoogle.java
com.nokia.example.capturetheflag.map.here.GameMapHere.java
com.nokia.example.capturetheflag.map.here.MarkerFactoryHere.java
com.nokia.example.capturetheflag.network.FlagCapturedResponse.java
com.nokia.example.capturetheflag.network.GameListRequest.java
com.nokia.example.capturetheflag.network.GameListResponse.java
com.nokia.example.capturetheflag.network.JSONRequest.java
com.nokia.example.capturetheflag.network.JSONResponse.java
com.nokia.example.capturetheflag.network.JoinRequest.java
com.nokia.example.capturetheflag.network.JoinedResponse.java
com.nokia.example.capturetheflag.network.NetworkClient.java
com.nokia.example.capturetheflag.network.OfflineClient.java
com.nokia.example.capturetheflag.network.SocketIONetworkClient.java
com.nokia.example.capturetheflag.network.UpdatePlayerRequest.java
com.nokia.example.capturetheflag.network.UpdatePlayerResponse.java
com.nokia.example.capturetheflag.network.model.Flag.java
com.nokia.example.capturetheflag.network.model.Game.java
com.nokia.example.capturetheflag.network.model.ModelConstants.java
com.nokia.example.capturetheflag.network.model.Player.java
com.nokia.example.capturetheflag.notifications.NotificationsManagerBase.java
com.nokia.example.capturetheflag.notifications.NotificationsManagerFactory.java
com.nokia.example.capturetheflag.notifications.NotificationsManagerInterface.java
com.nokia.example.capturetheflag.notifications.NotificationsUtils.java
com.nokia.example.capturetheflag.notifications.google.GcmBroadcastReceiver.java
com.nokia.example.capturetheflag.notifications.google.GcmIntentService.java
com.nokia.example.capturetheflag.notifications.google.NotificationsManagerGoogle.java
com.nokia.example.capturetheflag.notifications.nokia.NokiaNotificationsBroadcastReceiver.java
com.nokia.example.capturetheflag.notifications.nokia.NokiaNotificationsIntentService.java
com.nokia.example.capturetheflag.notifications.nokia.NotificationsManagerNokia.java