2015-12-14 105 views
-3

添加在谷歌地图API的Android V2 260个标记位置我使用这个代码插入谷歌地图上的多个标记:使用ArrayList的

@Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
... 

Marker marker1 = map.addMarker(new MarkerOptions() 
       .position(new LatLng(1.123456, -2.123456)) 
       .title("Title1") 
       .snippet("Snippet1") 
       .icon(BitmapDescriptorFactory.fromResource(R.drawable.icon1))); 


Marker marker2 = map.addMarker(new MarkerOptions() 
       .position(new LatLng(3.123456,-4.123456))    
       .title("Title2") 
       .snippet("Snippet2") 
       .icon(BitmapDescriptorFactory.fromResource(R.drawable.icon2))); 

但我有一个总的260个标记添加..每一个标记与具体(位置,标题,片段,图标)。

有人可以让我知道如何使用arraylist和循环来做到这一点?

谢谢。

+0

可能的重复[帮助理解java'的'循环](http://stackoverflow.com/questions/5162845/help-with-understanding-java-for-loops) – muddyfish

回答

1

假设您有ArrayListLatLng坐标为您的标记。所以,你可以用它这样的,例如:

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

希望它能帮助。

+0

谢谢,但我需要tu使用全部项目(位置+标题+片段+图标)。 – Gama

+0

我需要的是在地图上设置多个标记,但每个标记都有不同的位置(标题,片段,图标)。 – Gama

+0

请看新代码片段=) –