【发布时间】:2020-07-01 13:19:14
【问题描述】:
我正在尝试在我的应用中实现一项功能,该功能使用 Google Maps API Directions 在地图中绘制路径(从 A 到 B)。由于某种原因,应用程序似乎甚至无法发出请求。总是取result.size()<1的情况
protected void onPostExecute(List<List<HashMap<String, String>>> result) {
ArrayList<LatLng> points = null;
PolylineOptions lineOptions = null;
MarkerOptions markerOptions = new MarkerOptions();
String distance = "";
String duration = "";
dialogz.dismiss();
if(result.size()<1){
Toast.makeText(getBaseContext(), "Road name cannot be identified, Please choose a nearest location", Toast.LENGTH_SHORT).show();
return;
}
for(int i=0;i<result.size();i++){
points = new ArrayList<LatLng>();
lineOptions = new PolylineOptions();
List<HashMap<String, String>> path = result.get(i);
for(int j=0;j<path.size();j++){
HashMap<String,String> point = path.get(j);
if(j==0){
distance = (String)point.get("distance");
continue;
}else if(j==1){
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(18);
lineOptions.color(Color.parseColor("#2E2E2E"));
}
---and this is the code of getDirectionUrl----
private void getDirectionsUrl(LatLng picklatlng,LatLng droplatlng){
if(picklatlng!=null && droplatlng!=null) {
String str_origin = "origin=" + picklatlng.latitude + "," + picklatlng.longitude;
String str_dest = "destination=" + droplatlng.latitude + "," + droplatlng.longitude;
String sensor = "sensor=false";
String parameters = str_origin + "&" + str_dest + "&" + sensor;
String output = "json";
String url = "https://maps.googleapis.com/maps/api/directions/" + output + "?" + parameters;
DownloadTask downloadTask = new DownloadTask();
downloadTask.execute(url);
}
}
【问题讨论】:
标签: java android google-maps google-maps-api-2 google-directions-api