How to display multiple markers with different icons in Google Maps Android API v2?
I am parsing an XML file containing data for my Android application to be displayed on a map using the Android Google Maps API v2. Sample XML file format:
<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>
Now I want to display each marker on the map with different icons based on the value of the icon attribute in the marker element. My current code for adding markers through a loop:
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));
I have all the different icons for church, shops, school, etc. in my accessible folders. But I have a problem with the line:
.icon(BitmapDescriptorFactory.fromResource(R.drawable.icon)
because it R.drawable
only applies to files inside accessible folders. How can I dynamically display different icons for each marker based on the icon attribute in XML?
Any help would be greatly appreciated. :)
source to share
To get a resource:
getResources().getIdentifier(icon,"drawable", getPackageName())
The above icon
is assigning a variable to the XML attributeicon
Use this to dynamically icon
:
.icon(BitmapDescriptorFactory
.fromResource(getResources().getIdentifier(icon,"drawable", getPackageName()))
source to share
Try the following:
// Creates a marker rainbow demonstrating how to
// create default marker icons of different.
int numMarkersInRainbow[] =
{
R.drawable.arrow,
R.drawable.badge_nsw,
R.drawable.badge_qld,
R.drawable.badge_victoria
};
for (int i = 0; i < markers.getLength(); i++) {
mMap.addMarker(new MarkerOptions()
.position(position(new LatLng(lat, lon)))
.title(name)
.icon(BitmapDescriptorFactory.fromResource(numMarkersInRainbow[i])));
}
source to share