简体   繁体   English

在行驶时计算android中两个位置之间的距离

[英]Calculating distance between two locations in android while driving

I have developed an app like uber but I am facing issue in calculating correct fare due to incorrect distance from pick up location to the location where trip completed. 我已经开发了类似uber的应用,但是由于从接送地点到旅程完成的地点之间的距离不正确,我在计算正确的票价时遇到了问题。 What I have done and doing: 我所做和正在做的事情:

1st approach 第一种方法

I was getting distance between between current location of the driver(save location when trip started) and end location(where trip completed) like this. 我正在这样获得驾驶员当前位置(行程开始时保存位置)和终点位置(行程完成位置)之间的距离。

distanceInMeters = locationChanged.getmCurrentlocation().distanceTo(loc_pick_up);

but problem with this code is. 但是这段代码的问题是。 if passenger pick up location(where trip started) and drop off location(where trip completed) is same distance is zero. 如果乘客上车地点(行程开始的地方)和下车地点(行程结束的地方)的距离是零。

2nd Approach 第二种方法

Now I am calling a method every minute. 现在,我每分钟都会调用一个方法。 which when executed first time. 第一次执行时。 calculate distance between pickup location(from where trip started) and current location(location after 1 minute) and save difference in a variable. 计算取货地点(从旅行开始的地点)到当前地点(1分钟后的地点)之间的距离,并将差异保存在变量中。 and than current location becomes pickup location and when method is executed again. 然后当前位置变为拾取位置,并在再次执行方法时变为。 it calculates distance between pickuplocation and current location (between location before and after 1 minutes) and add the result in already saved distance. 它计算拾取位置与当前位置之间的距离(1分钟前后的位置之间),并将结果添加到已保存的距离中。

private void locationDistanceHandler() {

    handlerLocation = new Handler();
    runnableCode = new Runnable() {
        @Override
        public void run() {

            Location loc_pick_up = new Location("");
            if (!pick_up_value_used) {
                loc_pick_up.setLatitude(pickUpLatLng.latitude);
                loc_pick_up.setLongitude(pickUpLatLng.longitude);
                locationChanged.setmPickUpLocation(locationChanged.getmCurrentlocation());
                pick_up_value_used = true;
            } else {
                loc_pick_up.setLatitude(locationChanged.getmPickUpLocation().getLatitude());
                loc_pick_up.setLongitude(locationChanged.getmPickUpLocation().getLongitude());
            }
            Log.d(Constants.LOG, "handle location called:" + distanceInMeters);
            distanceInMeters = distanceInMeters + locationChanged.getmCurrentlocation().distanceTo(loc_pick_up);
            handlerLocation.postDelayed(this, 60000);
        }
    };
    handlerLocation.postDelayed(runnableCode, 60000);
}

But this is not accurate. 但这是不准确的。 today I travelled 20 km from office to home in 18 minutes. 今天我在18分钟内从办公室到家走了20公里。 but this method calculated distance 6.8km. 但此方法计算出的距离为6.8公里。

Please suggest me a better way of doing this or any working source code example. 请建议我做一个更好的方法或任何有效的源代码示例。 so I can resolve this issue. 所以我可以解决这个问题。

float[] results=new float[5];
                loc_pick_up.distanceBetween(startLatitude,startLongitude, endLatitude, endLongitude,results);
                String calculatedDistance=results[0];
                System.out.println("Distance between point A and point B are: " + calulcatedDistance);

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

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