Show large geojson file in android google maps

I am trying to show a geojson layer for google map. The code is as follows. The geojson file is stored locally in my source folder. The file has 286 functions (about 15 MB). Therefore, reading this file and displaying it consumes more time. Initially, I had a memory error that is deleted by setting the big heap as true in the manifest file. How to quickly download this file (it currently takes a minute or more)? I am wondering if there is another effective method for this. After that, I will also have other tasks of getting functions and displaying some properties.

@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map); mapFragment.getMapAsync(this); } @Override public void onMapReady(GoogleMap googleMap) { mMap = googleMap; mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(myanmar, 5.25f)); new MyAsyncTask().execute(); } public class MyAsyncTask extends AsyncTask <Void, Void, Void> { ProgressDialog pd; GeoJsonLayer layer; @Override protected void onPreExecute() { super.onPreExecute(); pd = new ProgressDialog(MapsActivity.this); pd.setMessage("Loading Data"); pd.show(); } @Override protected Void doInBackground(Void... voids) { try { layer = new GeoJsonLayer(mMap, R.raw.myanmar, getApplicationContext()); } catch (Exception e) { Log.d("Error is : ", e.toString()); } return null; } @Override protected void onPostExecute(Void aVoid) { super.onPostExecute(aVoid); pd.cancel(); layer.addLayerToMap(); } } 

A very small part of my geojson file looks like this:

 { "type": "FeatureCollection", "crs": { "type": "name", "properties": { "name": "urn:ogc:def:crs:OGC:1.3:CRS84" } }, "features": [ { "type": "Feature", "properties": { "ID_3": 193, "NAME_3": "Chaung-U" }, "geometry": { "type": "MultiPolygon", "coordinates": [ [ [ [ 95.348327636718807, 21.878610610961914 ], [ 95.297210693359432, 21.860000610351676 ], [ 95.286926269531307, 21.853612899780387 ], [ 95.276092529296989, 21.850000381469727 ], [ 95.265823364257926, 21.84832954406744 ], [ 95.257217407226676, 21.846940994262695 ] ] ] ] } }, { "type": "Feature", "properties": { "ID_3": 199, "NAME_3": "Myaung" }, "geometry": { "type": "MultiPolygon", "coordinates": [ [ [ [ 95.376281738281193, 21.708541870117301 ], [ 95.375259399414119, 21.703050613403434 ], [ 95.370529174804801, 21.681390762329102 ], [ 95.367752075195313, 21.664720535278434 ], [ 95.366699218750114, 21.658369064331055 ], [ 95.362762451171875, 21.649999618530273 ] ] ] ] } } ]} 

I am trying to split a file into several small file blocks and run separate async tasks in parallel, each processes a separate file:

 for (int i = 1; i < 3; i++) { new MyAsyncTask().executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR,i); } 

AsyncTask:

 public class MyAsyncTask extends AsyncTask <Integer, Void, GeoJsonLayer> { @Override protected void onPreExecute() { super.onPreExecute(); } @Override protected GeoJsonLayer doInBackground(Integer... integers) { int i = integers[0]; try { GeoJsonLayer layer = null; switch (i) { case 1: layer = new GeoJsonLayer(mMap, R.raw.small_1, getApplicationContext()); break; case 2: layer = new GeoJsonLayer(mMap, R.raw.small_2, getApplicationContext()); break; //other cases } for (GeoJsonFeature feature : layer.getFeatures()) { geoJsonFeatures.put(Integer.parseInt(feature.getProperty("ID_3")), feature); } return layer; } catch (Exception e) { return null; } } @Override protected void onPostExecute(GeoJsonLayer layer) { super.onPostExecute(layer); layer.addLayerToMap(); } } 

Is this an offer? At the same time, it decreases compared to the previous one, but still does not take into account the user experience coefficient very quickly.

+5
source share
3 answers

Finally, I solved the problem by reducing the size of the geojson file. It uses http://mapshaper.org/ , an online tool that provides you with options for reducing size, but keeping your shape. Used to obtain a simplified file (almost 300 KB) and an explosion, the download is completed within a few seconds. Hope this helps someone run into the same problem.

+2
source

I had code to deserialize geojson code. But I have never tried Google Map before. Maybe you may have another question?

Below I will show you how to deserialize your huge geojson file using GSON Streaming.

GeoJSON.java

 public class GeoJSON { @SerializedName("type") private String type; @SerializedName("features") private List<Features> features; public String getType() { return type; } public void setType(String type) { this.type = type; } public List<Features> getFeatures() { return features; } public void setFeatures(List<Features> features) { this.features = features; } } 

Features java

 public class Features { @SerializedName("type") private String type; public String getType() { return type; } public void setType(String type) { this.type = type; } } 

Convert json to pojo class

 private void readGeoJSON() { try { InputStream is = getAssets().open("geo.json"); JsonReader reader = new JsonReader(new InputStreamReader(is, "UTF-8")); Gson gson = new GsonBuilder().create(); GeoJSON geoJSON = new GeoJSON(); ArrayList<Features> features = new ArrayList<>(); reader.beginObject(); while (reader.hasNext()){ String key = reader.nextName(); if(key.equals("type")){ geoJSON.setType(reader.nextString()); }else if(key.equals("features")){ // read array reader.beginArray(); while (reader.hasNext()) { Features o = gson.fromJson(reader, Features.class); features.add(o); } reader.endArray(); }else{ reader.skipValue(); //avoid some unhandle events } geoJSON.setFeatures(features); } reader.endObject(); reader.close(); } catch (IOException e) { e.printStackTrace(); } } 

With this approach, we load all the objects into memory, but one by one, and not the entire file at once.

Suppose I store a geo.json file in a resource folder. Here is how you can deserialize your local geo.json, and I checked the code, it worked. You may need to add to other fields. Hope this can solve the memory issue.

0
source

Source: https://habr.com/ru/post/1264285/


All Articles