2016-06-18 18 views
1

Ich habe eine riesige GeoJson Datei (ca. 12 MB) zu einem Kartenfragment hinzugefügt werden. Ich verwende die Android-Karten-Dienstprogramm-Bibliothek, um es auf der Karte zu rendern. Es dauert etwa 5-10 Sekunden, um die Ebene auf der Karte anzuzeigen, während der die App nicht mehr reagiert. Da die Ebene im Hauptthread der Benutzeroberfläche hinzugefügt werden muss, kann ich sie nicht in einen Hintergrundthread verschieben um die App während dieser Zeit ansprechbar zu halten? oder Alternativen zur Verwendung der Geojosn Datei?GeoJson-Layer auf Android-Karten

Ich habe die addLayer() Methode in den UI-Thread verschoben und funktioniert. Ich habe ein paar Probleme festgestellt. Hier ist der Code.

ThreatMap.java

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<GoogleMap, Void, GeoJsonLayer> { 

     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 < 25) { 
         GeoJsonPolygonStyle polygonStyleRed = new GeoJsonPolygonStyle(); 
         polygonStyleRed.setFillColor(Color.RED); 
         feature.setPolygonStyle(polygonStyleRed); 
        } else if (ex_box_id < 50 && ex_box_id > 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(); 
      } 

     } 
    } 
} 

threat_map_fragment.xml

<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" /> 

threat_map.xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical"> 

    <include 
     android:id="@+id/app_bar_threat_map" 
     layout="@layout/appbar" /> 
    <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"/> 

</LinearLayout> 

Ich habe ein paar Probleme.

  1. Das Kartenfragment zoomt nicht auf den in der XML-Datei definierten Standardspeicherort.

  2. Ich habe die Methode GeoJsonPolygonStyle verwendet und den Polygonen auf der Ebene Farbe hinzugefügt. Die Farbe ist nicht in allen Zoomstufen sichtbar. Ich muss viel hineinzoomen, um die Farbe einiger Polygone zu sehen. Was ist der Grund dafür und gibt es irgendwelche Arbeiten?

+0

Gibt es eine Chance, die Datei in kleinere Bits zu teilen? Bist du sicher, dass du den Import auf dem Hauptthread machen musst? Gibt es nicht eine Methode 'addLayer()', die im UI-Thread aufgerufen werden muss? – m02ph3u5

+0

danke @ m02ph3u5, ich habe die addLayer() zu UI-Thread und seine Arbeit bewegt, ich habe danach einige weitere Probleme begegnet. Ich aktualisiere die Frage, bitte schau sie dir an. – rtk

+0

versuchen Sie diesen Beispielcode auf githube https://github.com/hiepxuan2008/GoogleMapDirectionSimple/ –

Antwort

0

Ich stehe vor ähnlichen Problem. Momentan teile ich die größere Geojson-Datei in kleine Stücke und lade sie durch Aufruf mehrerer Async-Aufgaben (Parallelverarbeitung). Dies führte zu einer Verbesserung der Performance im Vergleich zum Laden einer einzelnen Datei. Aber ich suche immer noch nach Optimierungstechniken. Sie können hier überprüfen (Show large geojson file in android google maps)