|
| 1 | +package com.mapbox.navigation.examples.standalone.isochrone |
| 2 | + |
| 3 | +import android.annotation.SuppressLint |
| 4 | +import android.graphics.Color |
| 5 | +import android.os.Bundle |
| 6 | +import android.util.Log |
| 7 | +import androidx.appcompat.app.AppCompatActivity |
| 8 | +import androidx.lifecycle.lifecycleScope |
| 9 | +import com.mapbox.api.isochrone.IsochroneCriteria |
| 10 | +import com.mapbox.api.isochrone.MapboxIsochrone |
| 11 | +import com.mapbox.geojson.Point |
| 12 | +import com.mapbox.maps.CameraOptions |
| 13 | +import com.mapbox.maps.MapView |
| 14 | +import com.mapbox.maps.MapboxMap |
| 15 | +import com.mapbox.maps.Style |
| 16 | +import com.mapbox.maps.extension.style.layers.addLayer |
| 17 | +import com.mapbox.maps.extension.style.layers.generated.fillLayer |
| 18 | +import com.mapbox.maps.extension.style.sources.addSource |
| 19 | +import com.mapbox.maps.extension.style.sources.generated.GeoJsonSource |
| 20 | +import com.mapbox.maps.extension.style.sources.generated.geoJsonSource |
| 21 | +import com.mapbox.maps.extension.style.sources.getSourceAs |
| 22 | +import com.mapbox.navigation.examples.R |
| 23 | +import com.mapbox.navigation.examples.databinding.ActivityIsochroneRangePreviewBinding |
| 24 | +import kotlinx.coroutines.Dispatchers |
| 25 | +import kotlinx.coroutines.async |
| 26 | +import kotlinx.coroutines.launch |
| 27 | + |
| 28 | +class IsochroneRangeInMeters : AppCompatActivity() { |
| 29 | + |
| 30 | + private val TAG = "IsochroneRangeInMeters" |
| 31 | + |
| 32 | + /** |
| 33 | + * Bindings to the example layout. |
| 34 | + */ |
| 35 | + private lateinit var binding: ActivityIsochroneRangePreviewBinding |
| 36 | + |
| 37 | + /** |
| 38 | + * Mapbox Maps entry point obtained from the [MapView]. |
| 39 | + * You need to get a new reference to this object whenever the [MapView] is recreated. |
| 40 | + */ |
| 41 | + private var mapboxMap: MapboxMap? = null |
| 42 | + |
| 43 | + @SuppressLint("MissingPermission") |
| 44 | + override fun onCreate(savedInstanceState: Bundle?) { |
| 45 | + super.onCreate(savedInstanceState) |
| 46 | + binding = ActivityIsochroneRangePreviewBinding.inflate(layoutInflater) |
| 47 | + setContentView(binding.root) |
| 48 | + mapboxMap = binding.mapView.getMapboxMap() |
| 49 | + initStyle() |
| 50 | + loadIsochrone() |
| 51 | + } |
| 52 | + |
| 53 | + override fun onDestroy() { |
| 54 | + super.onDestroy() |
| 55 | + mapboxMap = null |
| 56 | + } |
| 57 | + |
| 58 | + @SuppressLint("MissingPermission") |
| 59 | + private fun initStyle() { |
| 60 | + mapboxMap?.loadStyle( |
| 61 | + "mapbox://styles/mapbox/light-v10" |
| 62 | + ) { style: Style -> |
| 63 | + // Create the source for the Isochrone. |
| 64 | + geoJsonSource("myIsochroneSource").apply { |
| 65 | + style.addSource(this) |
| 66 | + } |
| 67 | + // Create the layer for the Isochrone. |
| 68 | + fillLayer("myIsochroneLayer", "myIsochroneSource") { |
| 69 | + fillOpacity(.5) |
| 70 | + fillColor(Color.parseColor("#ffcc00")) |
| 71 | + }.apply { |
| 72 | + style.addLayer(this) |
| 73 | + } |
| 74 | + |
| 75 | + val cameraOptions = CameraOptions.Builder().center(Point.fromLngLat(-73.990593, 40.740121)).zoom(12.0).build() |
| 76 | + mapboxMap?.setCamera(cameraOptions) |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + private fun loadIsochrone() { |
| 81 | + val mapboxIsochrone = MapboxIsochrone.builder() |
| 82 | + .accessToken(getString(R.string.mapbox_access_token)) |
| 83 | + .coordinates(Point.fromLngLat(-73.990593, 40.740121)) |
| 84 | + .addContoursMeters(1000) |
| 85 | + .profile(IsochroneCriteria.PROFILE_DRIVING_TRAFFIC) |
| 86 | + .polygons(true) |
| 87 | + .build() |
| 88 | + |
| 89 | + lifecycleScope.launch { |
| 90 | + val deferredResponse = async(Dispatchers.IO) { |
| 91 | + mapboxIsochrone.executeCall() |
| 92 | + } |
| 93 | + val response = deferredResponse.await() |
| 94 | + if (response.isSuccessful) { |
| 95 | + val featureCollection = response.body() |
| 96 | + if (featureCollection != null) { |
| 97 | + mapboxMap?.getStyle { style -> |
| 98 | + val source = style.getSourceAs<GeoJsonSource>("myIsochroneSource") |
| 99 | + source?.featureCollection(featureCollection, "") |
| 100 | + } |
| 101 | + } |
| 102 | + } else { |
| 103 | + Log.d(TAG, "Failed to get Isochrone, ${response.errorBody()}") |
| 104 | + } |
| 105 | + } |
| 106 | + } |
| 107 | +} |
0 commit comments