【问题标题】:Adding multiple points in map using polyline and arraylist使用折线和数组列表在地图中添加多个点
【发布时间】:2019-12-31 18:56:12
【问题描述】:

如何在谷歌地图中为多个经纬度坐标绘制折线。我需要动态绘制至少 20 组经纬度的折线。

【问题讨论】:

    标签: android android-maps-v2


    【解决方案1】:

    使用折线和数组列表在地图中添加多个点

    ArrayList<LatLng> coordList = new ArrayList<LatLng>();
    
    // Adding points to ArrayList
    coordList.add(new LatLng(0, 0);
    coordList.add(new LatLng(1, 1);
    coordList.add(new LatLng(2, 2);
    // etc...
    
    // Find map fragment. This line work only with support library
    GoogleMap gMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap();
    
    PolylineOptions polylineOptions = new PolylineOptions();
    
    // Create polyline options with existing LatLng ArrayList
    polylineOptions.addAll(coordList);
    polylineOptions
     .width(5)
     .color(Color.RED);
    
    // Adding multiple points in map using polyline and arraylist
    gMap.addPolyline(polylineOptions);
    

    【讨论】:

      【解决方案2】:

      来自Android documentation的示例:

         GoogleMap map;
         // ... get a map.
         // Add a thin red line from London to New York.
         Polyline line = map.addPolyline(new PolylineOptions()
           .add(new LatLng(51.5, -0.1), new LatLng(40.7, -74.0))
           .width(5)
           .color(Color.RED));
      

      只需拨打.add 多次即可。

      【讨论】:

      • 我需要通过arraylist添加经纬度。
      • 所以只需遍历arraylist的使用.addAll方法。
      • 但我有两个字段纬度和经度。
      • 那么具体的问题是什么?你不知道如何获取字段吗? for (Coords c: data) { polylineOptions.add(new LatLng(c.latitude, c.longitude)) }
      • 我通过 json 后端获得多个点,因此我需要将其存储在 arraylist 中并在地图上为至少 20 组坐标绘制一条折线。
      【解决方案3】:

      只需创建一个函数来检索 JSON 多段线,如下所示:

      private ArrayList<LatLng> getPolylines(String jsonStr) {
          // file exists, it is the first boot
          if (jsonStr != null) {
              // linea init
              LatLng polyline;
      
              // array list of lines init
              ArrayList<LatLng> polylines = new ArrayList<LatLng>();
      
              // get json array
              JSONArray jsonArray = JSON.getJSONArray(jsonStr, "polytag");
      
              for (int i = 0; i < jsonArray.length(); i++) {
      
                  JSONObject jsonPolyline;
      
                  try {
                      jsonPolyline = jsonArray.getJSONObject(i);
      
                      polyline = new LatLng(Double.valueOf(jsonPolyline.getString("lat")),
                              Double.valueOf(jsonPolyline.getString("lon")));
      
                      polylines.add(polyline);
      
                  } catch (JSONException e) {
                      Log.d(TAG, "JSONException reading polylines: " + e.getMessage());
                  } catch (Exception e) {
                      Log.d(TAG, "Exception reading polylines: " + e.getMessage());
                  }
              }
      
              return polylines;
          }
      
          return null;
      }
      

      然后获取这个 ArrayList 并以这种方式填充它:

      ArrayList<LatLng> polylines = getPolylines(linesJsonStr);
      map.addPolyline(new PolylineOptions().addAll(polylines).width(2.0f).color(Color.RED));
      

      希望对你有帮助!

      【讨论】:

        【解决方案4】:

        您可以遍历点数组,并为每个坐标添加一个新的 LatLng 到折线选项,然后在循环之后将折线选项添加到折线。

         val from = LatLng(it.data[0].lat, it.data[0].lng)
                                    val to = LatLng(it.data.last().lat, it.data.last().lng)
                                    map.addMarker(MarkerOptions().position(from).title("pickup location"))
                                    map.addMarker(MarkerOptions().position(to).title("destination location"))
                                    map.animateCamera(CameraUpdateFactory.newLatLngZoom(from, 13f)) 
                                    var polylineOptions = PolylineOptions()
                                    for (i in it.data){
                                        polylineOptions.add(LatLng(i.lat, i.lng))
                                    }
        
                                    polylineOptions.width(5f).color(Color.RED)
                                    map.addPolyline(
                                        polylineOptions
                                    )
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2013-07-25
          • 1970-01-01
          • 2020-09-21
          • 1970-01-01
          • 2015-11-13
          • 1970-01-01
          相关资源
          最近更新 更多