Example usage for android.location Location distanceTo

List of usage examples for android.location Location distanceTo

Introduction

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

Prototype

public float distanceTo(Location dest) 

Source Link

Document

Returns the approximate distance in meters between this location and the given location.

Usage

From source file:Main.java

public static float calcularDistanciaEntreDosPuntos(Location loc1, Location loc2) {
    return loc1.distanceTo(loc2);
}

From source file:Main.java

public static String getDistanceBetween(Location loc1, Location loc2) {

    if (loc1 != null && loc2 != null) {
        float distance = loc1.distanceTo(loc2) / 1000;

        return distanceFormat.format(distance) + " km";
    }/*  w  w w .j a v a  2 s. com*/

    return "n/a";
}

From source file:Main.java

public static float getDistanceBetweenLocations(Location first, Location second) {
    if (first == null || second == null)
        return 0;

    return first.distanceTo(second);
}

From source file:Main.java

public static double getDistance(double x1, double y1, double x2, double y2) {

    Location l1 = new Location("loc1");
    l1.setLatitude(y1);//from  www  .j av a 2  s.  co  m
    l1.setLongitude(x1);
    Location l2 = new Location("loc2");
    l2.setLatitude(y2);
    l2.setLongitude(x2);

    return l1.distanceTo(l2);
}

From source file:org.streetpacman.util.DMUtils.java

/**
 * Computes the distance on the two sphere between the point c0 and the line
 * segment c1 to c2.// ww  w  . j  av  a  2 s.c  om
 * 
 * @param c0 the first coordinate
 * @param c1 the beginning of the line segment
 * @param c2 the end of the lone segment
 * @return the distance in m (assuming spherical earth)
 */
public static double distance(final Location c0, final Location c1, final Location c2) {
    if (c1.equals(c2)) {
        return c2.distanceTo(c0);
    }

    final double s0lat = c0.getLatitude() * UnitConversions.TO_RADIANS;
    final double s0lng = c0.getLongitude() * UnitConversions.TO_RADIANS;
    final double s1lat = c1.getLatitude() * UnitConversions.TO_RADIANS;
    final double s1lng = c1.getLongitude() * UnitConversions.TO_RADIANS;
    final double s2lat = c2.getLatitude() * UnitConversions.TO_RADIANS;
    final double s2lng = c2.getLongitude() * UnitConversions.TO_RADIANS;

    double s2s1lat = s2lat - s1lat;
    double s2s1lng = s2lng - s1lng;
    final double u = ((s0lat - s1lat) * s2s1lat + (s0lng - s1lng) * s2s1lng)
            / (s2s1lat * s2s1lat + s2s1lng * s2s1lng);
    if (u <= 0) {
        return c0.distanceTo(c1);
    }
    if (u >= 1) {
        return c0.distanceTo(c2);
    }
    Location sa = new Location("");
    sa.setLatitude(c0.getLatitude() - c1.getLatitude());
    sa.setLongitude(c0.getLongitude() - c1.getLongitude());
    Location sb = new Location("");
    sb.setLatitude(u * (c2.getLatitude() - c1.getLatitude()));
    sb.setLongitude(u * (c2.getLongitude() - c1.getLongitude()));
    return sa.distanceTo(sb);
}

From source file:Main.java

public static double getDistance(double lat1, double lng1, double lat2, double lng2) {
    Location locationA = new Location("point A");
    locationA.setLatitude(lat1);// w  w  w . ja  v a  2s.  c  om
    locationA.setLongitude(lng1);

    Location locationB = new Location("point B");
    locationB.setLatitude(lat2);
    locationB.setLongitude(lng2);

    double distance = locationA.distanceTo(locationB);
    return distance;
}

From source file:Main.java

public static Boolean isInsideLocation(Context context, Location current_location, Location searchLocation,
        double radiusLocation) {

    if (current_location == null) {
        current_location = getLastBestLocation(context);
    }/*from ww  w.  j a va2s  .  com*/

    if (current_location != null) {
        float distanceMeters = current_location.distanceTo(searchLocation);
        return (distanceMeters <= radiusLocation);
    } else {
        return false;
    }
}

From source file:Main.java

public static double getCalculatedDistance(double startLatitude, double startLongitude, double targetLatitude,
        double targetLongitude) {

    Location startLocation = new Location("Start Location");

    startLocation.setLatitude(startLatitude);
    startLocation.setLongitude(startLongitude);

    Location targetLocation = new Location("Target Location");

    targetLocation.setLatitude(targetLatitude);
    targetLocation.setLongitude(targetLongitude);

    float distance = startLocation.distanceTo(targetLocation);

    double tempDistanceInMiles = distance * 0.000621371192;

    return tempDistanceInMiles;

}

From source file:Main.java

/**
 * Will return a list of random location from the given position & radius
 * @param amount/*from w  w w  . j  a  va2  s .c o  m*/
 * @param lat
 * @param lng
 * @param radius
 * @return ArrayList<Location> 
 */
public static ArrayList<Location> randomLocations(int amount, double lat, double lng, double radius) {
    ArrayList<Location> locations = new ArrayList<Location>(0);
    Location centerLocation = new Location("local");
    centerLocation.setLatitude(lat);
    centerLocation.setLongitude(lng);

    final double lngScale = Math.cos((Math.PI / 180.0) * lat);
    final double radiusDeg = radius / 111.2; // radius converted to degrees (square)

    while (locations.size() < amount) {
        Location l = new Location("local");
        double dLat = (Math.random() * radiusDeg * 2) - radiusDeg;
        double dLng = (Math.random() * radiusDeg * 2 / lngScale) - (radiusDeg / lngScale);

        l.setLatitude(centerLocation.getLatitude() + dLat);
        l.setLongitude(centerLocation.getLongitude() + dLng);
        double dist = l.distanceTo(centerLocation) / 1000.0;

        if (dist < (radius)) {
            locations.add(l);
        }
    }
    return locations;
}

From source file:uk.ac.horizon.ug.exploding.client.LocationUtils.java

/** distance in metres */
public static double getDistance(double lat1, double lon1, double lat2, double lon2) {
    Location l1 = getLocation(lat1, lon1);
    Location l2 = getLocation(lat2, lon2);
    return l1.distanceTo(l2);
}