如何在谷歌地图上显示源和目的地的所有可能的路线在android& xamarin android?

时间:2017-04-02 07:30:32

标签: android google-maps xamarin.android

我在xamarin android工作,我试图在谷歌地图上显示所有可能的路线(如谷歌地图android)2个地理位置,我可以使用谷歌方向api显示单一路线,所以任何人都可以帮我解决这个问题..

1 个答案:

答案 0 :(得分:1)

尝试将替代设置为true。根据{{​​3}}:

  

替代方案 - 如果设置为 true ,则指定路线服务可能会在响应中提供多个路径替代方案。请注意,提供路由选择可能会增加服务器的响应时间。

以下是处理document的示例代码:

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(4);
      lineOptions.color(Color.CYAN);


  tvDistanceDuration.setText("Distance:"+distance + ", Duration:"+duration);

  // Drawing polyline in the Google Map for the i-th route
  map.addPolyline(lineOptions);
    }
  }
}

希望这有帮助。