This repository has been archived by the owner on Oct 7, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 493
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Symbol switch based on zoom level example (#929)
* initial additions * refactoring based on tobrun review
- Loading branch information
Langston Smith
authored
Mar 29, 2019
1 parent
5933ab2
commit c7f4d90
Showing
9 changed files
with
208 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
158 changes: 158 additions & 0 deletions
158
...o/src/main/java/com/mapbox/mapboxandroiddemo/examples/dds/SymbolSwitchOnZoomActivity.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,158 @@ | ||
package com.mapbox.mapboxandroiddemo.examples.dds; | ||
|
||
import android.os.Bundle; | ||
import android.support.annotation.NonNull; | ||
import android.support.v7.app.AppCompatActivity; | ||
import android.widget.Toast; | ||
|
||
import com.mapbox.geojson.Feature; | ||
import com.mapbox.geojson.FeatureCollection; | ||
import com.mapbox.geojson.Point; | ||
import com.mapbox.mapboxandroiddemo.R; | ||
import com.mapbox.mapboxsdk.Mapbox; | ||
import com.mapbox.mapboxsdk.maps.MapView; | ||
import com.mapbox.mapboxsdk.maps.MapboxMap; | ||
import com.mapbox.mapboxsdk.maps.OnMapReadyCallback; | ||
import com.mapbox.mapboxsdk.maps.Style; | ||
import com.mapbox.mapboxsdk.style.layers.SymbolLayer; | ||
import com.mapbox.mapboxsdk.style.sources.GeoJsonSource; | ||
import com.mapbox.mapboxsdk.utils.BitmapUtils; | ||
|
||
import static com.mapbox.mapboxsdk.style.expressions.Expression.literal; | ||
import static com.mapbox.mapboxsdk.style.expressions.Expression.step; | ||
import static com.mapbox.mapboxsdk.style.expressions.Expression.stop; | ||
import static com.mapbox.mapboxsdk.style.expressions.Expression.zoom; | ||
import static com.mapbox.mapboxsdk.style.layers.PropertyFactory.iconAllowOverlap; | ||
import static com.mapbox.mapboxsdk.style.layers.PropertyFactory.iconIgnorePlacement; | ||
import static com.mapbox.mapboxsdk.style.layers.PropertyFactory.iconImage; | ||
|
||
/** | ||
* Use the SymbolLayer's setMinZoom and setMaxZoom methods to create the effect of SymbolLayer icons switching | ||
* based on the map camera's zoom level. | ||
*/ | ||
public class SymbolSwitchOnZoomActivity extends AppCompatActivity implements OnMapReadyCallback { | ||
|
||
private static final float ZOOM_LEVEL_FOR_SWITCH = 12; | ||
private static final String BLUE_PERSON_ICON_ID = "blue-car-icon-marker-icon-id"; | ||
private static final String BLUE_PIN_ICON_ID = "blue-marker-icon-marker-icon-id"; | ||
private MapView mapView; | ||
|
||
@Override | ||
protected void onCreate(Bundle savedInstanceState) { | ||
super.onCreate(savedInstanceState); | ||
|
||
// Mapbox access token is configured here. This needs to be called either in your application | ||
// object or in the same activity which contains the mapview. | ||
Mapbox.getInstance(this, getString(R.string.access_token)); | ||
|
||
// This contains the MapView in XML and needs to be called after the access token is configured. | ||
setContentView(R.layout.activity_zoom_based_icon_switch); | ||
|
||
mapView = findViewById(R.id.mapView); | ||
mapView.onCreate(savedInstanceState); | ||
mapView.getMapAsync(this); | ||
} | ||
|
||
@Override | ||
public void onMapReady(final MapboxMap mapboxMap) { | ||
|
||
mapboxMap.setStyle(new Style.Builder().fromUrl(Style.OUTDOORS) | ||
|
||
// Add images to the map so that the SymbolLayers can reference the images. | ||
.withImage(BLUE_PERSON_ICON_ID, BitmapUtils.getBitmapFromDrawable( | ||
getResources().getDrawable(R.drawable.ic_person))) | ||
.withImage(BLUE_PIN_ICON_ID, BitmapUtils.getBitmapFromDrawable( | ||
getResources().getDrawable(R.drawable.blue_marker))) | ||
|
||
// Add random data to the GeoJsonSource and then add the GeoJsonSource to the map | ||
.withSource(new GeoJsonSource("source-id", | ||
FeatureCollection.fromFeatures(new Feature[] { | ||
Feature.fromGeometry(Point.fromLngLat( | ||
9.205394983291626, | ||
45.47661043757903)), | ||
Feature.fromGeometry(Point.fromLngLat( | ||
9.223880767822266, | ||
45.47623240235297)), | ||
Feature.fromGeometry(Point.fromLngLat( | ||
9.15530204772949, | ||
45.4706650227671)), | ||
Feature.fromGeometry(Point.fromLngLat( | ||
9.153714179992676, | ||
45.48625229963004)), | ||
Feature.fromGeometry(Point.fromLngLat( | ||
9.158306121826172, | ||
45.482731998239636)), | ||
Feature.fromGeometry(Point.fromLngLat( | ||
9.188523888587952, | ||
45.4923746929562)), | ||
Feature.fromGeometry(Point.fromLngLat( | ||
9.20929491519928, | ||
45.45314676076135)), | ||
Feature.fromGeometry(Point.fromLngLat( | ||
9.177778959274292, | ||
45.45569808340158)) | ||
}) | ||
)), new Style.OnStyleLoaded() { | ||
@Override | ||
public void onStyleLoaded(@NonNull Style style) { | ||
|
||
// Create a SymbolLayer and use the {@link com.mapbox.mapboxsdk.style.expressions.Expression.step()} | ||
// to adjust the SymbolLayer icon based on the zoom level. The blue person icon is set as the default | ||
// icon and then a step is used to switch to the blue person icon at a certain map camera zoom level. | ||
SymbolLayer singleLayer = new SymbolLayer("symbol-layer-id", "source-id"); | ||
singleLayer.setProperties( | ||
iconImage(step(zoom(), literal(BLUE_PERSON_ICON_ID), | ||
stop(ZOOM_LEVEL_FOR_SWITCH, BLUE_PIN_ICON_ID))), | ||
iconIgnorePlacement(true), | ||
iconAllowOverlap(true)); | ||
style.addLayer(singleLayer); | ||
|
||
Toast.makeText(SymbolSwitchOnZoomActivity.this, | ||
R.string.zoom_map_in_and_out_icon_switch_instruction, Toast.LENGTH_SHORT).show(); | ||
} | ||
} | ||
); | ||
} | ||
|
||
@Override | ||
public void onStart() { | ||
super.onStart(); | ||
mapView.onStart(); | ||
} | ||
|
||
@Override | ||
public void onResume() { | ||
super.onResume(); | ||
mapView.onResume(); | ||
} | ||
|
||
@Override | ||
public void onPause() { | ||
super.onPause(); | ||
mapView.onPause(); | ||
} | ||
|
||
@Override | ||
public void onStop() { | ||
super.onStop(); | ||
mapView.onStop(); | ||
} | ||
|
||
@Override | ||
public void onLowMemory() { | ||
super.onLowMemory(); | ||
mapView.onLowMemory(); | ||
} | ||
|
||
@Override | ||
protected void onDestroy() { | ||
super.onDestroy(); | ||
mapView.onDestroy(); | ||
} | ||
|
||
@Override | ||
protected void onSaveInstanceState(Bundle outState) { | ||
super.onSaveInstanceState(outState); | ||
mapView.onSaveInstanceState(outState); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<vector xmlns:android="http://schemas.android.com/apk/res/android" | ||
android:width="24dp" | ||
android:height="24dp" | ||
android:viewportWidth="24.0" | ||
android:viewportHeight="24.0"> | ||
<path | ||
android:fillColor="#2096F3" | ||
android:pathData="M12,12c2.21,0 4,-1.79 4,-4s-1.79,-4 -4,-4 -4,1.79 -4,4 1.79,4 4,4zM12,14c-2.67,0 -8,1.34 -8,4v2h16v-2c0,-2.66 -5.33,-4 -8,-4z"/> | ||
</vector> |
18 changes: 18 additions & 0 deletions
18
MapboxAndroidDemo/src/main/res/layout/activity_zoom_based_icon_switch.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<FrameLayout | ||
xmlns:android="http://schemas.android.com/apk/res/android" | ||
xmlns:mapbox="http://schemas.android.com/apk/res-auto" | ||
xmlns:tools="http://schemas.android.com/tools" | ||
android:layout_width="match_parent" | ||
android:layout_height="match_parent" | ||
tools:context=".examples.dds.SymbolSwitchOnZoomActivity"> | ||
|
||
<com.mapbox.mapboxsdk.maps.MapView | ||
android:id="@+id/mapView" | ||
android:layout_width="match_parent" | ||
android:layout_height="match_parent" | ||
mapbox:mapbox_cameraTargetLat="45.459145" | ||
mapbox:mapbox_cameraTargetLng="9.186176" | ||
mapbox:mapbox_cameraZoom="10.662822"/> | ||
|
||
</FrameLayout> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters