简体   繁体   English

在 Google Maps v2 上计算当前位置和标记之间的距离

[英]Calculate Distance between Current Location and Marker on Google Maps v2

I'm new here...please help me我是新来的...请帮助我

I want to develop android application that calculate distance between user's current location and markers.我想开发计算用户当前位置和标记之间距离的 android 应用程序。 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.我想使用Haversine 公式,但我不知道如何将当前位置和标记坐标输入到公式中……例如,StartP 将是我当前的位置坐标,EndP 将是我的标记坐标。

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;
 }

The parameters that are accepted by the formula are of the type LatLng .公式接受的参数属于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.其中latitude1longitude1是起点的坐标,2 是终点的坐标。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM