简体   繁体   English

如何在Google Maps Android API v2中显示具有不同图标的多个标记?

[英]How to display multiple markers with different icons in Google Maps Android API v2?

I am parsing an XML file which contains the data for my Android Application that will be displayed on a map using the Google Maps Android API v2. 我正在解析一个XML文件,其中包含我的Android应用程序的数据,该数据将使用Google Maps Android API v2显示在地图上。 The sample format of the XML file is: XML文件的示例格式为:

<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 attribute value of icon in the marker element. 现在,我想根据标记元素中图标的属性值,使用不同的图标在地图上显示每个标记。 My current code for adding markers through looping is: 我目前通过循环添加标记的代码是:

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, shopping, school, etc. in my drawable folders. 在我的drawable文件夹中,我有教堂,购物,学校等所有不同的图标。 But I am having a problem with the line: 但我遇到了问题:

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

because R.drawable only pertains to the files inside the drawable folders. 因为R.drawable只适用于drawable文件夹中的文件。 How can I be able to display different icons for each marker based on the icon attribute on the XML dynamically? 如何能够动态地根据XML上的图标属性为每个标记显示不同的图标?

Any help would be greatly appreciated. 任何帮助将不胜感激。 :) :)

To get the resource: 获取资源:

getResources().getIdentifier(icon,"drawable", getPackageName())

The icon used above is assigned variable for the XML icon attribute 上面使用的icon是XML icon属性的变量

Use this to get the icon dynamically: 使用它来动态获取icon

.icon(BitmapDescriptorFactory
   .fromResource(getResources().getIdentifier(icon,"drawable", getPackageName()))

Try out as below: 试试如下:

// 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])));
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在 Google Maps API v2 Android 中添加多个标记 - Adding multiple markers in Google Maps API v2 Android 在适用于Android的Google Maps API v2中使用多个标记 - use multiple markers in google maps api v2 for android Android Google Maps API v2上带有文字的多个标记 - Multiple markers with text on Android Google Maps API v2 如何在不同的信息窗口中为Google Maps api v2 for Android中的不同标记添加不同的图像 - how to add different images in different information window for different markers in google maps api v2 for android 如何限制Android Google Maps API v2中标记的数量? - how to limit the number of the markers in Android Google Maps API v2? Android Java 谷歌地图 api v2 :: 如何检查标记在谷歌地图上的圆圈内 android api v2 - Android Java google maps api v2:: how check with markers are within a circle on google maps android api v2 如何在Google Maps for Android API v2上显示我的位置 - How to display my location on Google Maps for Android API v2 Google Maps Api v2-具有自己的信息窗口的多个标记 - Google Maps Api v2 - Multiple markers with their own infowindow 在Google Maps Api v2中添加多个标记 - Adding multiple markers in Google Maps Api v2 Android Google Maps API v2在地图上放置标记 - Android Google Maps API v2 placing markers on map
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM