-1

I'm new here...please help me

I want to develop android application that calculate distance between user's current location and markers. It will calculate the distance when users touch the makers. I want to use Haversine Formula, but i don't know how to input the current location and markers coordinates into the formula...for example StartP will be my current location coordinates and EndP will be my markers coordinates.

public double CalculationByDistance(LatLng StartP, LatLng EndP) {
    int Radius=6371;//radius of earth in Km         
    double lat1 = StartP.latitude;
    double lat2 = EndP.latitude;
    double lon1 = StartP.longitude;
    double lon2 = EndP.longitude;
    double dLat = Math.toRadians(lat2-lat1);
    double dLon = Math.toRadians(lon2-lon1);
    double a = Math.sin(dLat/2) * Math.sin(dLat/2) +
    Math.cos(Math.toRadians(lat1)) * Math.cos(Math.toRadians(lat2)) *
    Math.sin(dLon/2) * Math.sin(dLon/2);
    double c = 2 * Math.asin(Math.sqrt(a));
    double valueResult= Radius*c;
    double km=valueResult/1;
    DecimalFormat newFormat = new DecimalFormat("####");
    int kmInDec =  Integer.valueOf(newFormat.format(km));
    double meter=valueResult%1000;
    int  meterInDec= Integer.valueOf(newFormat.format(meter));
    Log.i("Radius Value",""+valueResult+"   KM  "+kmInDec+" Meter   "+meterInDec);

    return Radius * c;
 }
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
Lucy
  • 1
  • 1
  • 2

1 Answers1

0

The parameters that are accepted by the formula are of the type LatLng. Now you have not specified what that is any place in the code.
But in the function itself you will find that all you are giving the function is the latitude and longitude of the start point and the endpoint. So, if you have difficulty in giving input to this function, this is what you can do.
The function requires the latitude and longitude in the form of double values. What you get from maps is double values for latitude and longitude. So, you can pass them directly to this function like this.

public double CalculationByDistance(double latitude1, double longitude1, double latitude2, double longitude2) {
        int Radius=6371;//radius of earth in Km         
        double lat1 = latitude1;
        double lat2 = latitude2;
        double lon1 = longitude1;
        double lon2 = longitude2;
        double dLat = Math.toRadians(lat2-lat1);
        double dLon = Math.toRadians(lon2-lon1);
        double a = Math.sin(dLat/2) * Math.sin(dLat/2) +
        Math.cos(Math.toRadians(lat1)) * Math.cos(Math.toRadians(lat2)) *
        Math.sin(dLon/2) * Math.sin(dLon/2);
        double c = 2 * Math.asin(Math.sqrt(a));
        double valueResult= Radius*c;
        double km=valueResult/1;
        DecimalFormat newFormat = new DecimalFormat("####");
        Integer kmInDec =  Integer.valueOf(newFormat.format(km));
        double meter=valueResult%1000;
        Integer  meterInDec= Integer.valueOf(newFormat.format(meter));
        System.out.println("Radius Value "+valueResult+"   KM  "+kmInDec+" Meter   "+meterInDec);

        return Radius * c;
     }

where latitude1 and longitude1 are the co-ordinates of the start point and 2 for the end point.

Piyush
  • 18,895
  • 5
  • 32
  • 63
Msk
  • 857
  • 9
  • 16
  • 1
    Duplicate question but nevertheless, try this. [Link][1] [1]: http://developer.android.com/reference/android/location/Location.html#distanceBetween(double,%20double,%20double,%20double,%20float[]) – Hades May 04 '15 at 05:14
  • how do i pass my current location and marker location to the function??? i still don't get it... – Lucy May 06 '15 at 09:45
  • What do you not get from thr answer above? – Msk May 06 '15 at 09:50
  • i don't get how the function will get my current location and markers coordinates..how should i pass those value to the function? i am new in android development so i don't quite understand.. – Lucy May 06 '15 at 11:42
  • Have you implemented part of using maps api?? Getting location on phone?? Please implement that first. Haversine is last in line for implementation. To do that google required things and follow official documentation – Msk May 06 '15 at 12:17