diff --git a/MapboxAndroidDemo/src/global/java/com/mapbox/mapboxandroiddemo/MainActivity.java b/MapboxAndroidDemo/src/global/java/com/mapbox/mapboxandroiddemo/MainActivity.java index c9fda982d..041e75e1b 100644 --- a/MapboxAndroidDemo/src/global/java/com/mapbox/mapboxandroiddemo/MainActivity.java +++ b/MapboxAndroidDemo/src/global/java/com/mapbox/mapboxandroiddemo/MainActivity.java @@ -114,6 +114,7 @@ import com.mapbox.mapboxandroiddemo.examples.labs.SpinningSymbolLayerIconActivity; import com.mapbox.mapboxandroiddemo.examples.labs.SymbolLayerMapillaryActivity; import com.mapbox.mapboxandroiddemo.examples.labs.ValueAnimatorIconAnimationActivity; +import com.mapbox.mapboxandroiddemo.examples.labs.VibrateOnPinDropKotlinActivity; import com.mapbox.mapboxandroiddemo.examples.location.KotlinLocationComponentActivity; import com.mapbox.mapboxandroiddemo.examples.location.LocationChangeListeningActivity; import com.mapbox.mapboxandroiddemo.examples.location.LocationComponentActivity; @@ -1414,6 +1415,14 @@ private void initializeModels() { null, R.string.activity_lab_baseball_spray_chart_url, true, BuildConfig.MIN_SDK_VERSION)); + exampleItemModels.add(new ExampleItemModel( + R.id.nav_lab, + R.string.activity_lab_vibrate_on_pin_drop_title, + R.string.activity_lab_vibrate_on_pin_drop_description, + null, + new Intent(MainActivity.this, VibrateOnPinDropKotlinActivity.class), + R.string.activity_lab_vibrate_on_pin_drop_url, true, BuildConfig.MIN_SDK_VERSION)); + exampleItemModels.add(new ExampleItemModel( R.id.nav_dds, R.string.activity_dds_geojson_line_title, diff --git a/MapboxAndroidDemo/src/main/AndroidManifest.xml b/MapboxAndroidDemo/src/main/AndroidManifest.xml index 94ca810e3..b7205e84d 100644 --- a/MapboxAndroidDemo/src/main/AndroidManifest.xml +++ b/MapboxAndroidDemo/src/main/AndroidManifest.xml @@ -11,6 +11,7 @@ + + + + diff --git a/MapboxAndroidDemo/src/main/java/com/mapbox/mapboxandroiddemo/examples/labs/VibrateOnPinDropKotlinActivity.kt b/MapboxAndroidDemo/src/main/java/com/mapbox/mapboxandroiddemo/examples/labs/VibrateOnPinDropKotlinActivity.kt new file mode 100644 index 000000000..962c4266f --- /dev/null +++ b/MapboxAndroidDemo/src/main/java/com/mapbox/mapboxandroiddemo/examples/labs/VibrateOnPinDropKotlinActivity.kt @@ -0,0 +1,136 @@ +package com.mapbox.mapboxandroiddemo.examples.labs + +import android.os.Build +import android.os.Bundle +import android.os.VibrationEffect +import android.os.Vibrator +import android.widget.Toast +import androidx.appcompat.app.AppCompatActivity +import com.mapbox.geojson.Point +import com.mapbox.mapboxandroiddemo.R +import com.mapbox.mapboxsdk.Mapbox +import com.mapbox.mapboxsdk.geometry.LatLng +import com.mapbox.mapboxsdk.maps.MapboxMap +import com.mapbox.mapboxsdk.maps.OnMapReadyCallback +import com.mapbox.mapboxsdk.maps.Style +import com.mapbox.mapboxsdk.style.layers.PropertyFactory.iconAllowOverlap +import com.mapbox.mapboxsdk.style.layers.PropertyFactory.iconIgnorePlacement +import com.mapbox.mapboxsdk.style.layers.PropertyFactory.iconImage +import com.mapbox.mapboxsdk.style.layers.SymbolLayer +import com.mapbox.mapboxsdk.style.sources.GeoJsonSource +import com.mapbox.mapboxsdk.utils.BitmapUtils +import kotlinx.android.synthetic.main.activity_lab_vibrate_on_pin_drop_kotlin.* + +/** + * Vibrate the Android device when a pin icon is dropped on the map. + */ +class VibrateOnPinDropKotlinActivity : AppCompatActivity(), OnMapReadyCallback, MapboxMap.OnMapLongClickListener { + + var mapboxMap: MapboxMap? = null + + companion object { + private const val SOURCE_ID = "SOURCE_ID" + private const val ICON_ID = "ICON_ID" + private const val LAYER_ID = "LAYER_ID" + private const val VIBRATE_SPEED_ONE_HUNDRED_MILLISECONDS = 100L + } + + override fun onCreate(savedInstanceState: Bundle?) { + 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_lab_vibrate_on_pin_drop_kotlin) + mapView?.onCreate(savedInstanceState) + mapView?.getMapAsync(this) + } + + override fun onMapReady(mapboxMap: MapboxMap) { + mapboxMap.setStyle(Style.Builder().fromUri(Style.OUTDOORS) + + // Add the SymbolLayer icon image to the map style + .withImage(ICON_ID, BitmapUtils.getDrawableFromRes( + this@VibrateOnPinDropKotlinActivity, + R.drawable.mapbox_marker_icon_default)!!) + + // Adding a GeoJson source for the SymbolLayer icons. + .withSource(GeoJsonSource(SOURCE_ID)) + + // Adding the actual SymbolLayer to the map style. + .withLayer(SymbolLayer(LAYER_ID, SOURCE_ID) + .withProperties( + iconImage(ICON_ID), + iconAllowOverlap(true), + iconIgnorePlacement(true)) + )) { + this.mapboxMap = mapboxMap + mapboxMap.addOnMapLongClickListener(this@VibrateOnPinDropKotlinActivity) + Toast.makeText(this@VibrateOnPinDropKotlinActivity, + R.string.long_press_to_add_pin, Toast.LENGTH_SHORT).show() + } + } + + override fun onMapLongClick(point: LatLng): Boolean { + mapboxMap?.getStyle { + + // Update the SymbolLayer icon's source to move the map pin + val geoJsonSource = it.getSourceAs(SOURCE_ID) + geoJsonSource?.setGeoJson(Point.fromLngLat(point.longitude, point.latitude)) + + // Vibrate the device + vibrate() + } + return true + } + + /** + * Vibrate the device + */ + private fun vibrate() { + val vibrator = getSystemService(VIBRATOR_SERVICE) as Vibrator + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { + vibrator.vibrate(VibrationEffect.createOneShot(VIBRATE_SPEED_ONE_HUNDRED_MILLISECONDS, + VibrationEffect.DEFAULT_AMPLITUDE)) + } else { + vibrator.vibrate(VIBRATE_SPEED_ONE_HUNDRED_MILLISECONDS) + } + } + + // Add the mapView lifecycle to the activity's lifecycle methods + public override fun onResume() { + super.onResume() + mapView?.onResume() + } + + override fun onStart() { + super.onStart() + mapView?.onStart() + } + + override fun onStop() { + super.onStop() + mapView?.onStop() + } + + public override fun onPause() { + super.onPause() + mapView?.onPause() + } + + override fun onLowMemory() { + super.onLowMemory() + mapView?.onLowMemory() + } + + override fun onDestroy() { + super.onDestroy() + mapView?.onDestroy() + } + + override fun onSaveInstanceState(outState: Bundle) { + super.onSaveInstanceState(outState) + mapView?.onSaveInstanceState(outState) + } +} \ No newline at end of file diff --git a/MapboxAndroidDemo/src/main/res/layout/activity_lab_vibrate_on_pin_drop_kotlin.xml b/MapboxAndroidDemo/src/main/res/layout/activity_lab_vibrate_on_pin_drop_kotlin.xml new file mode 100644 index 000000000..2c25571ac --- /dev/null +++ b/MapboxAndroidDemo/src/main/res/layout/activity_lab_vibrate_on_pin_drop_kotlin.xml @@ -0,0 +1,17 @@ + + + + + + \ No newline at end of file diff --git a/MapboxAndroidDemo/src/main/res/values/activity_strings.xml b/MapboxAndroidDemo/src/main/res/values/activity_strings.xml index 417ae992b..f41577bca 100644 --- a/MapboxAndroidDemo/src/main/res/values/activity_strings.xml +++ b/MapboxAndroidDemo/src/main/res/values/activity_strings.xml @@ -475,6 +475,9 @@ Tap on a circle and then marker to toggle. Tap elsewhere to reset to circles. + + Long press on the map to add a pin icon + Miles diff --git a/MapboxAndroidDemo/src/main/res/values/descriptions_strings.xml b/MapboxAndroidDemo/src/main/res/values/descriptions_strings.xml index 328cac9b2..e0cbd4e39 100644 --- a/MapboxAndroidDemo/src/main/res/values/descriptions_strings.xml +++ b/MapboxAndroidDemo/src/main/res/values/descriptions_strings.xml @@ -149,6 +149,7 @@ Use the Android system\'s SharedPreferences to save and retrieve information such as coordinates. Use the Android system\'s fingerprint unlock to reveal "personal" data on a map. Use the Maps SDK and filters to explore baseball data. + Vibrate the device when an icon is dropped on the map for a more interactive map experience. Show an accurate and government-approved China map in your app using the Mapbox Maps SDK. Use the China plugin to determine whether or not the device is inside of China. Load a China style if the device is in China. Load a global/.com style if not. diff --git a/MapboxAndroidDemo/src/main/res/values/titles_strings.xml b/MapboxAndroidDemo/src/main/res/values/titles_strings.xml index cf8c0466c..6379e5423 100644 --- a/MapboxAndroidDemo/src/main/res/values/titles_strings.xml +++ b/MapboxAndroidDemo/src/main/res/values/titles_strings.xml @@ -152,4 +152,5 @@ Style attribution Saving to SharedPreferences Biometric fingerprint + Vibrate device on pin drop diff --git a/MapboxAndroidDemo/src/main/res/values/urls_strings.xml b/MapboxAndroidDemo/src/main/res/values/urls_strings.xml index 9ae7faa32..5042bd8f7 100644 --- a/MapboxAndroidDemo/src/main/res/values/urls_strings.xml +++ b/MapboxAndroidDemo/src/main/res/values/urls_strings.xml @@ -147,6 +147,7 @@ https://i.imgur.com/znxAhDG.png https://i.imgur.com/iQZzMIR.png https://i.imgur.com/1j7WVoO.png + https://i.imgur.com/0FgIP5n.png https://i.imgur.com/KwoEynZ.png https://i.imgur.com/fIFWqJu.png https://i.imgur.com/XBS1WAn.png