title: GeoJson layer on android maps
body: <p>I have a huge <code>GeoJson</code> file (about 12MB) to be added to a map fragment. I am using the android maps utility library to render it on the map. Its taking about 5-10 seconds to show the layer on the map, during which the app becomes unresponsive.Since the layer has to be added on the main UI thread, I can't move it to a background thread.Is there any way to keep the app responsive during this time? or Any alternatives to using the <code>Geojosn</code> file ? </p>
  
  <p>I have moved the <code>addLayer()</code> method to the UI thread and its working. I have encountered a few problems. Here is the code.</p>
  
  <p><strong>ThreatMap.java</strong></p>
  
  <pre><code>public class ThreatMap extends AppCompatActivity implements OnMapReadyCallback {
  
      private static final String TAG = "ThreatMap";
  
      @Override
      public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.threat_map);
  
          Toolbar toolbar = (Toolbar) findViewById(R.id.app_bar_threat_map);
          TextView title = (TextView) findViewById(R.id.main_title);
          title.setText(R.string.actionbar_threat_map);
          setSupportActionBar(toolbar);
  
          getSupportActionBar().setHomeButtonEnabled(true);
          getSupportActionBar().setDisplayHomeAsUpEnabled(true);
          getSupportActionBar().setDisplayShowTitleEnabled(false);
  
  
          SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
                  .findFragmentById(R.id.map);
          mapFragment.getMapAsync(this);
  
      }
  
      @Override
      public void onMapReady(GoogleMap googleMap) {
  
          ThreatMapRenderingTask task = new ThreatMapRenderingTask();
          task.execute(googleMap);
      }
  
  
      private class ThreatMapRenderingTask extends AsyncTask&lt;GoogleMap, Void, GeoJsonLayer&gt; {
  
          ProgressDialog progressDialog;
  
          public ThreatMapRenderingTask() {
              progressDialog = new ProgressDialog(ThreatMap.this);
          }
  
          @Override
          protected void onPreExecute() {
  
              progressDialog.setMessage("Loading Threat Map");
              progressDialog.show();
          }
  
  
          @Override
          protected GeoJsonLayer doInBackground(GoogleMap... googleMap) {
  
              GoogleMap gMap = googleMap[0];
              GeoJsonLayer layer = null ;
  
              PolygonOptions first = new PolygonOptions();
              try {
                 layer =  new GeoJsonLayer(gMap, R.raw.threatmap, getApplicationContext());
  
              } catch (IOException e) {
                  e.printStackTrace();
              } catch (JSONException e) {
                  e.printStackTrace();
              }
              return layer;
          }
  
          @Override
          protected void onPostExecute(GeoJsonLayer layer) {
  
              layer.addLayerToMap();
  
              for (GeoJsonFeature feature : layer.getFeatures()) {
                  if (feature.hasProperty("EX_BOX_ID")) {
                      String state = feature.getProperty("STATE_PROV");
                      int ex_box_id = Integer.parseInt(feature.getProperty("EX_BOX_ID"));
                      String pb_box_id = feature.getProperty("PB_BOX_ID");
  
  
                      if (ex_box_id &lt; 25) {
                          GeoJsonPolygonStyle polygonStyleRed = new GeoJsonPolygonStyle();
                          polygonStyleRed.setFillColor(Color.RED);
                          feature.setPolygonStyle(polygonStyleRed);
                      } else if (ex_box_id &lt; 50 &amp;&amp; ex_box_id &gt; 25) {
                          GeoJsonPolygonStyle polygonStyleYellow = new GeoJsonPolygonStyle();
                          polygonStyleYellow.setFillColor(Color.YELLOW);
                          feature.setPolygonStyle(polygonStyleYellow);
                      } else {
                          GeoJsonPolygonStyle polygonStyleGreen = new GeoJsonPolygonStyle();
                          polygonStyleGreen.setFillColor(Color.GREEN);
                          feature.setPolygonStyle(polygonStyleGreen);
                      }
                  }
              }
  
              if (progressDialog.isShowing()) {
                  progressDialog.dismiss();
              }
  
          }
      }
  }
  </code></pre>
  
  <p><strong>threat_map_fragment.xml</strong></p>
  
  <pre><code>&lt;fragment
      android:id="@+id/map"
      android:name="com.google.android.gms.maps.SupportMapFragment"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      tools:context=".ThreatMap"
      map:cameraTargetLat="78.00"
      map:cameraTargetLng="16.00"
      map:cameraZoom="3"
      xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:tools="http://schemas.android.com/tools"
      xmlns:map="http://schemas.android.com/apk/res-auto" /&gt;
  </code></pre>
  
  <p><strong>threat_map.xml</strong></p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:orientation="vertical"&gt;
  
      &lt;include
          android:id="@+id/app_bar_threat_map"
          layout="@layout/appbar" /&gt;
      &lt;include
         android:id="@+id/map"
         android:layout_width="match_parent"
         android:layout_height="match_parent"
         layout="@layout/threat_map_fragment"
         android:layout_below="@id/app_bar_threat_map"/&gt;
  
  &lt;/LinearLayout&gt;
  </code></pre>
  
  <p>I am having a couple of problems. </p>
  
  <ol>
  <li><p>The map fragment is not zooming to the default location defined in the xml file.</p></li>
  <li><p>I have used <code>GeoJsonPolygonStyle</code> method, and added color to the polygons on the layer. The color is not visible at all zoom levels. I have to zoom in a lot, to see the color for some polygons. What is the reason for this and is there any work around ?</p></li>
  </ol>