计算两个纬度和经度点之间的持续时间或预期行驶时间

时间:2018-07-18 09:29:28

标签: java android google-maps-android-api-2 google-distancematrix-api

我想计算两点之间的持续时间。我知道有Google Map API,我不想使用它,我需要方程式来完成。有一个计算距离,我需要一个持续时间或行驶时间。

3 个答案:

答案 0 :(得分:0)

好吧,猜一猜如何在不了解

的情况下实现该目标
  • 道路
  • 速度限制
  • 交通

很难。我看到2种解决方案:

使用Google Distance Matrix API

https://developers.google.com/maps/documentation/distance-matrix/start

https://maps.googleapis.com/maps/api/distancematrix/json?origins=my_origins&destinations=my_destinations&departure_time=now

估计速度

StringFormat

答案 1 :(得分:0)

 @Override
    protected void onPostExecute(List<List<HashMap<String, String>>> result) {
        ArrayList<LatLng> points = null;
        PolylineOptions lineOptions = null;
        MarkerOptions markerOptions = new MarkerOptions();
        String distance = "";
        String duration = "";


        if (result.size() < 1) {
            Toast.makeText(getBaseContext(), "No Points", Toast.LENGTH_SHORT).show();
            return;
        }


        // Traversing through all the routes
        for (int i = 0; i < result.size(); i++) {
            points = new ArrayList<LatLng>();
            lineOptions = new PolylineOptions();

            // Fetching i-th route
            List<HashMap<String, String>> path = result.get(i);

            // Fetching all the points in i-th route
            for (int j = 0; j < path.size(); j++) {
                HashMap<String, String> point = path.get(j);

                if (j == 0) {    // Get distance from the list
                    distance = (String) point.get("distance");
                    continue;
                } else if (j == 1) { // Get duration from the list
                    duration = (String) point.get("duration");
                    continue;
                }

                double lat = Double.parseDouble(point.get("lat"));
                double lng = Double.parseDouble(point.get("lng"));
                LatLng position = new LatLng(lat, lng);

                points.add(position);
            }

            // Adding all the points in the route to LineOptions
            lineOptions.addAll(points);
            lineOptions.width(10);
            lineOptions.color(Color.GREEN);

        }

        text.setText("Distance:" + distance + ", Duration:" + duration);
        Log.e("Final distance", "Distance:" + distance + ", Duration:" + duration);
        // Drawing polyline in the Google Map for the i-th route
        mMap.addPolyline(lineOptions);
    }
}

答案 2 :(得分:0)

public String getTimeTaken(Location source, Location dest) {


    double meter = source.distanceTo(dest);

    double kms = meter / 1000;

    double kms_per_min = 0.5;

    double mins_taken = kms / kms_per_min;

    int totalMinutes = (int) mins_taken;

    Log.d("ResponseT","meter :"+meter+ " kms : "+kms+" mins :"+mins_taken);

    if (totalMinutes<60)
    {
        return ""+totalMinutes+" mins";
    }else {
        String minutes = Integer.toString(totalMinutes % 60);
        minutes = minutes.length() == 1 ? "0" + minutes : minutes;
        return (totalMinutes / 60) + " hour " + minutes +"mins";

    }


}

像这样调用该方法

 binding.estimationTime.setText("" + getTimeTaken(deliveryboy_location, userLocation) );
相关问题