Java 计算两个纬度、经度点之间的持续时间或预期驾驶时间

Java 计算两个纬度、经度点之间的持续时间或预期驾驶时间,java,android,google-maps-android-api-2,google-distancematrix-api,Java,Android,Google Maps Android Api 2,Google Distancematrix Api,我想计算两点之间的持续时间。我知道有谷歌地图API,我不想用它,我需要公式来做。有一种方法可以计算出我需要的持续时间或驾驶时间的距离。好吧,猜猜在不知道 道路 限速 交通 图表 很难。我看到两种解决方案: 使用谷歌距离矩阵API 迅速估计 好吧,猜猜看,如果你不知道 道路 限速 交通 图表 很难。我看到两种解决方案: 使用谷歌距离矩阵API 迅速估计 @覆盖 受保护的void onPostExecute(列表结果){ ArrayList points=null; Polyline

我想计算两点之间的持续时间。我知道有谷歌地图API,我不想用它,我需要公式来做。有一种方法可以计算出我需要的持续时间或驾驶时间的距离。

好吧,猜猜在不知道

  • 道路
  • 限速
  • 交通
  • 图表
很难。我看到两种解决方案:

使用谷歌距离矩阵API

迅速估计
好吧,猜猜看,如果你不知道

  • 道路
  • 限速
  • 交通
  • 图表
很难。我看到两种解决方案:

使用谷歌距离矩阵API

迅速估计
@覆盖
受保护的void onPostExecute(列表结果){
ArrayList points=null;
PolylineOptions lineOptions=null;
MarkerOptions MarkerOptions=新MarkerOptions();
字符串距离=”;
字符串持续时间=”;
if(result.size()<1){
Toast.makeText(getBaseContext(),“无点”,Toast.LENGTH_SHORT.show();
返回;
}
//穿越所有路线
对于(int i=0;i
@覆盖
受保护的void onPostExecute(列表结果){
ArrayList points=null;
PolylineOptions lineOptions=null;
MarkerOptions MarkerOptions=新MarkerOptions();
字符串距离=”;
字符串持续时间=”;
if(result.size()<1){
Toast.makeText(getBaseContext(),“无点”,Toast.LENGTH_SHORT.show();
返回;
}
//穿越所有路线
对于(int i=0;i
持续时间取决于速度。你在寻找最快的流量持续时间吗?请更精确一点:我想要函数,它只取纬度,两个点的经度,它返回从第一个点到第二个点的持续时间或到达时间。它就像谷歌地图一样,当你给它两个点的起点和终点时,它会查看地图,在它们之间有一条线,到达线以上的时间我只需要这一次。持续时间取决于速度。你在寻找最快的流量持续时间吗?请更精确一点:我想要函数它只需要两个点的纬度、经度,它返回从第一个点到第二个点的持续时间或到达时间。它就像谷歌地图一样,当你给它两个点的起点和终点时,它会查看地图,在它们之间有一条线,到达线以上的时间我只需要这一次。
Location loc1 = new Location("");
loc1.setLatitude(lat1);
loc1.setLongitude(lon1);

Location loc2 = new Location("");
loc2.setLatitude(lat2);
loc2.setLongitude(lon2);

float distance = loc1.distanceTo(loc2);

int speed=30;
float time = distance/speed;
 @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);
    }
}
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) );