android-maps-utils: App freezes when loading KML layer

I am trying to add KML layer on to the maps. It was working okay with just a ms of freeze when i try to load the small size KML file. But some files are large with size more than 1Mb - 10Mb… in these all cases app is getting frozen for some time while the layer is adding to the map.

Below is the Asynctask class i am using to read the kml file from local storage path and adding it to maps.

I tried this in many ways using handlers, threads, asynctasks… as i cannot update the UI in background thread… this is causing my app to freeze for some time.

Eventhough it freezes I wouldn’t mind but in some device while the app is frozen if the user interacts with the app then app is getting crashed. In some devices it is displaying “wait” dialog.

Please help me out.

` private static class AddKMLLayerToMaps extends AsyncTask<String, String, KmlLayer> {

    //added weakreference to avoid memory leaks
    private WeakReference<CoverageFragment> weakReferencedFragment;

    AddKMLLayerToMaps(CoverageFragment reference) {

        this.weakReferencedFragment = new WeakReference<>(reference);

        weakReferencedFragment.get().showLoading();
    }

    @Override
    protected KmlLayer doInBackground(String... strings) {

        try {

            TraceUtils.logE("Coverage kml Path", strings[0]);

            FileInputStream fileInputStream = new FileInputStream(strings[0]);

            CoverageFragment fragment = weakReferencedFragment.get();
            if (fragment == null || fragment.getActivity() == null || fragment.getActivity().isFinishing()) {
                return null;
            }

            KmlLayer kmlLayer = new KmlLayer(fragment.mMap, fileInputStream, getApplicationContext());

            fileInputStream.close();

            File file = new File(strings[0]);
            if (file.exists()) file.delete();

            return kmlLayer;

        } catch (Exception e) {

            weakReferencedFragment.get().hideLoading();

            TraceUtils.logException(e);

        }

        return null;
    }

    @Override
    protected void onPostExecute(KmlLayer kmlLayer) {

        super.onPostExecute(kmlLayer);

        if (kmlLayer != null) {

            //add the KML layer to map using the UI thread
            weakReferencedFragment.get().mActivity.runOnUiThread(() -> {
                try {

                    kmlLayer.addLayerToMap();

                } catch (Exception e) {

                    weakReferencedFragment.get().hideLoading();

                    TraceUtils.logException(e);

                }
            });

            //zoom to the center of KML layer containers
            new MoveMapToKMLLayer(weakReferencedFragment.get()).execute(kmlLayer);
        }
    }

}`

About this issue

  • Original URL
  • State: closed
  • Created 5 years ago
  • Comments: 27 (9 by maintainers)

Most upvoted comments

@barbeau yes, with the v2 SDK it takes 10-15 seconds to add the polygons and display on the map. So this is a regression in v3. I created a bug report.

I profiled with the maps v3 beta (https://github.com/googlemaps/android-maps-utils/pull/608) and actually couldn’t get the KML to display on the map at all. The app hung for a few minutes with the main thread blocked and eventually crashed.

2020-01-28 15:45:03.596 13775-13816/com.google.maps.android.utils.demo E/System: java.lang.OutOfMemoryError: OutOfMemoryError thrown while trying to throw an exception; no stack trace available
2020-01-28 15:44:31.912 589-589/? A/libc: crash_dump helper failed to exec
2020-01-28 15:45:04.379 13775-13882/? E/AndroidRuntime: Error reporting crash
    java.lang.OutOfMemoryError: OutOfMemoryError thrown while trying to throw OutOfMemoryError; no stack trace available

Other smaller KML files worked as expected.

@barbeau I actually did pull the maps v3 branch to test and ran into this crash again on my Pixel Slate. I’ll try profiling again on my phone. I also wanted to merge master into the android-sdk-v3-beta branch, which has conflicts.

I was happy to see that performance issue fixed in v3, as I was tracking it closely. There hasn’t been much movement on the v3 beta since it was first announced last spring, not even an iterative beta release with that crash fixed.

@sharkboy777 Could you please provide a sample KML file that you’re having this problem with? https://drive.google.com/file/d/1sbNgwgLqdmKbJR3nnOxpp4ZTSa7DWRFl/view?usp=sharing

Here is the drive link for kml file… please give me a solution