Example usage for android.location Location distanceBetween

List of usage examples for android.location Location distanceBetween

Introduction

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

Prototype

public static void distanceBetween(double startLatitude, double startLongitude, double endLatitude,
        double endLongitude, float[] results) 

Source Link

Document

Computes the approximate distance in meters between two locations, and optionally the initial and final bearings of the shortest path between them.

Usage

From source file:Main.java

public static double getBearing(double startLatitude, double startLongitude, double endLatitude,
        double endLongitude) {
    float[] results = new float[2];
    Location.distanceBetween(startLatitude, startLongitude, endLatitude, endLongitude, results);
    return results[1];
}

From source file:Main.java

/**
 * using WSG84 using the Metric system/* w  w w.  j a va  2  s.  c o  m*/
 * 
 * @return meters distance
 */
public static float getDistance(double startLati, double startLongi, double goalLati, double goalLongi) {
    float[] resultArray = new float[5]; // meters
    Location.distanceBetween(startLati, startLongi, goalLati, goalLongi, resultArray);
    return resultArray[0];
}

From source file:Main.java

/**
 * //from  w ww  .j av a2 s .c o m
 * @param lat1
 * @param lng1
 * @param lat2
 * @param lng2
 * @return distance in km
 */

public static float distFrom(double lat1, double lng1, double lat2, double lng2) {
    Log.e("lat1" + lat1 + "  long1" + lng1, "lat 2" + lat2 + "  long2" + lng2);
    /*double earthRadius = 6371;
    double dLat = Math.toRadians(lat2 - lat1);
    double dLng = Math.toRadians(lng2 - lng1);
    lat1 = Math.toRadians(lat1);
    lat2 = Math.toRadians(lat2);
    double sindLat = Math.sin(dLat / 2);
    double sindLng = Math.sin(dLng / 2);
    double a = Math.pow(sindLat, 2) + Math.pow(sindLng, 2) * Math.cos(lat1) * Math.cos(lat2);
    double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
    double dist = earthRadius * c;
    Log.e("difference in distance",""+dist);
    return distance(lat1, lng1, lat2, lng2);*/
    float[] result = new float[3];
    Location.distanceBetween(lat1, lng1, lat2, lng2, result);
    //conversion to m
    result[0] = result[0] / 1000;

    return result[0];

}

From source file:Main.java

private static float getDistance(double lat1, double lon1, double lat2, double lon2) {
    float results[] = new float[10];
    Location.distanceBetween(lat1, lon1, lat2, lon2, results);
    return results[0];
}

From source file:uk.co.senab.photup.model.Place.java

public void calculateDistanceFrom(Location location) {
    float[] results = new float[1];
    Location.distanceBetween(mLatitude, mLongitude, location.getLatitude(), location.getLongitude(), results);
    mDistanceFromLocation = Math.round(results[0]);
}

From source file:pl.mg6.newmaps.demo.ShapesExampleActivity.java

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.shapes_example);

    GoogleMap map = GoogleMapHelper.getMap(this, R.id.shapes_map);

    map.setOnMapClickListener(new OnMapClickListener() {

        @Override// w ww.j  a va2s.  c o  m
        public void onMapClick(LatLng position) {
            LatLng center = circle.getCenter();
            float[] results = new float[1];
            Location.distanceBetween(center.latitude, center.longitude, position.latitude, position.longitude,
                    results);
            if (circle.getRadius() > results[0]) {
                circle.setRadius(results[0]);
            }
            Log.i("latlng", String.format(Locale.US, "options.add(new LatLng(%.4f, %.4f));", position.latitude,
                    position.longitude));
        }
    });

    addCircle(map);
    addPolygon(map);
    addPolyline(map);
    addGroundOverlay(map);
}

From source file:com.poguico.palmabici.widgets.StationInfoWidget.java

