如何显示多个标记与谷歌地图Android的API V2不同的图标? [英] How to display multiple markers with different icons in Google Maps Android API v2?

本文介绍了如何显示多个标记与谷歌地图Android的API V2不同的图标?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我解析其中包含的数据为我将要使用的谷歌地图Android的API V2的地图显示的Andr​​oid应用程序的XML文件。 XML文件的示例格式是:

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. The sample format of the XML file is:

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

现在,我要显示基于图标的标记元素的属性值不同的图标在地图上的每个标记。我现在的code,通过循环添加标记是:

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. But I am having a problem with the line:

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

由于 R.drawable 只适用于绘制的文件夹里面的文件。我如何能够显示不同的图标为基础的动态的XML图标属性,每个标记?

because R.drawable only pertains to the files inside the drawable folders. How can I be able to display different icons for each marker based on the icon attribute on the XML dynamically?

任何帮助将是很大的AP preciated。 :)

Any help would be greatly appreciated. :)

推荐答案

要获取资源:

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

以上图标用于指定变量中的XML 图标属性

The icon used above is assigned variable for the XML icon attribute

使用此获得图标动态:

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

这篇关于如何显示多个标记与谷歌地图Android的API V2不同的图标?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