diff options
author | Ivo van Dongen <info@ivovandongen.nl> | 2016-08-16 16:11:39 +0200 |
---|---|---|
committer | Ivo van Dongen <info@ivovandongen.nl> | 2016-08-16 17:04:35 +0200 |
commit | 01a33749f9cd05f4b78c0463327821a47ae96baf (patch) | |
tree | 521969ca9a8540a136dc3556c47fbb14ba65ec91 /platform/android | |
parent | 2a5bf311ac0b79103fc7c9ec28a5493570e5c052 (diff) | |
download | qtlocation-mapboxgl-01a33749f9cd05f4b78c0463327821a47ae96baf.tar.gz |
[android] #5941 - add geojson typed constructor to GeoJsonSource and an example
Diffstat (limited to 'platform/android')
2 files changed, 39 insertions, 28 deletions
diff --git a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/style/sources/GeoJsonSource.java b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/style/sources/GeoJsonSource.java index 6f2113ab0e..fd226a0060 100644 --- a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/style/sources/GeoJsonSource.java +++ b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/style/sources/GeoJsonSource.java @@ -1,5 +1,8 @@ package com.mapbox.mapboxsdk.style.sources; +import com.google.gson.Gson; +import com.mapbox.services.commons.geojson.FeatureCollection; + import java.net.URL; import java.util.HashMap; @@ -23,12 +26,7 @@ public class GeoJsonSource extends Source { if (geoJson == null || geoJson.startsWith("http")) { throw new IllegalArgumentException("Expected a raw json body"); } - - //Wrap the String in a map as an Object is expected by the - //style conversion template - HashMap<String, String> wrapper = new HashMap<>(); - wrapper.put(DATA_KEY, geoJson); - this.put(DATA_KEY, wrapper); + setRawJson(geoJson); } /** @@ -42,6 +40,17 @@ public class GeoJsonSource extends Source { this.put(DATA_KEY, url.toExternalForm()); } + /** + * Create a GeoJsonSource from a FeatureCollection + * + * @param id the source id + * @param features the features + */ + public GeoJsonSource(String id, FeatureCollection features) { + super(id, TYPE); + setRawJson(features.toJson()); + } + public GeoJsonSource withCluster(boolean cluster) { this.put("cluster", cluster); return this; @@ -56,4 +65,12 @@ public class GeoJsonSource extends Source { this.put("clusterRadius", radius); return this; } + + private void setRawJson(String geoJson) { + //Wrap the String in a map as an Object is expected by the + //style conversion template + HashMap<String, String> wrapper = new HashMap<>(); + wrapper.put(DATA_KEY, geoJson); + this.put(DATA_KEY, wrapper); + } } diff --git a/platform/android/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp/activity/feature/QueryRenderedFeaturesBoxHighlightActivity.java b/platform/android/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp/activity/feature/QueryRenderedFeaturesBoxHighlightActivity.java index 53937b4eca..a993feebf9 100644 --- a/platform/android/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp/activity/feature/QueryRenderedFeaturesBoxHighlightActivity.java +++ b/platform/android/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp/activity/feature/QueryRenderedFeaturesBoxHighlightActivity.java @@ -11,22 +11,21 @@ import android.view.MenuItem; import android.view.View; import android.widget.Toast; -import com.google.gson.JsonElement; import com.mapbox.mapboxsdk.annotations.Marker; -import com.mapbox.mapboxsdk.annotations.PolygonOptions; import com.mapbox.mapboxsdk.camera.CameraUpdateFactory; import com.mapbox.mapboxsdk.geometry.LatLng; import com.mapbox.mapboxsdk.maps.MapView; import com.mapbox.mapboxsdk.maps.MapboxMap; import com.mapbox.mapboxsdk.maps.OnMapReadyCallback; +import com.mapbox.mapboxsdk.style.layers.FillLayer; +import com.mapbox.mapboxsdk.style.sources.GeoJsonSource; import com.mapbox.mapboxsdk.testapp.R; import com.mapbox.services.commons.geojson.Feature; -import com.mapbox.services.commons.geojson.Polygon; -import com.mapbox.services.commons.models.Position; +import com.mapbox.services.commons.geojson.FeatureCollection; -import java.util.ArrayList; import java.util.List; -import java.util.Map; + +import static com.mapbox.mapboxsdk.style.layers.PropertyFactory.fillColor; /** * Demo's query rendered features @@ -44,8 +43,6 @@ public class QueryRenderedFeaturesBoxHighlightActivity extends AppCompatActivity setContentView(R.layout.activity_query_features_box); setupActionBar(); - final float density = getResources().getDisplayMetrics().density; - final View selectionBox = findViewById(R.id.selection_box); //Initialize map as normal @@ -57,6 +54,7 @@ public class QueryRenderedFeaturesBoxHighlightActivity extends AppCompatActivity public void onMapReady(final MapboxMap mapboxMap) { QueryRenderedFeaturesBoxHighlightActivity.this.mapboxMap = mapboxMap; + //Add a fill layer to display stuff on selectionBox.setOnClickListener(new View.OnClickListener() { @Override @@ -71,21 +69,17 @@ public class QueryRenderedFeaturesBoxHighlightActivity extends AppCompatActivity //Show count Toast.makeText(QueryRenderedFeaturesBoxHighlightActivity.this, String.format("%s features in box", features.size()), Toast.LENGTH_SHORT).show(); - for (Feature feature : features) { - if (feature.getGeometry() instanceof Polygon) { - Polygon building = (Polygon) feature.getGeometry(); - - //Convert outer ring - List<List<Position>> coordinates = building.getCoordinates(); - List<Position> outerRing = coordinates.get(0); - List<LatLng> points = new ArrayList<LatLng>(); - for (Position position : outerRing) { - points.add(new LatLng(position.getLatitude(), position.getLongitude())); - } - - mapboxMap.addPolygon(new PolygonOptions().addAll(points).fillColor(Color.RED)); - } + //remove layer / source if already added + try { + mapboxMap.removeSource("highlighted-shapes-source"); + mapboxMap.removeLayer("highlighted-shapes-layer"); + } catch (Exception e) { + //that's ok } + + //Add layer / source + mapboxMap.addSource(new GeoJsonSource("highlighted-shapes-source", FeatureCollection.fromFeatures(features))); + mapboxMap.addLayer(new FillLayer("highlighted-shapes-layer", "highlighted-shapes-source").withProperties(fillColor(Color.RED))); } }); |