21

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...
  }
}
Philipp Jahoda
  • 50,880
  • 24
  • 180
  • 187
user2590541
  • 522
  • 3
  • 7
  • 19
  • 2
    You need to write a function distance, it doesn't exist in Android. You might also want to check this: http://developer.android.com/training/location/geofencing.html – Ran Aug 11 '13 at 08:34
  • sir i simply want to compare two latitudes and longitude values. if matched then want to call one event else other event. is there any other simple way to do that... the is my complete code http://stackoverflow.com/questions/18150929/how-to-call-event-depening-upon-latitide-and-longitude/18152841?noredirect=1#18152841 – user2590541 Aug 11 '13 at 09:18
  • so it the problem of distance method. will you post the signature of distance method. – Sandeep Aug 11 '13 at 09:28
  • The problem is that he does not have a distance method. – Philipp Jahoda Aug 11 '13 at 09:36

3 Answers3

42

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
}
Boken
  • 4,825
  • 10
  • 32
  • 42
Philipp Jahoda
  • 50,880
  • 24
  • 180
  • 187
  • sir i simply want to compare two latitudes and longitude values. if matched then want to call one event else other event. is there any other simple way to do that... this is my complete code http://stackoverflow.com/questions/18150929/how-to-call-event-depening-upon-latitide-and-longitude/18152841?noredirect=1#18152841 – user2590541 Aug 11 '13 at 09:20
  • 1
    Then you should express yourself that way and not use a function called distance that doesnt exist. – Philipp Jahoda Aug 11 '13 at 09:21
  • i tried but got errors... can u plz do a sample coding for this purpose – user2590541 Aug 11 '13 at 09:24
  • 3
    You have the complete example in my code. This method calculates the distance between two locations, and it can of course also be used for comparison. For example if the distance between 2 locations is below 0.01 miles you could take them as equal. – Philipp Jahoda Aug 11 '13 at 09:26
  • i tried it but it not giving the desire outputs, kindly check it what wrong i am doing... – user2590541 Aug 11 '13 at 10:35
  • Maybe the problem is `if (distance(lat,lon,currentLat,currentLon)<2.0)` What is 2.0? – iCantSeeSharp Aug 11 '13 at 11:11
  • i tried it but it not giving the desire outputs, kindly check it what wrong i am doing... i have a sub class in which i am working on onlocationchanges() and then i am calling it in main activity. when i call it in main it shows error for parameters i try fixing it with 2 possibilities 1. Giving parameter null 2. giving parameter value Location But in both cases it just terminate my app any idea sir ??? – user2590541 Aug 11 '13 at 11:20
17

You can use the Location class's static distanceBetween() method like so:

import android.location.Location

float[] distance = new float[1];

Location.distanceBetween(lat, lon, currentLat, currentLon, distance);

// distance[0] is now the distance between these lat/lons in meters
if (distance[0] < 2.0) {
    // your code...
}

If you have two Location objects, another option is distanceTo()

Boken
  • 4,825
  • 10
  • 32
  • 42
yuval
  • 6,369
  • 3
  • 32
  • 44
0

All the 4 input parameters are in float. Distance is a relative one not real distance. You need to convert this distance to a real distance by searching some formula online :( I used this in my app to get nearest metro station from my current location. Hope this snippet helps someone :)

float distance(float lat,float lon,float clat,float clon)
{
    float distance;
    float temp1;
    float temp2;
    temp1=(float)((lat-clat)*(lat-clat));
    temp2=(float)((lon-clon)*(lon-clon));
    distance=temp1+temp2;
    return distance;
}