|
| 1 | +package com.mapbox.mapboxandroiddemo.examples.dds; |
| 2 | + |
| 3 | +import android.os.Bundle; |
| 4 | +import android.support.annotation.NonNull; |
| 5 | +import android.support.v7.app.AppCompatActivity; |
| 6 | +import android.widget.Toast; |
| 7 | + |
| 8 | +import com.mapbox.geojson.Feature; |
| 9 | +import com.mapbox.geojson.FeatureCollection; |
| 10 | +import com.mapbox.geojson.Point; |
| 11 | +import com.mapbox.mapboxandroiddemo.R; |
| 12 | +import com.mapbox.mapboxsdk.Mapbox; |
| 13 | +import com.mapbox.mapboxsdk.maps.MapView; |
| 14 | +import com.mapbox.mapboxsdk.maps.MapboxMap; |
| 15 | +import com.mapbox.mapboxsdk.maps.OnMapReadyCallback; |
| 16 | +import com.mapbox.mapboxsdk.maps.Style; |
| 17 | +import com.mapbox.mapboxsdk.style.layers.SymbolLayer; |
| 18 | +import com.mapbox.mapboxsdk.style.sources.GeoJsonSource; |
| 19 | +import com.mapbox.mapboxsdk.utils.BitmapUtils; |
| 20 | + |
| 21 | +import static com.mapbox.mapboxsdk.style.expressions.Expression.literal; |
| 22 | +import static com.mapbox.mapboxsdk.style.expressions.Expression.step; |
| 23 | +import static com.mapbox.mapboxsdk.style.expressions.Expression.stop; |
| 24 | +import static com.mapbox.mapboxsdk.style.expressions.Expression.zoom; |
| 25 | +import static com.mapbox.mapboxsdk.style.layers.PropertyFactory.iconAllowOverlap; |
| 26 | +import static com.mapbox.mapboxsdk.style.layers.PropertyFactory.iconIgnorePlacement; |
| 27 | +import static com.mapbox.mapboxsdk.style.layers.PropertyFactory.iconImage; |
| 28 | + |
| 29 | +/** |
| 30 | + * Use the SymbolLayer's setMinZoom and setMaxZoom methods to create the effect of SymbolLayer icons switching |
| 31 | + * based on the map camera's zoom level. |
| 32 | + */ |
| 33 | +public class SymbolSwitchOnZoomActivity extends AppCompatActivity implements OnMapReadyCallback { |
| 34 | + |
| 35 | + private static final float ZOOM_LEVEL_FOR_SWITCH = 12; |
| 36 | + private static final String BLUE_PERSON_ICON_ID = "blue-car-icon-marker-icon-id"; |
| 37 | + private static final String BLUE_PIN_ICON_ID = "blue-marker-icon-marker-icon-id"; |
| 38 | + private MapView mapView; |
| 39 | + |
| 40 | + @Override |
| 41 | + protected void onCreate(Bundle savedInstanceState) { |
| 42 | + super.onCreate(savedInstanceState); |
| 43 | + |
| 44 | + // Mapbox access token is configured here. This needs to be called either in your application |
| 45 | + // object or in the same activity which contains the mapview. |
| 46 | + Mapbox.getInstance(this, getString(R.string.access_token)); |
| 47 | + |
| 48 | + // This contains the MapView in XML and needs to be called after the access token is configured. |
| 49 | + setContentView(R.layout.activity_zoom_based_icon_switch); |
| 50 | + |
| 51 | + mapView = findViewById(R.id.mapView); |
| 52 | + mapView.onCreate(savedInstanceState); |
| 53 | + mapView.getMapAsync(this); |
| 54 | + } |
| 55 | + |
| 56 | + @Override |
| 57 | + public void onMapReady(final MapboxMap mapboxMap) { |
| 58 | + |
| 59 | + mapboxMap.setStyle(new Style.Builder().fromUrl(Style.OUTDOORS) |
| 60 | + |
| 61 | + // Add images to the map so that the SymbolLayers can reference the images. |
| 62 | + .withImage(BLUE_PERSON_ICON_ID, BitmapUtils.getBitmapFromDrawable( |
| 63 | + getResources().getDrawable(R.drawable.ic_person))) |
| 64 | + .withImage(BLUE_PIN_ICON_ID, BitmapUtils.getBitmapFromDrawable( |
| 65 | + getResources().getDrawable(R.drawable.blue_marker))) |
| 66 | + |
| 67 | + // Add random data to the GeoJsonSource and then add the GeoJsonSource to the map |
| 68 | + .withSource(new GeoJsonSource("source-id", |
| 69 | + FeatureCollection.fromFeatures(new Feature[] { |
| 70 | + Feature.fromGeometry(Point.fromLngLat( |
| 71 | + 9.205394983291626, |
| 72 | + 45.47661043757903)), |
| 73 | + Feature.fromGeometry(Point.fromLngLat( |
| 74 | + 9.223880767822266, |
| 75 | + 45.47623240235297)), |
| 76 | + Feature.fromGeometry(Point.fromLngLat( |
| 77 | + 9.15530204772949, |
| 78 | + 45.4706650227671)), |
| 79 | + Feature.fromGeometry(Point.fromLngLat( |
| 80 | + 9.153714179992676, |
| 81 | + 45.48625229963004)), |
| 82 | + Feature.fromGeometry(Point.fromLngLat( |
| 83 | + 9.158306121826172, |
| 84 | + 45.482731998239636)), |
| 85 | + Feature.fromGeometry(Point.fromLngLat( |
| 86 | + 9.188523888587952, |
| 87 | + 45.4923746929562)), |
| 88 | + Feature.fromGeometry(Point.fromLngLat( |
| 89 | + 9.20929491519928, |
| 90 | + 45.45314676076135)), |
| 91 | + Feature.fromGeometry(Point.fromLngLat( |
| 92 | + 9.177778959274292, |
| 93 | + 45.45569808340158)) |
| 94 | + }) |
| 95 | + )), new Style.OnStyleLoaded() { |
| 96 | + @Override |
| 97 | + public void onStyleLoaded(@NonNull Style style) { |
| 98 | + |
| 99 | + // Create a SymbolLayer and use the {@link com.mapbox.mapboxsdk.style.expressions.Expression.step()} |
| 100 | + // to adjust the SymbolLayer icon based on the zoom level. The blue person icon is set as the default |
| 101 | + // icon and then a step is used to switch to the blue person icon at a certain map camera zoom level. |
| 102 | + SymbolLayer singleLayer = new SymbolLayer("symbol-layer-id", "source-id"); |
| 103 | + singleLayer.setProperties( |
| 104 | + iconImage(step(zoom(), literal(BLUE_PERSON_ICON_ID), |
| 105 | + stop(ZOOM_LEVEL_FOR_SWITCH, BLUE_PIN_ICON_ID))), |
| 106 | + iconIgnorePlacement(true), |
| 107 | + iconAllowOverlap(true)); |
| 108 | + style.addLayer(singleLayer); |
| 109 | + |
| 110 | + Toast.makeText(SymbolSwitchOnZoomActivity.this, |
| 111 | + R.string.zoom_map_in_and_out_icon_switch_instruction, Toast.LENGTH_SHORT).show(); |
| 112 | + } |
| 113 | + } |
| 114 | + ); |
| 115 | + } |
| 116 | + |
| 117 | + @Override |
| 118 | + public void onStart() { |
| 119 | + super.onStart(); |
| 120 | + mapView.onStart(); |
| 121 | + } |
| 122 | + |
| 123 | + @Override |
| 124 | + public void onResume() { |
| 125 | + super.onResume(); |
| 126 | + mapView.onResume(); |
| 127 | + } |
| 128 | + |
| 129 | + @Override |
| 130 | + public void onPause() { |
| 131 | + super.onPause(); |
| 132 | + mapView.onPause(); |
| 133 | + } |
| 134 | + |
| 135 | + @Override |
| 136 | + public void onStop() { |
| 137 | + super.onStop(); |
| 138 | + mapView.onStop(); |
| 139 | + } |
| 140 | + |
| 141 | + @Override |
| 142 | + public void onLowMemory() { |
| 143 | + super.onLowMemory(); |
| 144 | + mapView.onLowMemory(); |
| 145 | + } |
| 146 | + |
| 147 | + @Override |
| 148 | + protected void onDestroy() { |
| 149 | + super.onDestroy(); |
| 150 | + mapView.onDestroy(); |
| 151 | + } |
| 152 | + |
| 153 | + @Override |
| 154 | + protected void onSaveInstanceState(Bundle outState) { |
| 155 | + super.onSaveInstanceState(outState); |
| 156 | + mapView.onSaveInstanceState(outState); |
| 157 | + } |
| 158 | +} |
0 commit comments