Created
November 27, 2025 15:34
-
-
Save Mojtaba-Shafaei/ba3fc285f7e8ba55a9d8990b1734ed5d to your computer and use it in GitHub Desktop.
Kotlin function to find current gps location in Android
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| suspend fun goToCurrentLocation(context: Context, mapView: MapView) { | |
| withContext(Dispatchers.Main) { | |
| // Find or create location overlay | |
| var locationOverlay = mapView.overlays.filterIsInstance<MyLocationNewOverlay>().firstOrNull() | |
| if (locationOverlay == null) { | |
| locationOverlay = | |
| MyLocationNewOverlay(GpsMyLocationProvider(context), mapView).apply { | |
| enableMyLocation() | |
| enableFollowLocation() | |
| mapView.overlays.add(this) | |
| } | |
| Timber.d("Created new location overlay") | |
| } else { | |
| // Make sure it's enabled | |
| if (!locationOverlay.isMyLocationEnabled) { | |
| locationOverlay.enableMyLocation() | |
| } | |
| if (!locationOverlay.isFollowLocationEnabled) { | |
| locationOverlay.enableFollowLocation() | |
| } | |
| Timber.d("Using existing location overlay") | |
| } | |
| // Try to get current location | |
| val currentLocation = locationOverlay.myLocation | |
| if (currentLocation != null) { | |
| // Location already available | |
| mapView.controller.animateTo(currentLocation) | |
| mapView.controller.setZoom(19.0) | |
| mapView.invalidate() | |
| Toast.makeText(context, "Moved to current location", Toast.LENGTH_SHORT).show() | |
| Timber.d("Location available: ${currentLocation.latitude}, ${currentLocation.longitude}") | |
| } else { | |
| // Wait for location fix | |
| Toast.makeText(context, "Getting your location...", Toast.LENGTH_SHORT).show() | |
| Timber.d("Waiting for GPS fix...") | |
| // Use runOnFirstFix for better results | |
| locationOverlay.runOnFirstFix { | |
| Handler(Looper.getMainLooper()).post { | |
| val firstFix = locationOverlay.myLocation | |
| if (firstFix != null) { | |
| mapView.controller.animateTo(firstFix) | |
| mapView.controller.setZoom(19.0) | |
| mapView.invalidate() | |
| Toast.makeText(context, "Got GPS fix!", Toast.LENGTH_SHORT).show() | |
| Timber.d("First fix: ${firstFix.latitude}, ${firstFix.longitude}") | |
| } else { | |
| Toast.makeText(context, "Unable to get location", Toast.LENGTH_SHORT).show() | |
| Timber.w("runOnFirstFix but location is still null") | |
| } | |
| } | |
| } | |
| // Fallback timeout | |
| withTimeoutOrNull(15_000L) { | |
| var attempts = 0 | |
| while (locationOverlay.myLocation == null && attempts < 30) { | |
| delay(500) | |
| attempts++ | |
| Timber.d("Waiting for location... attempt $attempts") | |
| } | |
| } | |
| ?: run { | |
| withContext(Dispatchers.Main) { | |
| Toast.makeText( | |
| context, | |
| "Location timeout. Please check GPS settings.", | |
| Toast.LENGTH_LONG, | |
| ) | |
| .show() | |
| Timber.w("Location timeout after 15 seconds") | |
| } | |
| } | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment