Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Comparing two locations using their Longitude and Latitude

Hi i need to cal an event at onchangelocation(), by comparing current latitude and longitude with some saved latitude and longitude but i m getting an error. eclipse is not recognizing key word distance, and for correction error it is giving hint to crate method "distance" having 4 parameter ... how to fix it??? or some other way to do same work???

thanks and regards. code is attached below

@Override
public void onLocationChanged(Location location) {
 double currentLat=location.getLatitude();
 double currentLon=location.getLongitude();

    if (distance(lat,lon,currentLat,currentLon)<2.0){
 //do what you want to do...
  }
}
like image 251
user2590541 Avatar asked Aug 11 '13 08:08

user2590541


People also ask

How do you compare two latitudes?

Take the smaller away from the larger to get the difference. 4.74 degrees is the difference in latitude. As another example you can use Denver (again) and Buenos Aires. Denver, once again, is 39.74 degrees and Buenos Aires is -34.61 degrees south of the equator.

Why do we use latitude and longitude to compare places on maps?

Latitude and longitude make up the grid system that helps us identify absolute, or exact, locations on the Earth's surface. You can use latitude and longitude to identify specific locations. Latitude and longitude are also helpful in identifying landmarks.

What do we call the method of locating places using longitude and latitude?

Absolute Location describes precise locations of a place based on a fixed point on earth. The most common way to identify a location is by using coordinates such as latitude and longitude.


1 Answers

You actually need to implement a function called distance that will calculate the distance between two locations. Calculating the distance between two locations is one possible way of comparing the longitude and latitude values.

An example of comparing them:

@Override
public void onLocationChanged(Location location) {

 double lat2 = location.getLatitude();
 double lng2 = location.getLongitude();

    // lat1 and lng1 are the values of a previously stored location
    if (distance(lat1, lng1, lat2, lng2) < 0.1) { // if distance < 0.1 miles we take locations as equal
       //do what you want to do...
    }
}

/** calculates the distance between two locations in MILES */
private double distance(double lat1, double lng1, double lat2, double lng2) {

    double earthRadius = 3958.75; // in miles, change to 6371 for kilometer output

    double dLat = Math.toRadians(lat2-lat1);
    double dLng = Math.toRadians(lng2-lng1);

    double sindLat = Math.sin(dLat / 2);
    double sindLng = Math.sin(dLng / 2);

    double a = Math.pow(sindLat, 2) + Math.pow(sindLng, 2)
        * Math.cos(Math.toRadians(lat1)) * Math.cos(Math.toRadians(lat2));

    double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));

    double dist = earthRadius * c;

    return dist; // output distance, in MILES
}

And in Kotlin:

private fun distance(lat1: Double, lng1: Double, lat2: Double, lng2: Double): Double {
    val earthRadius = 3958.75 // in miles, change to 6371 for kilometer output

    val dLat = Math.toRadians(lat2 - lat1)
    val dLng = Math.toRadians(lng2 - lng1)

    val sindLat = sin(dLat / 2)
    val sindLng = sin(dLng / 2)

    val a = sindLat.pow(2.0) +
            (sindLng.pow(2.0) * cos(Math.toRadians(lat1)) * cos(Math.toRadians(lat2)))

    val c = 2 * atan2(sqrt(a), sqrt(1 - a))

    return earthRadius * c // output distance, in MILES
}
like image 142
Philipp Jahoda Avatar answered Oct 07 '22 12:10

Philipp Jahoda