Comment afficher des marqueurs multiples avec différentes icônes dans Google Maps API Android v2?

Je suis l'analyse d'un fichier XML qui contient les données de mon Application Android qui va être affiché sur une carte à l'aide de Google Maps API Android v2. Le format de l'échantillon, le fichier XML est:

<markers>
  <marker name="San Pedro Cathedral"
          address="Davao City"
          lat="7.0647222"
          long="125.6091667"
          icon="church"/>
  <marker name="SM Lanang Premier"
          address="Davao City"
          lat="7.0983333"
          long="125.6308333"
          icon="shopping"/>
  <marker name="Davao Central High School"
          address="Davao City"
          lat="7.0769444"
          long="125.6136111"
          icon="school"/>
</markers>

Maintenant, je veux que l'affichage de chaque marqueur sur la carte avec les icônes différentes basé sur la valeur de l'attribut de l'icône dans l'élément de marqueur. Mon code actuel pour l'ajout de marqueurs à travers la boucle est:

DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document doc = builder.parse("http://dds.orgfree.com/DDS/landmarks_genxml.php");

NodeList markers = doc.getElementsByTagName("marker");

for (int i = 0; i < markers.getLength(); i++) {
    Element item = (Element) markers.item(i);
    String name = item.getAttribute("name");
    String address = item.getAttribute("address");
    String stringLat = item.getAttribute("lat");
    String stringLong = item.getAttribute("long");
    String icon = item.getAttribute("icon"); //assigned variable for the XML icon attribute
    Double lat = Double.valueOf(stringLat);
    Double lon = Double.valueOf(stringLong);
    map = ((MapFragment) getFragmentManager().findFragmentById(
            R.id.map)).getMap();

    map.addMarker(new MarkerOptions()
            .position(new LatLng(lat, lon))
            .title(name)
            .snippet(address)
            //I have a coding problem here...
            .icon(BitmapDescriptorFactory
                    .fromResource(R.drawable.icon)));

    //Move the camera instantly to City Hall with a zoom of 15.
    map.moveCamera(CameraUpdateFactory.newLatLngZoom(CITYHALL, 15));

J'ai toutes les différentes icônes de l'église, magasins, école, etc. dans mon drawable dossiers. Mais j'ai un problème avec la ligne:

.icon(BitmapDescriptorFactory.fromResource(R.drawable.icon)

parce que R.drawable concernent uniquement les fichiers à l'intérieur de la drawable dossiers. Comment puis-je être capable d'afficher des icônes différentes pour chaque indicateur basé sur l'icône de l'attribut XML dynamiquement?

Toute aide serait grandement appréciée. 🙂

OriginalL'auteur sharic19 | 2013-01-30