summaryrefslogtreecommitdiff
path: root/platform/android/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp/activity/camera/LatLngBoundsActivity.kt
blob: b83ba5c4cde96eb5c62a735dc5c62d69ce0dcf92 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
package com.mapbox.mapboxsdk.testapp.activity.camera

import android.content.Context
import android.os.Bundle
import android.support.design.widget.BottomSheetBehavior
import android.support.v7.app.AppCompatActivity
import android.view.View
import com.mapbox.geojson.FeatureCollection
import com.mapbox.geojson.FeatureCollection.fromJson
import com.mapbox.geojson.Point
import com.mapbox.mapboxsdk.geometry.LatLng
import com.mapbox.mapboxsdk.geometry.LatLngBounds
import com.mapbox.mapboxsdk.maps.MapboxMap
import com.mapbox.mapboxsdk.maps.Style
import com.mapbox.mapboxsdk.style.layers.Property.ICON_ANCHOR_CENTER
import com.mapbox.mapboxsdk.style.layers.PropertyFactory.*
import com.mapbox.mapboxsdk.style.layers.SymbolLayer
import com.mapbox.mapboxsdk.style.sources.GeoJsonSource
import com.mapbox.mapboxsdk.testapp.R
import com.mapbox.mapboxsdk.testapp.utils.GeoParseUtil.loadStringFromAssets
import com.mapbox.mapboxsdk.utils.BitmapUtils
import java.net.URISyntaxException
import kotlinx.android.synthetic.main.activity_latlngbounds.*

/**
 * Test activity showcasing using the LatLngBounds camera API.
 */
class LatLngBoundsActivity : AppCompatActivity() {

  private lateinit var mapboxMap: MapboxMap
  private lateinit var bottomSheetBehavior: BottomSheetBehavior<*>
  private lateinit var bounds: LatLngBounds

  private val peekHeight by lazy {
    375.toPx(this) // 375dp
  }

  private val additionalPadding by lazy {
    32.toPx(this) // 32dp
  }

  override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_latlngbounds)
    initMapView(savedInstanceState)
  }

  private fun initMapView(savedInstanceState: Bundle?) {
    mapView.onCreate(savedInstanceState)
    mapView.getMapAsync { map ->
      mapboxMap = map

      val featureCollection: FeatureCollection = fromJson(loadStringFromAssets(this, "points-sf.geojson"))
      bounds = createBounds(featureCollection)

      map.getCameraForLatLngBounds(bounds, createPadding(peekHeight))?.let {
        map.cameraPosition = it
      }

      try {
        loadStyle(featureCollection)
      } catch (e: URISyntaxException) {
        e.printStackTrace()
      }
    }
  }

  private fun loadStyle(featureCollection: FeatureCollection) {
    mapboxMap.setStyle(Style.Builder()
      .fromUri(Style.MAPBOX_STREETS)
      .withLayer(SymbolLayer("symbol", "symbol")
        .withProperties(
          iconAllowOverlap(true),
          iconIgnorePlacement(true),
          iconImage("icon"),
          iconAnchor(ICON_ANCHOR_CENTER)
        )
      )
      .withSource(GeoJsonSource("symbol", featureCollection))
      .withImage("icon", BitmapUtils.getDrawableFromRes(this@LatLngBoundsActivity, R.drawable.ic_android)!!)
    ) {
      initBottomSheet()
      fab.setOnClickListener { bottomSheetBehavior.state = BottomSheetBehavior.STATE_EXPANDED }
    }
  }

  private fun initBottomSheet() {
    bottomSheetBehavior = BottomSheetBehavior.from(bottomSheet)
    bottomSheetBehavior.setBottomSheetCallback(object : BottomSheetBehavior.BottomSheetCallback() {
      override fun onSlide(bottomSheet: View, slideOffset: Float) {
        val offset = convertSlideOffset(slideOffset)
        val bottomPadding = (peekHeight * offset).toInt()

        mapboxMap.getCameraForLatLngBounds(bounds, createPadding(bottomPadding))?.let {
          mapboxMap.cameraPosition = it
        }
      }

      override fun onStateChanged(bottomSheet: View, newState: Int) {
        // no-op
      }
    })
  }

  // slideOffset ranges from NaN to -1.0, range from 1.0 to 0 instead
  fun convertSlideOffset(slideOffset: Float): Float {
    return if (slideOffset.equals(Float.NaN)) {
      1.0f
    } else {
      1 + slideOffset
    }
  }

  fun createPadding(bottomPadding: Int): IntArray {
    return intArrayOf(additionalPadding, additionalPadding, additionalPadding, bottomPadding)
  }

  private fun createBounds(featureCollection: FeatureCollection): LatLngBounds {
    val boundsBuilder = LatLngBounds.Builder()
    featureCollection.features()?.let {
      for (feature in it) {
        val point = feature.geometry() as Point
        boundsBuilder.include(LatLng(point.latitude(), point.longitude()))
      }
    }
    return boundsBuilder.build()
  }

  override fun onStart() {
    super.onStart()
    mapView.onStart()
  }

  override fun onResume() {
    super.onResume()
    mapView.onResume()
  }

  override fun onPause() {
    super.onPause()
    mapView.onPause()
  }

  override fun onStop() {
    super.onStop()
    mapView.onStop()
  }

  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)
  }
}

fun Int.toPx(context: Context): Int = (this * context.resources.displayMetrics.density).toInt()