Question

I have 4 values I am trying to put into a hashmap from an xml file. I can parse the xml no problem, but would like to get the data to put points on a map via lat/lon. I am Trying to populate a hashmap from the pullparser, but am failing. I am running into issues on where to populate the hashmap I think.

Here is my hashmap:

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

Here is my MapMarkers class:

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

}

Here is my pullparser running in async since the xml is large:

public class BuildMapInfoTask extends AsyncTask { String sname = null; String sdesc = null; String slat = null; String 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;

    }

I created uniquekey as the KEY and then load the lat,lon,title,desc in the Values area... My values are not being populated. Where do I need to DataLocations.put(...) within the PullParser to make this populate DataLocations and how do I pull this if I have it populated currently? Do I have this all wrong?

Was it helpful?

Solution

I didn't create a new marker object on each loop through. This ended up working for me:

 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());
        }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top