5

我有一个

places = ArrayList<ArrayList<LatLng>>

我将 LatLng 点添加到内部 ArrayList 中,然后我有一个 for 循环,该循环循环并将折线添加到地图中。除了它不这样做...如何将折线动态添加到 GoogleMap?我检查了地方是否有人居住,确实有人居住。

提前致谢。

ArrayList<Polyline> pl = new ArrayList<Polyline>();                 
for(int i =0; i<places.size(); i++){
        pl.add(mMap.addPolyline(new PolylineOptions().addAll(places.get(i))));
        Log.e("size of places", "size of places is " + places.size());
    }
4

3 回答 3

16

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

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);
于 2014-09-22T16:19:32.947 回答
8

一旦您的列表中有纬度和经度列表,您可以使用下面的内容来绘制线条。

List<LatLng> points = decodePoly(_path); // list of latlng
for (int i = 0; i < points.size() - 1; i++) {
  LatLng src = points.get(i);
  LatLng dest = points.get(i + 1);

  // mMap is the Map Object
  Polyline line = mMap.addPolyline(
    new PolylineOptions().add(
      new LatLng(src.latitude, src.longitude),
      new LatLng(dest.latitude,dest.longitude)
    ).width(2).color(Color.BLUE).geodesic(true)
  );
}

以上在我的应用程序中对我有用

于 2013-05-01T12:30:07.167 回答
0

您拥有的places变量是什么,因为位置需要是该行中的所有位置,而不仅仅是 1 个点。

因此,假设地点就是ArrayList<LatLng>这样做places.get(i),您只给出一分,而不是整个积分列表;

于 2013-05-01T01:48:14.990 回答