0

我有 4 个值试图从 xml 文件放入哈希图中。我可以解析 xml 没问题,但想通过纬度/经度获取数据以将点放在地图上。我正在尝试从 pullparser 填充哈希图,但失败了。我遇到了关于在哪里填充我认为的哈希图的问题。

这是我的哈希图:

Map<Integer, MapMarkers> DataLocations = new HashMap<Integer, MapMarkers>();

这是我的 MapMarkers 类:

public class MapMarkers {
    private String lon;
    private String lat;
    private String title;
    private String desc;

    public MapMarkers() {
        super();
    }

    public MapMarkers(String lon, String lat, String title, String desc) {
        super();
        this.lon = lon;
        this.lat = lat;
        this.title = title;
        this.desc = desc;
    }

    public String getLon() {
        return lon;
    }

    public void setLon(String lon) {
        this.lon = lon;
    }

    public String getLat() {
        return lat;
    }

    public void setLat(String lat) {
        this.lat = lat;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getDesc() {
        return desc;
    }

    public void setDesc(String desc) {
        this.desc = desc;
    }

}

这是我的 pullparser 异步运行,因为 xml 很大:

公共类 BuildMapInfoTask 扩展 AsyncTask { String sname = null; 字符串 sdesc = null; 字符串 slat = null; 字符串 slon = null;

    @Override
    protected String doInBackground(String... params) {
        try {
            XmlPullParserFactory factory = XmlPullParserFactory
                    .newInstance();
            factory.setValidating(false);
            XmlPullParser myxml = factory.newPullParser();
            FileInputStream fs = new FileInputStream(
                    "/storage/emulated/0/snoteldata/kml/snotelwithlabels.kml");
            myxml.setInput(fs, null);
            int eventType = myxml.getEventType();
            int uniquekey = 0;
            marks = new MapMarkers(slat, slon, sname, sdesc);
            while (eventType != XmlPullParser.END_DOCUMENT) {
                if (eventType == XmlPullParser.START_TAG) {
                    String tag = myxml.getName();
                    if ("name".equals(tag)) {
                        sname = myxml.nextText().trim();
                        marks.setTitle(sname);
                    } else if ("description".equals(tag)) {
                        sdesc = myxml.nextText().trim();
                        marks.setDesc(sdesc);
                    } else if ("longitude".equals(tag)) {
                        slon = myxml.nextText().trim();
                        marks.setLon(slon);
                    } else if ("latitude".equals(tag)) {
                        slat = myxml.nextText().trim();
                        marks.setLat(slat);
                    }
                    Log.v("ET",uniquekey+"");
                    DataLocations.put(uniquekey, marks);
                    uniquekey++;
                }
                eventType = myxml.next();
            }
        } catch (XmlPullParserException e) {
            Log.e("PP Error", e.getMessage());
        } catch (IOException e) {
            Log.e("PP IOException", e.getMessage());
        }
        return null;

    }

我创建了 uniquekey 作为 KEY,然后在 Values 区域中加载 lat,lon,title,desc ...我的值没有被填充。我需要在 PullParser 中的哪里使用 DataLocations.put(...) 来填充 DataLocations,如果我目前已经填充了它,我该如何提取它?我错了吗?

4

1 回答 1

0

我没有在每个循环上创建一个新的标记对象。这最终为我工作:

 try {
            markerInfo = new HashMap<Marker, MapMarkers>();
            File fXmlFile = new File(
                    "/storage/emulated/0/data/kml/labels.kml");

            DocumentBuilderFactory dbFactory = DocumentBuilderFactory
                    .newInstance();
            DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
            Document doc = dBuilder.parse(fXmlFile);

            doc.getDocumentElement().normalize();

            NodeList nList = doc.getElementsByTagName("Placemark");

            for (int temp = 0; temp < nList.getLength(); temp++) {
                // must make new each time around...
                MapMarkers marks = new MapMarkers();

                Node nNode = nList.item(temp);

                if (nNode.getNodeType() == Node.ELEMENT_NODE) {

                    Element eElement = (Element) nNode;

                    String name = eElement.getElementsByTagName("name").item(0)
                            .getTextContent();

                    String desc = eElement.getElementsByTagName("description")
                            .item(0).getTextContent();

                    String lon = eElement.getElementsByTagName("longitude")
                            .item(0).getTextContent();

                    String lat = eElement.getElementsByTagName("latitude")
                            .item(0).getTextContent();

                    lon = lon.trim();
                    lat = lat.trim();

                    double lati = Double.parseDouble(lat);
                    double lngi = Double.parseDouble(lon);

                    marks.setTitle(name);
                    marks.setDesc(desc);

                    Marker m = map.addMarker(new MarkerOptions()
                            .position(new LatLng(lati, lngi))
                            .title(marks.getTitle())
                            .icon(BitmapDescriptorFactory
                                    .fromResource(R.drawable.youmarker)));

                    markerInfo.put(m, marks);

                    map.setOnInfoWindowClickListener(new OnInfoWindowClickListener() {
                        @Override
                        public void onInfoWindowClick(Marker marker) {

                            MapMarkers eventInfo = markerInfo.get(marker);

                            msg(Html.fromHtml(eventInfo.getDesc()));

                            // Log.e("ID", marker.getId());
                            // Log.v("EI", eventInfo.getTitle() + " ----- "
                            // + eventInfo.getDesc());

                        }

                    });

                }

            }

        } catch (Exception e) {
            Log.e("Data Buildmap Error", e.getMessage());
        }
于 2013-09-15T21:36:46.843 回答