在地图上绘制多个点之间的路线

7

我是android的新手。我希望可以在多个标记之间绘制路线。我已经从服务器获取到了纬度、经度和日期时间。现在我想显示这些点之间的路线。我已经将它们存储在ArrayList中。以下是我在异步任务doInBackground()中获取这些点的方法。

  newLatt= new ArrayList<String>();
  newLongg= new ArrayList<String>();
  newdatTime= new ArrayList<String>();

  JSONArray arr = new JSONArray(strServerResponse);
  for (int i = 0; i < arr.length(); i++) {
        JSONObject jsonObj1 = arr.getJSONObject(i);
        String status = jsonObj1.optString("status");
        if (status!="false"){
          Pojo pojo = new Pojo();
          String latitude = jsonObj1.optString("Latitude");
          String longitude = jsonObj1.optString("Longitude");
          String date_time = jsonObj1.optString("date_time");
          newLatt.add(latitude);
          newLongg.add(longitude);
          newdatTime.add(date_time);
       }else {
              Handler handler = new Handler(Looper.getMainLooper());
              handler.post(new Runnable() {
              @Override
              public void run() {
                   AlertDialog alertDialog = new AlertDialog.Builder(
                   MapActivity.this).create();
                   alertDialog.setMessage("Locations Not Available");
                   alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
                   public void onClick(DialogInterface dialog, int which) {
                   }
                   });
                    alertDialog.show();
                  }
               }
       );
     }

在postExecute()方法中,我正在显示标记。

        SupportMapFragment supportMapFragment = (SupportMapFragment) getSupportFragmentManager()
                .findFragmentById(R.id.map);
        map = supportMapFragment.getMap();
        map.setMyLocationEnabled(true);
        if (newLatt.size()>0){
            for (int i = 0; i < newLatt.size(); i++) {
                Double lati = Double.parseDouble(newLatt.get(i));
                Double longi = Double.parseDouble(newLongg.get(i));
                String dattme = newdatTime.get(i);
                dest = new LatLng(lati, longi);
                if (map != null) {
                    MarkerOptions markerOptions = new MarkerOptions();
                    markerOptions.position(dest);
                    map.moveCamera(CameraUpdateFactory.newLatLng(dest));
                    map.animateCamera(CameraUpdateFactory.zoomTo(15));

                    markerOptions.icon(BitmapDescriptorFactory
                            .defaultMarker(BitmapDescriptorFactory.HUE_RED));
                    markerOptions.title("" + dattme);
                    map.addMarker(markerOptions);
                    map.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener() {
                        @Override
                        public boolean onMarkerClick(Marker marker) {
                            marker.showInfoWindow();
                            return false;
                        }
                    });

更新

 PolylineOptions rectOptions = new PolylineOptions();
        //this is the color of route
        rectOptions.color(Color.argb(255, 85, 166, 27));
        LatLng startLatLng = null;
        LatLng endLatLng = null;
        SupportMapFragment supportMapFragment = (SupportMapFragment) getSupportFragmentManager()
                .findFragmentById(R.id.map);
        map = supportMapFragment.getMap();
        map.setMyLocationEnabled(true);
        if (newLatt.size()>0){
            for (int i = 0; i < newLatt.size(); i++) {
                Double lati = Double.parseDouble(newLatt.get(i));
                Double longi = Double.parseDouble(newLongg.get(i));
                String dattme = newdatTime.get(i);
                dest = new LatLng(lati, longi);
                if (map != null) {
                    MarkerOptions markerOptions = new MarkerOptions();
                    markerOptions.position(dest);
                    map.moveCamera(CameraUpdateFactory.newLatLng(dest));
                    map.animateCamera(CameraUpdateFactory.zoomTo(15));

                    markerOptions.icon(BitmapDescriptorFactory
                            .defaultMarker(BitmapDescriptorFactory.HUE_RED));
                    markerOptions.title("" + dattme);
                    map.addMarker(markerOptions);
                    map.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener() {
                        @Override
                        public boolean onMarkerClick(Marker marker) {
                            marker.showInfoWindow();
                            return false;
                        }
                    });

                        LatLng latlng = new LatLng(lati,
                                longi);
                        if (i == 0) {
                            startLatLng = latlng;
                        }
                        if (i == newLatt.size() - 1) {
                            endLatLng = latlng;
                        }
                        rectOptions.add(latlng);
                    String url = getDirectionsUrl(startLatLng, endLatLng);
                    DownloadTask downloadTask = new DownloadTask();
                    downloadTask.execute(url);
                }

            }
            map.addPolyline(rectOptions);

获取方向:

    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;
}
2个回答

2
在postExecute中添加以下代码以在地图上添加ploylines。
                PolylineOptions rectOptions = new PolylineOptions();
                //this is the color of route
                rectOptions.color(Color.argb(255, 85, 166, 27));

                LatLng startLatLng = null;
                LatLng endLatLng = null;
                for (int i = 0; i < newLatt.size(); i++) {
                Double lati = Double.parseDouble(newLatt.get(i));
                Double longi = Double.parseDouble(newLongg.get(i));
                    LatLng latlng = new LatLng(lati,
                            longi);
                    if (i == 0) {
                        startLatLng = latlng;
                    }
                    if (i == jArr.length() - 1) {
                        endLatLng = latlng;
                    }
                    rectOptions.add(latlng);
                }
                map.addPolyline(rectOptions);

愉快编码...


1
记录 newLatt 的大小。你得到一条直线,因为你可能只有两个经纬度。 - A Raj
抱歉,那是我的代码片段。请将 jArr.length() 替换为 newLatt.size()。 - A Raj
应该可以正常工作。如果您得到了预期的结果,请点赞。 - A Raj
我在getDirectionsurl中的String str_dest = "destination=" + dest.latitude + "," + dest.longitude;处遇到了空指针异常。 - user5333329
让我们在聊天中继续这个讨论:http://chat.stackoverflow.com/rooms/91739/discussion-between-a-raj-and-pri。 - A Raj

0
Android does not provide embedded direction service in google map api. To draw route between points you must use google direction services REST API .
You can get complete code and description from http://wptrafficanalyzer.in/blog/drawing-driving-route-directions-between-two-locations-using-google-directions-in-google-map-android-api-v2/


ArrayList<LatLng> points = null;
            PolylineOptions lineOptions = null;
            MarkerOptions markerOptions = new MarkerOptions();

            // 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);

                    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(2);
                lineOptions.color(Color.RED);
            }

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

是的,我知道这个。但是我有一个包含纬度和经度的ArrayList,如何访问它们以显示路线? - user5333329
你的列表有哈希表吗? - USKMobility

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接