Skip to content

Commit a34213a

Browse files
authored
Improve Nearby fragment initial location (#50)
1 parent 1f1b7b1 commit a34213a

File tree

1 file changed

+10
-3
lines changed

1 file changed

+10
-3
lines changed

app/src/main/java/org/nitri/opentopo/MapFragment.kt

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -305,8 +305,12 @@ class MapFragment : Fragment(), LocationListener, PopupMenu.OnMenuItemClickListe
305305
val hasCoarseLocationPermission =
306306
hostActivity?.checkSelfPermission(Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED
307307
if (hasFineLocationPermission || hasCoarseLocationPermission) {
308-
locationViewModel?.let {
309-
it.currentLocation.value = locationManager?.getLastKnownLocation(LocationManager.GPS_PROVIDER)
308+
locationViewModel?.let { viewModel ->
309+
val lastKnownLocation =
310+
locationManager?.getLastKnownLocation(LocationManager.GPS_PROVIDER)
311+
?: locationManager?.getLastKnownLocation(LocationManager.NETWORK_PROVIDER)
312+
?: locationManager?.getLastKnownLocation(LocationManager.PASSIVE_PROVIDER)
313+
viewModel.currentLocation.value = lastKnownLocation
310314
}
311315
}
312316
savedInstanceState?.let {
@@ -798,7 +802,10 @@ class MapFragment : Fragment(), LocationListener, PopupMenu.OnMenuItemClickListe
798802
R.id.action_nearby -> {
799803
listener?.let { listener ->
800804
listener.clearSelectedNearbyPlace()
801-
val nearbyCenter = mapView.mapCenter as GeoPoint
805+
val nearbyCenter =
806+
locationViewModel?.currentLocation?.value?.let { location ->
807+
GeoPoint(location)
808+
} ?: mapView.mapCenter as GeoPoint
802809
nearbyCenter.let { center ->
803810
listener.addNearbyFragment(center)
804811
}

0 commit comments

Comments
 (0)