@Override
public void onOpen(Object item) {
    //super.onOpen(item);
    eItem = (ExtendedOverlayItem) item;//from  ww  w .  j  a  v a  2s  . com

    int freeBikes, freeSlots;
    LinearLayout.LayoutParams layoutParams;
    String formattedDistance = "";

    float[] distance = new float[1];
    Location myLocation = locationSynchronizer.getLocation();
    station = networkInformation.get(Integer.parseInt(eItem.getDescription()));

    TextView title = (TextView) mView.findViewById(R.id.markerTitle);
    TextView tvFreeBikes = (TextView) mView.findViewById(R.id.freeBikes);
    TextView tvFreeSlots = (TextView) mView.findViewById(R.id.freeSlots);
    LinearLayout lyBrokenApparel = (LinearLayout) mView.findViewById(R.id.brokenApparel);

    freeBikes = station.getBusySlots();
    freeSlots = station.getFreeSlots();

    if (myLocation != null) {
        Location.distanceBetween(station.getLat(), station.getLong(), myLocation.getLatitude(),
                myLocation.getLongitude(), distance);

        formattedDistance += " (" + Formatter.formatDistance(distance[0], context) + ")";
    }

    title.setText(eItem.getTitle() + formattedDistance);
    tvFreeBikes.setText(String.valueOf(freeBikes));
    tvFreeSlots.setText(String.valueOf(freeSlots));

    lyBrokenApparel.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, 0));

    if (station.getBusySlots() == 0) {
        layoutParams = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        layoutParams.setMargins(5, 5, 5, 5);
        alarmButton.setLayoutParams(layoutParams);

        if (networkSynchronizer.hasAlarm(station.getId())) {
            alarmButton.setImageResource(R.drawable.bell_active);
            active = true;
        } else {
            alarmButton.setImageResource(R.drawable.bell);
            active = false;
        }
    } else {
        layoutParams = new LinearLayout.LayoutParams(0, 0);
        layoutParams.setMargins(0, 0, 0, 0);
        alarmButton.setLayoutParams(layoutParams);
    }
    alarmButton.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            toggleAlarm();
            String filename = "marker0";
            filename += (NetworkStationAlarm.hasAlarm(station.getId())) ? "_alarm" : "";
            try {
                eItem.setMarker(context.getResources()
                        .getDrawable(R.drawable.class.getDeclaredField(filename).getInt(null)));
            } catch (NotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IllegalArgumentException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (NoSuchFieldException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    });

    locationSynchronizer.addSynchronizableElement(this);
}

From source file:com.cyrilmottier.android.polaris2demo.CircleDemoActivity.java

private static double toRadiusMeters(LatLng center, LatLng radius) {
    float[] result = new float[1];
    Location.distanceBetween(center.latitude, center.longitude, radius.latitude, radius.longitude, result);
    return result[0];
}

From source file:de.quist.app.maps.example.CircleDemoActivity.java

private static double toRadiusMeters(LatLng center, LatLng radius) {
    float[] result = new float[1];
    Location.distanceBetween(center.latitude(), center.longitude(), radius.latitude(), radius.longitude(),
            result);//  ww w  .  j  a  v  a 2 s . c o m
    return result[0];
}

From source file:com.yammy.meter.map.MainMapBengkel.java

private void drawMarker(Location myLocation) {
    //map.clear();
    try {/*  w  ww . j  av  a 2 s . c  o m*/
        this.currentLocation.remove();
    } catch (NullPointerException e) {
    }

    //current location
    LatLng currentPosition = new LatLng(myLocation.getLatitude(), myLocation.getLongitude());
    this.currentLocation = map.addMarker(new MarkerOptions().position(currentPosition)
            .snippet("Lat:" + myLocation.getLatitude() + "Lng:" + myLocation.getLongitude())
            .icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_AZURE)).title("ME"));

    //data location
    float[] lengthdata = new float[3];
    try {
        this.dataLocation = map.addMarker(new MarkerOptions().position(data_pos).title(bundle.getString("nama"))
                .snippet(bundle.getString("alamat")));
        map.moveCamera(CameraUpdateFactory.newLatLngZoom(data_pos, 18.0f));
        Location.distanceBetween(myLocation.getLatitude(), myLocation.getLongitude(), data_pos.latitude,
                data_pos.longitude, lengthdata);
        this.jarak.setText("Jarak : " + String.format("%.2f", lengthdata[0]) + " m");
    } catch (Exception e) {
        map.moveCamera(CameraUpdateFactory.newLatLngZoom(currentPosition, 18.0f));
        this.jarak.setText("Jarak : 0 m");
    }
}