假设您有ArrayList
和LatLng
坐标为您的标记。所以,你可以用它这样的,例如:
ArrayList<LatLng> coordinates; // your ArrayList with marker's coordinates
BitmapDescriptor icon = BitmapDescriptorFactory.fromResource(R.drawable.icon2));
...
int size = coordinates.size();
for (int i = 0; i < size; ++i) {
LatLng coordinate = coordinates.get(i);
googleMap.addMarker(new MarkerOptions()
.position(coordinate)
.icon(icon)
.title("Title" + (i + 1))
.snippet("Snippet" + (i + 1))
.anchor(0.5f, 0.5f));
}
希望它能帮助。
UPD 2015年12月15日
我更新了我的代码,代码使用的所有属性,根据你的代码。
另一个UPD 2015年12月15日
好吧,如果我们需要不同的标题,摘要,图标和位置为每个标记,我们需要存储这些特性专班,对不对?它会看起来像:
public class MyMarker {
private LatLng position;
private BitmapDescriptor icon;
private String title;
private String snippet;
public MyMarker(LatLng position, BitmapDescriptor icon, String title, String snippet) {
this.position = position;
this.icon = icon;
this.title = title;
this.snippet = snippet;
}
public LatLng getPosition() {
return position;
}
public BitmapDescriptor getIcon() {
return icon;
}
public String getTitle() {
return title;
}
public String getSnippet() {
return snippet;
}
public MarkerOptions buildGoogleMarker() {
return new MarkerOptions()
.position(this.position)
.icon(this.icon)
.title(this.title)
.snippet(this.snippet)
.anchor(0.5f, 0.5f);
}
}
然后,我想,我们以某种方式得到这样的对象的集合。并且可以使用它像这样:
ArrayList<MyMarker> markers; // your ArrayList with markers
for (MyMarker myMarker: markers) {
googleMap.addMarker(myMarker.buildGoogleMarker());
}
UPD 3
要添加新项目到ArrayList
使用.add
方法ArrayList
,e.g:
List<MyMarker> markers = new ArrayList<>();
MyMarker myMarker = new MyMarker(
new LatLng(1.123456, -2.123456),
BitmapDescriptorFactory.fromResource(R.drawable.icon2)),
"title1",
"snippet1");
markers.add(myMarker);
希望它能帮助。
可能的重复[帮助理解java'的'循环](http://stackoverflow.com/questions/5162845/help-with-understanding-java-for-loops) – muddyfish