开发Map Activity时无法显示手机位置的问题排查求助
Hey there! Let's walk through the most common issues that could be preventing your map from showing your device's location and custom marker, even after you've requested permissions:
Double-check actual permission status
Just requesting permissions doesn't guarantee the user granted them. Add a quick check in your code to confirm the current permission state, like:val hasFineLocation = ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED val hasCoarseLocation = ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED Log.d("LocationPerms", "Fine: $hasFineLocation, Coarse: $hasCoarseLocation")Also, for Android 10+, ensure you're requesting the right foreground location permissions, and that you're checking the status after the user responds to the prompt. If you initialized the map before permissions were approved, you'll need to re-trigger location updates once access is granted.
Enable the map's "My Location" layer
Even with permissions, the map won't show your location unless you explicitly enable it. In youronMapReadycallback, add:googleMap.isMyLocationEnabled = trueThis turns on the default blue dot for your location. If you want a custom marker, use a location provider like
FusedLocationProviderClientto fetch coordinates, then add the marker manually:fusedLocationClient.lastLocation.addOnSuccessListener { location -> location?.let { googleMap.addMarker(MarkerOptions().position(LatLng(it.latitude, it.longitude)).title("My Position")) googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(LatLng(it.latitude, it.longitude), 15f)) } }Verify device location services are turned on
Your app can't get location data if the user disabled location services in system settings. Add a check to confirm:val locationManager = getSystemService(Context.LOCATION_SERVICE) as LocationManager val isGpsEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER) val isNetworkEnabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER) if (!isGpsEnabled && !isNetworkEnabled) { // Show a dialog to guide the user to enable location services }For newer Android versions, use
SettingsClientto check location settings more formally.Check your Maps API key and configuration
Make sure yourAndroidManifest.xmlhas the correct API key, and that this key is enabled for the Maps SDK for Android in the Google Cloud Console. If the key is invalid or the service isn't enabled, the map might load, but location features will fail silently. Confirm you've added the required meta-data tag:<meta-data android:name="com.google.android.geo.API_KEY" android:value="YOUR_API_KEY"/>Account for location update delays
Location data can take a few seconds to load, especially on first launch. If you're trying to add a marker immediately when the map loads, the location might not be available yet. UseFusedLocationProviderClient's location callbacks to add the marker only after coordinates are retrieved, rather than relying onlastLocationwhich might return null.Debug your custom marker code
If using a custom marker, double-check:- Is the marker being added inside the
onMapReadycallback (so the map is fully initialized)? - Are you passing valid latitude/longitude coordinates?
- Is the marker icon visible against the map background? Try using the default marker first to rule out icon issues.
- Is the marker being added inside the
内容的提问来源于stack exchange,提问作者jerry fa

