diff options
author | Tobrun Van Nuland <tobrun.van.nuland@gmail.com> | 2017-08-07 10:11:13 +0200 |
---|---|---|
committer | Tobrun <tobrun@mapbox.com> | 2017-08-10 13:27:17 +0200 |
commit | 1300e89c0b431951f27dc14fcdf3c57ea15735bb (patch) | |
tree | cfa329f4f21c92a15ada7e56a136bc425be8753d /platform/android/MapboxGLAndroidSDKTestApp/src | |
parent | 688431c663161b992bbf2af24477dfdfc4b9bb64 (diff) | |
download | qtlocation-mapboxgl-1300e89c0b431951f27dc14fcdf3c57ea15735bb.tar.gz |
[android] - expose setStyleJson and getStyleJson
Diffstat (limited to 'platform/android/MapboxGLAndroidSDKTestApp/src')
6 files changed, 142 insertions, 31 deletions
diff --git a/platform/android/MapboxGLAndroidSDKTestApp/src/main/AndroidManifest.xml b/platform/android/MapboxGLAndroidSDKTestApp/src/main/AndroidManifest.xml index 9ef85be755..59d5051587 100644 --- a/platform/android/MapboxGLAndroidSDKTestApp/src/main/AndroidManifest.xml +++ b/platform/android/MapboxGLAndroidSDKTestApp/src/main/AndroidManifest.xml @@ -687,7 +687,7 @@ android:label="@string/activity_bottom_sheet"> <meta-data android:name="@string/category" - android:value="@string/category_basic"/> + android:value="@string/category_maplayout"/> </activity> <!-- For Instrumentation tests --> diff --git a/platform/android/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp/activity/maplayout/DebugModeActivity.java b/platform/android/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp/activity/maplayout/DebugModeActivity.java index dea1abea8e..5b394a7895 100644 --- a/platform/android/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp/activity/maplayout/DebugModeActivity.java +++ b/platform/android/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp/activity/maplayout/DebugModeActivity.java @@ -41,10 +41,18 @@ public class DebugModeActivity extends AppCompatActivity { setContentView(R.layout.activity_debug_mode); mapView = (MapView) findViewById(R.id.mapView); + mapView.addOnMapChangedListener(new MapView.OnMapChangedListener() { + @Override + public void onMapChanged(int change) { + if (change == MapView.DID_FINISH_LOADING_STYLE && mapboxMap != null) { + Timber.e("New loaded style = %s", mapboxMap.getStyleJson()); + } + } + }); + mapView.setTag(true); mapView.setStyleUrl(STYLES[currentStyleIndex]); mapView.onCreate(savedInstanceState); - mapView.getMapAsync(new OnMapReadyCallback() { @Override public void onMapReady(@NonNull MapboxMap map) { @@ -63,13 +71,12 @@ public class DebugModeActivity extends AppCompatActivity { } }); - FloatingActionButton fabDebug = (FloatingActionButton) findViewById(R.id.fabDebug); fabDebug.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { if (mapboxMap != null) { - Timber.d("Debug FAB: isDebug Active? %s" , mapboxMap.isDebugActive()); + Timber.d("Debug FAB: isDebug Active? %s", mapboxMap.isDebugActive()); mapboxMap.cycleDebugOptions(); } } diff --git a/platform/android/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp/activity/style/StyleFileActivity.java b/platform/android/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp/activity/style/StyleFileActivity.java index 7dcae6a16d..29683c4a66 100644 --- a/platform/android/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp/activity/style/StyleFileActivity.java +++ b/platform/android/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp/activity/style/StyleFileActivity.java @@ -1,5 +1,6 @@ package com.mapbox.mapboxsdk.testapp.activity.style; +import android.content.Context; import android.os.AsyncTask; import android.os.Bundle; import android.support.annotation.NonNull; @@ -25,11 +26,12 @@ import java.io.InputStreamReader; import java.io.Reader; import java.io.StringWriter; import java.io.Writer; +import java.lang.ref.WeakReference; import timber.log.Timber; /** - * Test activity showcasing how to use a file:// resource for the style.json + * Test activity showcasing how to use a file:// resource for the style.json and how to use MapboxMap#setStyleJson. */ public class StyleFileActivity extends AppCompatActivity { @@ -48,13 +50,21 @@ public class StyleFileActivity extends AppCompatActivity { public void onMapReady(@NonNull final MapboxMap map) { mapboxMap = map; - FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab); + FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab_file); fab.setColorFilter(ContextCompat.getColor(StyleFileActivity.this, R.color.primary)); fab.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { - Timber.i("Loading style file"); - new CreateStyleFileTask().execute(); + new CreateStyleFileTask(view.getContext(), mapboxMap).execute(); + } + }); + + FloatingActionButton fabStyleJson = (FloatingActionButton) findViewById(R.id.fab_style_json); + fabStyleJson.setColorFilter(ContextCompat.getColor(StyleFileActivity.this, R.color.primary)); + fabStyleJson.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View view) { + new LoadStyleFileTask(view.getContext(), mapboxMap).execute(); } }); } @@ -62,10 +72,51 @@ public class StyleFileActivity extends AppCompatActivity { } /** + * Task to read a style file from the raw folder + */ + private static class LoadStyleFileTask extends AsyncTask<Void, Void, String> { + private WeakReference<Context> context; + private WeakReference<MapboxMap> mapboxMap; + + LoadStyleFileTask(Context context, MapboxMap mapboxMap) { + this.context = new WeakReference<>(context); + this.mapboxMap = new WeakReference<>(mapboxMap); + } + + @Override + protected String doInBackground(Void... voids) { + String styleJson = ""; + try { + styleJson = RawResourceReaderWriter.readRawResource(context.get(), R.raw.sat_style); + } catch (Exception exception) { + Timber.e(exception, "Can't load local file style"); + } + return styleJson; + } + + @Override + protected void onPostExecute(String json) { + super.onPostExecute(json); + Timber.d("Read json, %s", json); + MapboxMap mapboxMap = this.mapboxMap.get(); + if (mapboxMap != null) { + mapboxMap.setStyleJson(json); + } + } + } + + /** * Task to write a style file to local disk and load it in the map view */ - private class CreateStyleFileTask extends AsyncTask<Void, Integer, Long> { + private static class CreateStyleFileTask extends AsyncTask<Void, Integer, Long> { private File cacheStyleFile; + private WeakReference<Context> context; + private WeakReference<MapboxMap> mapboxMap; + + CreateStyleFileTask(Context context, MapboxMap mapboxMap) { + this.context = new WeakReference<>(context); + this.mapboxMap = new WeakReference<>(mapboxMap); + } @Override protected Long doInBackground(Void... params) { @@ -73,33 +124,22 @@ public class StyleFileActivity extends AppCompatActivity { cacheStyleFile = File.createTempFile("my-", ".style.json"); cacheStyleFile.createNewFile(); Timber.i("Writing style file to: %s", cacheStyleFile.getAbsolutePath()); - writeToFile(cacheStyleFile, readRawResource(R.raw.local_style)); + Context context = this.context.get(); + if (context != null) { + writeToFile(cacheStyleFile, RawResourceReaderWriter.readRawResource(context, R.raw.local_style)); + } } catch (Exception exception) { - Toast.makeText(StyleFileActivity.this, "Could not create style file in cache dir", Toast.LENGTH_SHORT).show(); + Toast.makeText(context.get(), "Could not create style file in cache dir", Toast.LENGTH_SHORT).show(); } return 1L; } protected void onPostExecute(Long result) { // Actual file:// usage - mapboxMap.setStyleUrl("file://" + cacheStyleFile.getAbsolutePath()); - } - - private String readRawResource(@RawRes int rawResource) throws IOException { - InputStream is = getResources().openRawResource(rawResource); - Writer writer = new StringWriter(); - char[] buffer = new char[1024]; - try { - Reader reader = new BufferedReader(new InputStreamReader(is, "UTF-8")); - int numRead; - while ((numRead = reader.read(buffer)) != -1) { - writer.write(buffer, 0, numRead); - } - } finally { - is.close(); + MapboxMap mapboxMap = this.mapboxMap.get(); + if (mapboxMap != null) { + mapboxMap.setStyleUrl("file://" + cacheStyleFile.getAbsolutePath()); } - - return writer.toString(); } private void writeToFile(File file, String contents) throws IOException { @@ -115,6 +155,28 @@ public class StyleFileActivity extends AppCompatActivity { } } + static class RawResourceReaderWriter { + static String readRawResource(Context context, @RawRes int rawResource) throws IOException { + String json = ""; + if (context != null) { + InputStream is = context.getResources().openRawResource(rawResource); + Writer writer = new StringWriter(); + char[] buffer = new char[1024]; + try { + Reader reader = new BufferedReader(new InputStreamReader(is, "UTF-8")); + int numRead; + while ((numRead = reader.read(buffer)) != -1) { + writer.write(buffer, 0, numRead); + } + } finally { + is.close(); + } + json = writer.toString(); + } + return json; + } + } + @Override protected void onStart() { super.onStart(); diff --git a/platform/android/MapboxGLAndroidSDKTestApp/src/main/res/layout/activity_style_file.xml b/platform/android/MapboxGLAndroidSDKTestApp/src/main/res/layout/activity_style_file.xml index b133f3d9a5..83150be4bf 100644 --- a/platform/android/MapboxGLAndroidSDKTestApp/src/main/res/layout/activity_style_file.xml +++ b/platform/android/MapboxGLAndroidSDKTestApp/src/main/res/layout/activity_style_file.xml @@ -15,7 +15,18 @@ app:mapbox_styleUrl="@string/mapbox_style_mapbox_streets"/> <android.support.design.widget.FloatingActionButton - android:id="@id/fab" + android:id="@+id/fab_style_json" + android:layout_width="wrap_content" + android:layout_height="wrap_content" + android:layout_alignParentEnd="true" + android:layout_alignParentRight="true" + android:layout_above="@+id/fab_file" + android:layout_margin="@dimen/fab_margin" + android:src="@drawable/ic_add" + app:backgroundTint="@android:color/white"/> + + <android.support.design.widget.FloatingActionButton + android:id="@id/fab_file" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentBottom="true" diff --git a/platform/android/MapboxGLAndroidSDKTestApp/src/main/res/raw/sat_style.json b/platform/android/MapboxGLAndroidSDKTestApp/src/main/res/raw/sat_style.json new file mode 100644 index 0000000000..f2cd969be4 --- /dev/null +++ b/platform/android/MapboxGLAndroidSDKTestApp/src/main/res/raw/sat_style.json @@ -0,0 +1,31 @@ +{ + "version": 8, + "name": "Satellite", + "metadata": { + "mapbox:autocomposite": true + }, + "sources": { + "mapbox": { + "type": "raster", + "url": "mapbox://mapbox.satellite", + "tileSize": 256 + } + }, + "sprite": "mapbox://sprites/mapbox/satellite-v8", + "glyphs": "mapbox://fonts/mapbox/{fontstack}/{range}.pbf", + "layers": [ + { + "id": "background", + "type": "background", + "paint": { + "background-color": "rgb(4,7,14)" + } + }, + { + "id": "satellite", + "type": "raster", + "source": "mapbox", + "source-layer": "mapbox_satellite_full" + } + ] +}
\ No newline at end of file diff --git a/platform/android/MapboxGLAndroidSDKTestApp/src/main/res/values/strings.xml b/platform/android/MapboxGLAndroidSDKTestApp/src/main/res/values/strings.xml index 2e5d2ef0b6..4774fa8d8f 100644 --- a/platform/android/MapboxGLAndroidSDKTestApp/src/main/res/values/strings.xml +++ b/platform/android/MapboxGLAndroidSDKTestApp/src/main/res/values/strings.xml @@ -187,8 +187,8 @@ <string name="dynamic_marker_chelsea_snippet">Stamford Bridge</string> <string name="dynamic_marker_arsenal_title">Arsenal</string> <string name="dynamic_marker_arsenal_snippet">Emirates Stadium</string> - <string name="debug_zoom">Zoom: %f</string> - <string name="viewcache_size">"ViewCache size %d"</string> + <string name="debug_zoom">Zoom: %.2f</string> + <string name="viewcache_size">ViewCache size %.2f</string> <string name="latitude">Latitude</string> <string name="min_value">-180</string> <string name="longitude">Longitude</string> |