亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

獲取谷歌地圖中折線的長度

獲取谷歌地圖中折線的長度

呼喚遠方 2021-09-15 10:40:58
我的主要目標是獲得兩點之間的距離。我正在嘗試計算折線起點/終點之間的距離(以英里為單位)。使用下面的代碼,我能夠繪制多段線。但是,我不知道如何獲得這條折線的總距離。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);可以給我最短距離的函數對我來說是無用的。由于我使用的是折線,所以這種方法根本行不通。
查看完整描述

2 回答

?
九州編程

TA貢獻1785條經驗 獲得超4個贊

          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.


查看完整回答
反對 回復 2021-09-15
?
PIPIONE

TA貢獻1829條經驗 獲得超9個贊

@grant,所以 .forEach 會幫助你:)


查看完整回答
反對 回復 2021-09-15
  • 2 回答
  • 0 關注
  • 173 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號