0

I have a file with about 1700 markers that I am trying to load onto a gmap v2. On my galaxy nexus running 4.2.2 it loads no problem, but some folks with 4.0.x and 4.1.x are not having the same results. They get the map, but no points or the app crashes after about 30 seconds. I am loading a local file...

Here is my method:

public void BuildMap() {

        FileInputStream fXmlFile;
        markerInfo = new HashMap<Marker, MapMarkers>();
        try {
            fXmlFile = new FileInputStream(
                    "/storage/emulated/0/snoteldata/kml/snotelwithlabels.kml");

            XmlDom xml = new XmlDom(fXmlFile);
            List<XmlDom> locations = xml.tags("Placemark");
            String Name, Description, Lat, Lon;
            markerInfo = new HashMap<Marker, MapMarkers>();
            for (XmlDom location : locations) {
                MapMarkers marks = new MapMarkers();
                Name = location.tag("name").text();
                Description = location.tag("description").text();

                Lat = location.tag("latitude").text();
                Lon = location.tag("longitude").text();

                la = Float.parseFloat(Lat);
                lo = Float.parseFloat(Lon);

                marks.setTitle(Name);
                marks.setDesc(Description);

                Marker m = map.addMarker(new MarkerOptions()
                        .position(new LatLng(la, lo))
                        .title(marks.getTitle())
                        .icon(BitmapDescriptorFactory
                                .fromResource(R.drawable.snotel_marker)));

                markerInfo.put(m, marks);

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

                        MapMarkers markInfo = markerInfo.get(marker);

                        Intent i = new Intent(MainActivity.this,
                                MarkerInformation.class);
                        i.putExtra("name", markInfo.getTitle()).putExtra(
                                "description", markInfo.getDesc());
                        i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                        startActivity(i);

                    }

                });
            }

        } catch (SAXException e) {
            // TODO Auto-generated catch block
            Log.e("SAXException", e.getMessage());
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            Log.e("FileNotFoundException", e.getMessage());
        }
    }

I have tried putting this in a AsyncTask, but get the Not on Main Thread error each time... so I am not sure how to run it in the background to keep it loading for folks until the parsing has completely happened.

Why does this show for my Gnex and a Nexus 7 tablet, but not for 4.0.x etc??? How can I figure out where the issue is on others devices?

jasonflaherty
  • 1,924
  • 7
  • 40
  • 82

2 Answers2

2

There are two problems with your code.

First, you are reading file on the main thread. Do this part in background, e.g. using AsyncTask which returns a list of MarkerOptions. Iterate over returned list in onPostExecute to add those to the map.

Second issue might be the amount of Markers. There are a few ways to handle this. Check this answer: Add markers dynamically on Google Maps v2 for Android

Community
  • 1
  • 1
MaciejGórski
  • 22,187
  • 7
  • 70
  • 94
  • Thanks for the info. I have checked out your library... I think I'll give it a try here. :) – jasonflaherty Sep 28 '13 at 01:28
  • Ok, I am confused a bit. What do I return? Or Better, How do I return the Marker in Async? protected Marker[] doInBackground(Marker... m) {}? – jasonflaherty Sep 28 '13 at 01:46
  • I got it working better with the Async Method... Thanks for pointing out the onPostExecute. – jasonflaherty Sep 28 '13 at 20:36
  • @jasonflaherty hi, i have 300 markers, it takes about 5 seconds to load them all even I use asynctask, how did you speed it up, can you please give me some hints? thanks in advance – Haifeng Zhang Oct 01 '14 at 21:57
0

Do this way

public void BuildMap() {

        final Handler mHandler = new Handler();

        new Thread(new Runnable() {

            @Override
            public void run() {
                FileInputStream fXmlFile;
                markerInfo = new HashMap<Marker, MapMarkers>();
                try {
                    fXmlFile = new FileInputStream("/storage/emulated/0/snoteldata/kml/snotelwithlabels.kml");

                    XmlDom xml = new XmlDom(fXmlFile);
                    List<XmlDom> locations = xml.tags("Placemark");
                    String Name, Description, Lat, Lon;
                    markerInfo = new HashMap<Marker, MapMarkers>();
                    for (XmlDom location : locations) {
                        final MapMarkers marks = new MapMarkers();
                        Name = location.tag("name").text();
                        Description = location.tag("description").text();

                        Lat = location.tag("latitude").text();
                        Lon = location.tag("longitude").text();

                        la = Float.parseFloat(Lat);
                        lo = Float.parseFloat(Lon);

                        marks.setTitle(Name);
                        marks.setDesc(Description);

                        mHandler.post(new Runnable() {

                            @Override
                            public void run() {

                                Marker m = map.addMarker(new MarkerOptions().position(new LatLng(la, lo)).title(marks.getTitle())
                                        .icon(BitmapDescriptorFactory.fromResource(R.drawable.snotel_marker)));

                                markerInfo.put(m, marks);

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

                                        MapMarkers markInfo = markerInfo.get(marker);

                                        Intent i = new Intent(MainActivity.this, MarkerInformation.class);
                                        i.putExtra("name", markInfo.getTitle()).putExtra("description", markInfo.getDesc());
                                        i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                                        startActivity(i);

                                    }

                                });
                            }
                        });
                    }

                } catch (SAXException e) {
                    // TODO Auto-generated catch block
                    Log.e("SAXException", e.getMessage());
                } catch (FileNotFoundException e) {
                    // TODO Auto-generated catch block
                    Log.e("FileNotFoundException", e.getMessage());
                }
            }
        }).start();

    }
Biraj Zalavadia
  • 28,348
  • 10
  • 61
  • 77