获取谷歌地图中折线的长度

我的主要目标是获得两点之间的距离。我正在尝试计算折线起点/终点之间的距离(以英里为单位)。使用下面的代码,我能够绘制多段线。但是,我不知道如何获得这条折线的总距离。


private String getDirectionsUrl(LatLng origin,LatLng dest){



    // Origin of route

    String str_origin = "origin="+origin.latitude+","+origin.longitude;


    // Destination of route

    String str_dest = "destination="+dest.latitude+","+dest.longitude;


    // Sensor enabled

    String sensor = "sensor=false";


    // Building the parameters to the web service

    String parameters = str_origin+"&"+str_dest+"&"+sensor;


    // Output format

    String output = "json";


    // Building the url to the web service

    String url = "https://maps.googleapis.com/maps/api/directions/"+output+"?"+parameters;


    return url;

}


private String downloadUrl(String strUrl) throws IOException{

    String data = "";

    InputStream iStream = null;

    HttpURLConnection urlConnection = null;

    try{

        URL url = new URL(strUrl);


        // Creating an http connection to communicate with url

        urlConnection = (HttpURLConnection) url.openConnection();


        // Connecting to url

        urlConnection.connect();


        // Reading data from url

        iStream = urlConnection.getInputStream();


        BufferedReader br = new BufferedReader(new InputStreamReader(iStream));


        StringBuffer sb = new StringBuffer();


        String line = "";

        while( ( line = br.readLine()) != null){

            sb.append(line);

        }


        data = sb.toString();


        br.close();


    }catch(Exception e){

        Log.d("trDraw", e.toString());

    }finally{

        iStream.close();

        urlConnection.disconnect();

    }

    return data;

}

由于我试图用折线计算两点的距离,因此使用distanceOf(Location1, Location2);可以给我最短距离的函数对我来说是无用的。由于我使用的是折线,所以这种方法根本行不通。


呼唤远方
浏览 155回答 2
2回答

九州编程

          JSONArray array = jObject.getJSONArray("routes");            JSONObject routes = array.getJSONObject(0);            JSONArray legs = routes.getJSONArray("legs");            JSONObject steps = legs.getJSONObject(0);            JSONObject distance = steps.getJSONObject("distance");            Log.i("trJSON", "Distance String: " + distanceD.toString());            Double dist = Double.parseDouble(distanceD.getString("text").replaceAll("[^\\.0123456789]","") );            Log.i("trJSON", "Distance Double: " + dist);通过使用legs.getJSONObject(0),我可以从 获得距离或持续时间steps。因此,在获得以英里为单位的距离字符串后,我只需将其转换为Double.

PIPIONE

@grant,所以 .forEach 会帮助你:)
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java