@@ -284,14 +284,23 @@ class ExploreMapFragment : CommonsDaggerSupportFragment(), ExploreMapContract.Vi
284284 requireActivity().registerReceiver(broadcastReceiver, intentFilter)
285285 }
286286 setSearchThisAreaButtonVisibility(false )
287+
288+ // Check if we need to wait for first location
289+ val hasPermission = locationPermissionsHelper?.checkLocationPermission(requireActivity()) == true
290+ val cachedLocation = locationManager.getLastLocation()
291+
292+ if (hasPermission && cachedLocation == null && ! isWaitingForFirstLocation) {
293+ isWaitingForFirstLocation = true
294+ setProgressBarVisibility(true )
295+ }
296+
287297 if (shouldPerformMapReadyActionsOnResume) {
288298 shouldPerformMapReadyActionsOnResume = false
289299 performMapReadyActions()
290300 }
291301
292302 // Only refresh if permission state changed from false to true
293- val hasPermissionNow = locationPermissionsHelper?.checkLocationPermission(requireActivity()) == true
294- if (hasPermissionNow && ! hadLocationPermissionOnPause) {
303+ if (hasPermission && ! hadLocationPermissionOnPause) {
295304 performMapReadyActions()
296305 }
297306 }
@@ -372,11 +381,9 @@ class ExploreMapFragment : CommonsDaggerSupportFragment(), ExploreMapContract.Vi
372381 isPermissionDenied = true
373382 }
374383
375- lastKnownLocation = getLastLocation()
376-
377- if (lastKnownLocation == null ) {
378- lastKnownLocation = defaultLatLng
379- }
384+ // Use locationManager.getLastLocation() directly to get fresh location
385+ // Don't use getLastLocation() as it may return cached defaultLatLng
386+ lastKnownLocation = locationManager.getLastLocation()
380387
381388 // if we came from 'Show in Explore' in Nearby, load Nearby map center and zoom
382389 if (isCameFromNearbyMap) {
@@ -385,11 +392,20 @@ class ExploreMapFragment : CommonsDaggerSupportFragment(), ExploreMapContract.Vi
385392 prevZoom.coerceIn(1.0 , 22.0 ),
386393 1L
387394 )
388- } else {
395+ } else if (lastKnownLocation != null ) {
396+ // We have a real location - center to it
397+ moveCameraToPosition(
398+ GeoPoint (lastKnownLocation!! .latitude, lastKnownLocation!! .longitude)
399+ )
400+ } else if (! isWaitingForFirstLocation) {
401+ // No location and not waiting - use default
402+ // This happens when permission not granted yet
403+ lastKnownLocation = defaultLatLng
389404 moveCameraToPosition(
390405 GeoPoint (lastKnownLocation!! .latitude, lastKnownLocation!! .longitude)
391406 )
392407 }
408+ // If waiting for first location, don't center - will center when location arrives
393409 presenter!! .onMapReady(exploreMapController)
394410 }
395411
@@ -483,11 +499,13 @@ class ExploreMapFragment : CommonsDaggerSupportFragment(), ExploreMapContract.Vi
483499 }
484500 }
485501
486- override fun onLocationChangedSignificantly (latLng : LatLng ) =
502+ override fun onLocationChangedSignificantly (latLng : LatLng ) {
487503 handleLocationUpdate(latLng, LocationChangeType .LOCATION_SIGNIFICANTLY_CHANGED )
504+ }
488505
489- override fun onLocationChangedSlightly (latLng : LatLng ) =
506+ override fun onLocationChangedSlightly (latLng : LatLng ) {
490507 handleLocationUpdate(latLng, LocationChangeType .LOCATION_SLIGHTLY_CHANGED )
508+ }
491509
492510 private fun handleLocationUpdate (
493511 latLng : LatLng ? ,
@@ -496,16 +514,16 @@ class ExploreMapFragment : CommonsDaggerSupportFragment(), ExploreMapContract.Vi
496514 lastKnownLocation = latLng
497515 exploreMapController.currentLocation = lastKnownLocation
498516
499- // If waiting for first location after permission grant, center map now
517+ // If we were waiting for first location after permission grant, center map now
500518 if (isWaitingForFirstLocation && latLng != null ) {
501519 isWaitingForFirstLocation = false
520+ setProgressBarVisibility(false )
521+
502522 val targetP = GeoPoint (latLng.latitude, latLng.longitude)
503523 mapCenter = targetP
504524 binding!! .mapView.controller.setCenter(targetP)
505525 recenterMarkerToPosition(targetP)
506526 moveCameraToPosition(targetP)
507- setProgressBarVisibility(false )
508- presenter!! .onMapReady(exploreMapController)
509527 }
510528
511529 presenter!! .updateMap(locationChangeType)
@@ -587,35 +605,27 @@ class ExploreMapFragment : CommonsDaggerSupportFragment(), ExploreMapContract.Vi
587605 private fun locationPermissionGranted () {
588606 isPermissionDenied = false
589607 applicationKvStore.putBoolean(" doNotAskForLocationPermission" , false )
608+
609+ // Add listener and register location manager
610+ locationManager.addLocationListener(this )
611+ locationManager.registerLocationManager()
612+
590613 lastKnownLocation = locationManager.getLastLocation()
591- val target = lastKnownLocation
614+
592615 if (lastKnownLocation != null ) {
593- // Location already available, center immediately
594- val targetP = GeoPoint (target!! .latitude, target.longitude)
616+ val targetP = GeoPoint (lastKnownLocation!! .latitude, lastKnownLocation!! .longitude)
595617 mapCenter = targetP
596618 binding!! .mapView.controller.setCenter(targetP)
597619 recenterMarkerToPosition(targetP)
598620 moveCameraToPosition(targetP)
599621 presenter!! .onMapReady(exploreMapController)
600- } else if (locationManager.isGPSProviderEnabled()
601- || locationManager.isNetworkProviderEnabled()
602- ) {
603- // No cached location, request updates and wait for first location
622+ } else {
623+ // No cached location - set flag to wait for first GPS fix
604624 isWaitingForFirstLocation = true
605- locationManager.requestLocationUpdatesFromProvider(LocationManager .NETWORK_PROVIDER )
606- locationManager.requestLocationUpdatesFromProvider(LocationManager .GPS_PROVIDER )
607625 setProgressBarVisibility(true )
608- // Don't call onMapReady yet - will be called when location arrives
609- } else {
610- // Location services disabled
611- locationPermissionsHelper!! .showLocationOffDialog(
612- requireActivity(),
613- R .string.ask_to_turn_location_on_text
614- )
615- // Fallback to default location
626+ // Load map but don't center yet - will center when location arrives
616627 presenter!! .onMapReady(exploreMapController)
617628 }
618- registerUnregisterLocationListener(false )
619629 }
620630
621631 fun registerUnregisterLocationListener (removeLocationListener : Boolean ) {
@@ -1152,12 +1162,31 @@ class ExploreMapFragment : CommonsDaggerSupportFragment(), ExploreMapContract.Vi
11521162
11531163 override fun onLocationPermissionGranted () {
11541164 if (locationPermissionsHelper!! .isLocationAccessToAppsTurnedOn()) {
1165+ locationManager.addLocationListener(this )
11551166 locationManager.registerLocationManager()
11561167 drawMyLocationMarker()
1168+
1169+ // Check if we have a cached location
1170+ val cachedLocation = locationManager.getLastLocation()
1171+
1172+ if (cachedLocation != null ) {
1173+ // Center map immediately
1174+ val targetP = GeoPoint (cachedLocation.latitude, cachedLocation.longitude)
1175+ mapCenter = targetP
1176+ binding?.mapView?.controller?.setCenter(targetP)
1177+ recenterMarkerToPosition(targetP)
1178+ moveCameraToPosition(targetP)
1179+ populatePlaces(cachedLocation)
1180+ } else {
1181+ // No cached location - wait for first GPS fix
1182+ isWaitingForFirstLocation = true
1183+ setProgressBarVisibility(true )
1184+ // Still need to populate with default, but will recenter when location arrives
1185+ populatePlaces(getMapCenter())
1186+ }
11571187 } else {
11581188 locationPermissionsHelper!! .showLocationOffDialog(requireActivity(), R .string.location_off_dialog_text)
11591189 }
1160- onLocationChanged(LocationChangeType .PERMISSION_JUST_GRANTED , null )
11611190 }
11621191
11631192 fun onLocationChanged (locationChangeType : LocationChangeType , location : Location ? ) {
0 commit comments