Skip to content
This repository has been archived by the owner on Aug 8, 2023. It is now read-only.

Expose setStyleJson and getStyleJson #9714

Merged
merged 2 commits into from
Aug 10, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -581,7 +581,7 @@ void onMapChange(int rawChange) {
try {
onMapChangedListener.onMapChanged(rawChange);
} catch (RuntimeException err) {
Timber.e("Exception (%s) in MapView.OnMapChangedListener: %s", err.getClass(), err.getMessage());
Timber.e(err, "Exception in MapView.OnMapChangedListener");
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1088,7 +1088,7 @@ private void setStyleUrl(@NonNull MapboxMapOptions options) {
}

/**
* Returns the map style currently displayed in the map view.
* Returns the map style url currently displayed in the map view.
*
* @return The URL of the map style
*/
Expand All @@ -1097,6 +1097,27 @@ public String getStyleUrl() {
return nativeMapView.getStyleUrl();
}

/**
* Loads a new map style from a json string.
* <p>
* If the style fails to load or an invalid style URL is set, the map view will become blank.
* An error message will be logged in the Android logcat and {@link MapView#DID_FAIL_LOADING_MAP} event will be
* sent.
* </p>
*/
public void setStyleJson(@NonNull String styleJson) {
nativeMapView.setStyleJson(styleJson);
}

/**
* Returns the map style json currently displayed in the map view.
*
* @return The json of the map style
*/
public String getStyleJson() {
return nativeMapView.getStyleJson();
}

//
// Annotations
//
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.mapbox.mapboxsdk.testapp.style;

import android.content.res.Resources;
import android.support.annotation.RawRes;
import android.support.test.espresso.UiController;
import android.support.test.espresso.ViewAction;
Expand All @@ -13,6 +12,7 @@
import com.mapbox.mapboxsdk.testapp.R;
import com.mapbox.mapboxsdk.testapp.activity.BaseActivityTest;
import com.mapbox.mapboxsdk.testapp.activity.style.RuntimeStyleTestActivity;
import com.mapbox.mapboxsdk.testapp.utils.ResourceUtils;
import com.mapbox.services.commons.geojson.Feature;
import com.mapbox.services.commons.geojson.FeatureCollection;
import com.mapbox.services.commons.geojson.Point;
Expand All @@ -21,18 +21,13 @@
import org.junit.Test;
import org.junit.runner.RunWith;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.io.StringWriter;
import java.io.Writer;

import timber.log.Timber;

import static android.support.test.espresso.Espresso.onView;
import static android.support.test.espresso.matcher.ViewMatchers.isDisplayed;
import static android.support.test.espresso.matcher.ViewMatchers.withId;
import static org.junit.Assert.fail;

/**
* Tests for {@link GeoJsonSource}
Expand All @@ -46,19 +41,22 @@ protected Class getActivityClass() {
}

@Test
public void testFeatureCollection() {
public void testFeatureCollection() throws Exception {
validateTestSetup();
onView(withId(R.id.mapView)).perform(new BaseViewAction() {

@Override
public void perform(UiController uiController, View view) {
GeoJsonSource source = new GeoJsonSource("source", FeatureCollection
.fromJson(readRawResource(rule.getActivity().getResources(), R.raw.test_feature_collection)));
GeoJsonSource source = null;
try {
source = new GeoJsonSource("source", FeatureCollection
.fromJson(ResourceUtils.readRawResource(rule.getActivity(), R.raw.test_feature_collection)));
} catch (IOException exception) {
Timber.e(exception);
}
mapboxMap.addSource(source);

mapboxMap.addLayer(new CircleLayer("layer", source.getId()));
}

});
}

Expand All @@ -79,14 +77,19 @@ public void perform(UiController uiController, View view) {
}

@Test
public void testFeatureProperties() {
public void testFeatureProperties() throws IOException {
validateTestSetup();
onView(withId(R.id.mapView)).perform(new BaseViewAction() {

@Override
public void perform(UiController uiController, View view) {
GeoJsonSource source = new GeoJsonSource("source",
readRawResource(rule.getActivity().getResources(), R.raw.test_feature_properties));
GeoJsonSource source = null;
try {
source = new GeoJsonSource("source",
ResourceUtils.readRawResource(rule.getActivity(), R.raw.test_feature_properties));
} catch (IOException exception) {
Timber.e(exception);
}
mapboxMap.addSource(source);

mapboxMap.addLayer(new CircleLayer("layer", source.getId()));
Expand Down Expand Up @@ -141,8 +144,11 @@ public void perform(UiController uiController, View view) {
Layer layer = new CircleLayer("layer", source.getId());
mapboxMap.addLayer(layer);

source.setGeoJson(Feature.fromJson(
readRawResource(rule.getActivity().getResources(), resource)));
try {
source.setGeoJson(Feature.fromJson(ResourceUtils.readRawResource(rule.getActivity(), resource)));
} catch (IOException exception) {
Timber.e(exception);
}

mapboxMap.removeLayer(layer);
mapboxMap.removeSource(source);
Expand All @@ -151,27 +157,6 @@ public void perform(UiController uiController, View view) {
});
}

private String readRawResource(Resources resources, @RawRes int rawResource) {
InputStream is = resources.openRawResource(rawResource);
Writer writer = new StringWriter();
char[] buffer = new char[1024];
try {
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();
}
} catch (IOException err) {
fail(err.getMessage());
}

return writer.toString();
}

public abstract class BaseViewAction implements ViewAction {

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import android.graphics.BitmapFactory;
import android.graphics.RectF;
import android.os.Bundle;
import android.support.annotation.RawRes;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Toast;
Expand All @@ -14,15 +13,10 @@
import com.mapbox.mapboxsdk.style.layers.SymbolLayer;
import com.mapbox.mapboxsdk.style.sources.GeoJsonSource;
import com.mapbox.mapboxsdk.testapp.R;
import com.mapbox.mapboxsdk.testapp.utils.ResourceUtils;
import com.mapbox.services.commons.geojson.Feature;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.io.StringWriter;
import java.io.Writer;
import java.util.List;

import timber.log.Timber;
Expand Down Expand Up @@ -57,9 +51,10 @@ public void onMapReady(final MapboxMap mapboxMap) {

// Add a symbol layer (also works with annotations)
try {
mapboxMap.addSource(new GeoJsonSource("symbols-source", readRawResource(R.raw.test_points_utrecht)));
mapboxMap.addSource(new GeoJsonSource("symbols-source", ResourceUtils.readRawResource(
QueryRenderedFeaturesBoxSymbolCountActivity.this, R.raw.test_points_utrecht)));
} catch (IOException ioException) {
Timber.e(ioException,"Could not load geojson");
Timber.e(ioException, "Could not load geojson");
return;
}
mapboxMap.addImage(
Expand Down Expand Up @@ -94,23 +89,6 @@ public void onClick(View view) {
});
}

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();
}

return writer.toString();
}

public MapboxMap getMapboxMap() {
return mapboxMap;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,18 @@ protected void onCreate(Bundle savedInstanceState) {
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) {
Expand All @@ -63,13 +71,12 @@ public void onCameraChange(CameraPosition position) {
}
});


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();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.RawRes;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.view.MenuItem;
Expand All @@ -18,14 +17,9 @@
import com.mapbox.mapboxsdk.style.sources.GeoJsonSource;
import com.mapbox.mapboxsdk.style.sources.Source;
import com.mapbox.mapboxsdk.testapp.R;
import com.mapbox.mapboxsdk.testapp.utils.ResourceUtils;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.io.StringWriter;
import java.io.Writer;

import timber.log.Timber;

Expand Down Expand Up @@ -355,7 +349,7 @@ private void addParksLayer() {
// Add a source
Source source;
try {
source = new GeoJsonSource("amsterdam-parks-source", readRawResource(R.raw.amsterdam));
source = new GeoJsonSource("amsterdam-parks-source", ResourceUtils.readRawResource(this, R.raw.amsterdam));
mapboxMap.addSource(source);
} catch (IOException ioException) {
Toast.makeText(
Expand All @@ -375,21 +369,4 @@ private void addParksLayer() {
)
);
}

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();
}

return writer.toString();
}
}
Loading