0

In my Android app, I am iterating through an array of locations and adding a map marker for each item. I would like to use a different marker icon for each item in the array. There are 30 items in the locations array and 30 different icons. The icon file names are icon1.png, icon2.png, icon3.png etc.

My code is as follows:

    for (int i = 1; i < myArray.size(); i ++) {

        marker = mMap.addMarker(new MarkerOptions()
                .position(myArray.get(i))
                .title("Marker " + i)
                .icon(BitmapDescriptorFactory.fromResource(R.drawable.icon1)));

    }

How can I set R.drawable.icon1 to change for each iteration? i.e. R.drawable.icon(i)

brndn
  • 521
  • 1
  • 4
  • 18

1 Answers1

1

You just have to lookup the resource having the name, for example:

int resourceId=Resources.getSystem().getIdentifier(pDrawableName, "drawable", "android");
if(resourceId==0){
    return null;
} else {
    return Resources.getSystem().getDrawable(resourceId);
}

you can use as pDrawableName: pDrawableName = "icon"+i;

pay attention to your loop, it starts from i=1 so you are missing the "0" element (you should also have an 'icon0' drawable for that).

N Dorigatti
  • 3,482
  • 2
  • 22
  • 33
  • Thanks for your help. I will try this way as an alternate to the array method I replied about in a comment above. – brndn Jan 20 '15 at 03:41