我的主要目标是获得两点之间的距离。我正在尝试计算折线起点/终点之间的距离(以英里为单位)。使用下面的代码,我能够绘制多段线。但是,我不知道如何获得这条折线的总距离。
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);可以给我最短距离的函数对我来说是无用的。由于我使用的是折线,所以这种方法根本行不通。
九州编程
PIPIONE
相关分类