简体   繁体   English

如何从arraylist动态添加折线

[英]How to dynamically add polylines from an arraylist

I have an 我有一个

places = ArrayList<ArrayList<LatLng>>

I am adding LatLng points into the inner ArrayList and then I have a for loop that loops and adds polylines to the map.. except it doesnt do that... How can I add polylines dynamically to the GoogleMap? 我将LatLng点添加到内部ArrayList中,然后我有一个for循环,循环并向地图添加折线......除了它不这样做...如何动态地将折线添加到GoogleMap? I checked whether or not places was being populated and it is. 我检查了地方是否正在填充,它是。

Thanks in advance. 提前致谢。

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());
    }

Adding multiple points in map using polyline and arraylist 使用折线和arraylist在地图中添加多个点

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

Once you have list of latitude an longitudes in your List, you can use the below to draw lines. 一旦列表中有经度纬度列表,就可以使用下面的线条绘制线条。

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

the above worked for me in my application 以上在我的申请中为我工作

What is the places variable you have because places need to be all the locations in the line and not just 1 point. 您拥有的places变量是什么,因为地点需要是该行中的所有位置而不仅仅是1个点。

So assuming places is ArrayList<LatLng> then by doing places.get(i) you are only giving one point and not the whole list of points; 假设places是ArrayList<LatLng>那么通过执行places.get(i)你只给出一个点,而不是整个点列表;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM