Search code examples
androidgeojsonosmdroid

Android osm map using geojson is very slow


I want to show geoJson objects in OSM map and to do this I'm using osmdroid (version 5.4) and osmbonuspack (version 5.8) libraries. My activity is:

public class TempMapActivity extends Activity {

MapView map;

KmlDocument mKmlGeoJson;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main_activity);
    initMap();
}

private void initMap() {
    map = (MapView) findViewById(R.id.map);
    map.setTileSource(TileSourceFactory.DEFAULT_TILE_SOURCE);
    map.setBuiltInZoomControls(true);
    map.setMultiTouchControls(true);
    IMapController mapController = map.getController();
    map.setMinZoomLevel(18);
    map.setMaxZoomLevel(22);
    map.getController().setCenter(new GeoPoint(2.9691, 101.7146));
    mapController.setZoom(18);
    setupGeoJson();
}


private void setupGeoJson() {

    String gJson = null;
    try {
        gJson = getGeoString();
    } catch (IOException e) {
        e.printStackTrace();
    }

    mKmlGeoJson = new KmlDocument();
    mKmlGeoJson.parseGeoJSON(gJson);
    FolderOverlay myOverLay = (FolderOverlay) mKmlGeoJson.mKmlRoot.
            buildOverlay(map, null, null, mKmlGeoJson);
    map.getOverlays().add(myOverLay);
    map.getController().setZoom(16);
    map.getController().setCenter(new GeoPoint(2.9691, 101.7146));
    map.invalidate();
}


private String getGeoString() throws IOException {
    InputStream is = getResources().openRawResource(R.raw.json_template);
    Writer writer = new StringWriter();
    char[] buffer = new char[1024];
    try {
        Reader reader = new BufferedReader(new InputStreamReader(is, "UTF-8"));
        int n;
        while ((n = reader.read(buffer)) != -1) {
            writer.write(buffer, 0, n);
        }
    } finally {
        is.close();
    }

    String jsonString = writer.toString();
    return jsonString;
}

}

Objects from the geoJson is showing, but the map is working very slow when zooming out/in (when loading). When I switch off to load geoJson, the map is working fine. I don't know where I'm doing wrong.


Solution

  • The Android doc says:

    Beginning in Android 3.0 (API level 11), the Android 2D rendering pipeline supports hardware acceleration, meaning that all drawing operations that are performed on a View's canvas use the GPU. Because of the increased resources required to enable hardware acceleration, your app will consume more RAM.

    It turned out that I had forgotten to disable hardware acceleration in my activity where I used OSM Map with hundreds of GeoJson elements. Now I've disabled and the map works like charm.