Skip to content

Commit c57145d

Browse files
author
Martin Dinh
committed
Merge branch '56-pass-zoom-level-to-tile-request-and-zoom-to-incremental-padding' into 'master'
Resolve "Pass zoom level to tile request and zoom to incremental padding" Closes #56 See merge request pace/mobile/android/pace-cloud-sdk!45
2 parents b51f6a8 + e236a00 commit c57145d

File tree

4 files changed

+65
-42
lines changed

4 files changed

+65
-42
lines changed

library/src/main/java/cloud/pace/sdk/poikit/POIKit.kt

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@ object POIKit : CloudSDKKoinComponent, LifecycleObserver {
4141
private val navigationApi: NavigationApiClient by inject()
4242
private val addressSearchApi: AddressSearchClient by inject()
4343
private val locationProvider: LocationProvider by inject()
44-
var maxPoiSearchBoxSize = 15000.0
4544

4645
fun startLocationListener(): LocationProvider {
4746
ProcessLifecycleOwner.get().lifecycle.addObserver(this)
@@ -63,8 +62,8 @@ object POIKit : CloudSDKKoinComponent, LifecycleObserver {
6362
}
6463

6564
@JvmOverloads
66-
fun observe(visibleRegion: VisibleRegion, withMaxPoiSearchBoxSize: Boolean = true, completion: (Completion<List<PointOfInterest>>) -> Unit): VisibleRegionNotificationToken {
67-
return VisibleRegionNotificationToken(visibleRegion, withMaxPoiSearchBoxSize, database.gasStationDao(), completion)
65+
fun observe(visibleRegion: VisibleRegion, padding: Double = 0.0, completion: (Completion<List<PointOfInterest>>) -> Unit): VisibleRegionNotificationToken {
66+
return VisibleRegionNotificationToken(visibleRegion, padding, database.gasStationDao(), completion)
6867
}
6968

7069
fun observe(vararg ids: String, completion: (Completion<List<PointOfInterest>>) -> Unit): IDsNotificationToken {

library/src/main/java/cloud/pace/sdk/poikit/poi/PoiKitObserverToken.kt

Lines changed: 21 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,11 @@ package cloud.pace.sdk.poikit.poi
33
import androidx.lifecycle.LiveData
44
import androidx.lifecycle.MutableLiveData
55
import androidx.lifecycle.Observer
6-
import cloud.pace.sdk.poikit.POIKit
76
import cloud.pace.sdk.poikit.database.GasStationDAO
87
import cloud.pace.sdk.poikit.poi.download.TileDownloader
98
import cloud.pace.sdk.poikit.poi.download.TileQueryRequestOuterClass.TileQueryRequest.*
109
import cloud.pace.sdk.poikit.utils.POIKitConfig
11-
import cloud.pace.sdk.poikit.utils.ZoomException
12-
import cloud.pace.sdk.poikit.utils.diameter
13-
import cloud.pace.sdk.poikit.utils.zoomToDiameter
10+
import cloud.pace.sdk.poikit.utils.addPadding
1411
import cloud.pace.sdk.utils.CloudSDKKoinComponent
1512
import cloud.pace.sdk.utils.Completion
1613
import cloud.pace.sdk.utils.Failure
@@ -29,7 +26,7 @@ open class PoiKitObserverToken : CloudSDKKoinComponent {
2926
val loading = MutableLiveData<Boolean>()
3027
var lastRefreshTime: Date? = null
3128

32-
open fun refresh() {
29+
open fun refresh(zoomLevel: Int = POIKitConfig.ZOOMLEVEL) {
3330
lastRefreshTime = Date()
3431
}
3532

@@ -38,7 +35,7 @@ open class PoiKitObserverToken : CloudSDKKoinComponent {
3835

3936
class VisibleRegionNotificationToken(
4037
val visibleRegion: VisibleRegion,
41-
withMaxPoiSearchBoxSize: Boolean,
38+
padding: Double,
4239
private val gasStationDao: GasStationDAO,
4340
private val completion: (Completion<List<PointOfInterest>>) -> Unit
4441
) : PoiKitObserverToken() {
@@ -48,37 +45,28 @@ class VisibleRegionNotificationToken(
4845
private var downloadTask: Call? = null
4946

5047
init {
51-
if (withMaxPoiSearchBoxSize && visibleRegion.diameter() > POIKit.maxPoiSearchBoxSize) {
52-
completion(Failure(ZoomException()))
53-
} else {
54-
// load all the points that are around a certain radius of the visible center
55-
val regionToLoad = if (withMaxPoiSearchBoxSize) {
56-
visibleRegion.zoomToDiameter(POIKit.maxPoiSearchBoxSize)
57-
} else {
58-
visibleRegion
59-
}
60-
gasStations = gasStationDao.getInBoundingBoxLive(
61-
minLat = regionToLoad.latLngBounds.southwest.latitude,
62-
minLon = regionToLoad.latLngBounds.southwest.longitude,
63-
maxLat = regionToLoad.latLngBounds.northeast.latitude,
64-
maxLon = regionToLoad.latLngBounds.northeast.longitude
65-
)
66-
67-
gasStationsObserver = Observer {
68-
completion(Success(it))
69-
}
70-
71-
gasStationsObserver?.let { gasStations?.observeForever(it) }
48+
// load all the points that are around a certain radius of the visible center
49+
val regionToLoad = visibleRegion.addPadding(padding)
50+
51+
gasStations = gasStationDao.getInBoundingBoxLive(
52+
minLat = regionToLoad.latLngBounds.southwest.latitude,
53+
minLon = regionToLoad.latLngBounds.southwest.longitude,
54+
maxLat = regionToLoad.latLngBounds.northeast.latitude,
55+
maxLon = regionToLoad.latLngBounds.northeast.longitude
56+
)
57+
58+
gasStationsObserver = Observer {
59+
completion(Success(it))
7260
}
61+
62+
gasStationsObserver?.let { gasStations?.observeForever(it) }
7363
}
7464

75-
override fun refresh() {
76-
// TODO: Calculate best zoom level based on the diameter of the request + meta data which discribes what is available at what zoom level
65+
override fun refresh(zoomLevel: Int) {
7766
if (gasStationsObserver == null) return
7867

7968
loading.value = true
8069

81-
val zoomLevel = POIKitConfig.ZOOMLEVEL
8270
val northEast = visibleRegion.latLngBounds.northeast.toLocationPoint().tileInfo(zoom = zoomLevel)
8371
val southWest = visibleRegion.latLngBounds.southwest.toLocationPoint().tileInfo(zoom = zoomLevel)
8472

@@ -116,7 +104,7 @@ class VisibleRegionNotificationToken(
116104
}
117105
}
118106

119-
super.refresh()
107+
super.refresh(zoomLevel)
120108
}
121109

122110
override fun invalidate() {
@@ -139,12 +127,9 @@ class IDsNotificationToken(
139127
gasStations.observeForever(gasStationsObserver)
140128
}
141129

142-
override fun refresh() {
130+
override fun refresh(zoomLevel: Int) {
143131
loading.value = true
144132

145-
// TODO: Calculate best zoom level based on the diameter of the request + meta data which discribes what is available at what zoom level
146-
val zoomLevel = POIKitConfig.ZOOMLEVEL
147-
148133
GlobalScope.launch {
149134
// Build request from bounding box
150135
val tiles = gasStationDao
@@ -177,7 +162,7 @@ class IDsNotificationToken(
177162
}
178163
}
179164

180-
super.refresh()
165+
super.refresh(zoomLevel)
181166
}
182167

183168
override fun invalidate() {

library/src/main/java/cloud/pace/sdk/poikit/utils/PoiKitExceptions.kt

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
package cloud.pace.sdk.poikit.utils
22

3-
open class PoiKitExceptions : Exception()
4-
class ZoomException : PoiKitExceptions()
5-
class ApiException(val errorCode: Int, val errorMessage: String) : Exception() {
3+
class ApiException(private val errorCode: Int, private val errorMessage: String) : Exception() {
64
override fun toString(): String {
75
return super.toString() + "\n" +
86
"code = $errorCode || message = $errorMessage"

library/src/main/java/cloud/pace/sdk/poikit/utils/VisibleRegion.kt

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,3 +46,44 @@ fun VisibleRegion.zoomToDiameter(diameter: Double): VisibleRegion {
4646

4747
return VisibleRegion(nearLeft, nearRight, farLeft, farRight, bounds)
4848
}
49+
50+
/**
51+
* Adds the [padding] to each side of [this] [VisibleRegion] and returns a new padded [VisibleRegion].
52+
*
53+
* @return A new [VisibleRegion] with the added [padding]
54+
*/
55+
fun VisibleRegion.addPadding(padding: Double): VisibleRegion {
56+
val diameter = diameter()
57+
val paddedDiameter = diameter + diameter * padding
58+
59+
// calculate the headings to each edge
60+
val center = latLngBounds.center
61+
val nearLeft = SphericalUtil.computeOffset(center, paddedDiameter / 2.0, center.headingTo(nearLeft))
62+
val nearRight = SphericalUtil.computeOffset(center, paddedDiameter / 2.0, center.headingTo(nearRight))
63+
val farLeft = SphericalUtil.computeOffset(center, paddedDiameter / 2.0, center.headingTo(farLeft))
64+
val farRight = SphericalUtil.computeOffset(center, paddedDiameter / 2.0, center.headingTo(farRight))
65+
66+
val bounds = LatLngBounds.Builder()
67+
.include(nearLeft)
68+
.include(nearRight)
69+
.include(farLeft)
70+
.include(farRight)
71+
.build()
72+
73+
return VisibleRegion(nearLeft, nearRight, farLeft, farRight, bounds)
74+
}
75+
76+
/**
77+
* Calculates the incremental padding depending on the [currentIncrement].
78+
* It ensures that the value lies in the specified range [minPadding]..[maxPadding].
79+
*
80+
* @param maxIncrements The maximum number of increments/steps
81+
*/
82+
fun incrementalPadding(maxIncrements: Int, currentIncrement: Int, maxPadding: Double = 0.85, minPadding: Double = 0.2): Double {
83+
val paddingDifference = maxPadding - minPadding
84+
val factor = maxIncrements - currentIncrement
85+
val relativePadding = (paddingDifference / maxIncrements) * factor
86+
val padding = relativePadding + minPadding
87+
88+
return padding.coerceIn(minPadding, maxPadding)
89+
}

0 commit comments

Comments
 (0)