Skip to content
This repository has been archived by the owner on Oct 7, 2024. It is now read-only.

Commit

Permalink
added loadJsonFromAsset() method
Browse files Browse the repository at this point in the history
  • Loading branch information
Langston Smith committed May 22, 2019
1 parent 7fa22e6 commit fff1abe
Showing 1 changed file with 19 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import com.mapbox.mapboxsdk.style.layers.SymbolLayer;
import com.mapbox.mapboxsdk.style.sources.GeoJsonSource;

import java.io.IOException;
import java.io.InputStream;
import java.lang.ref.WeakReference;
import java.util.Scanner;
Expand Down Expand Up @@ -66,7 +67,8 @@ protected void onCreate(Bundle savedInstanceState) {
@Override
public void onMapReady(@NonNull final MapboxMap mapboxMap) {
mapboxMap.setStyle(new Style.Builder().fromUrl(Style.LIGHT)
.withSource(new GeoJsonSource(GEOJSON_SRC_ID,"asset://poi_places.geojson"))
.withSource(new GeoJsonSource(GEOJSON_SRC_ID,
loadJsonFromAsset("poi_places.geojson")))
// Adds a SymbolLayer to display POI labels
.withLayer(new SymbolLayer(POI_LABELS_LAYER_ID, GEOJSON_SRC_ID)
.withProperties(
Expand All @@ -90,6 +92,22 @@ public void onStyleLoaded(@NonNull final Style style) {
});
}

private String loadJsonFromAsset(String filename) {
// Using this method to load in GeoJSON files from the assets folder.
try {
InputStream is = getAssets().open(filename);
int size = is.available();
byte[] buffer = new byte[size];
is.read(buffer);
is.close();
return new String(buffer, "UTF-8");

} catch (IOException ex) {
ex.printStackTrace();
return null;
}
}

// Add the mapView lifecycle to the activity's lifecycle methods
@Override
public void onResume() {
Expand Down

0 comments on commit fff1abe

Please sign in to comment.