Android Kotlin项目中Google Maps无法完整显示且标记不显示的问题求助
Hey there! As someone who’s fumbled through plenty of Google Maps setup snags early in my Android dev journey, let’s work through your two issues step by step—no jargon, just actionable fixes.
Issue 1: Map Only Loads Partial Australia Region
This usually ties back to incorrect initial positioning or a misconfigured API key. Here’s what to check first:
Validate your intent coordinate values
Your code pullslatitudeandlongitudefrom the intent with default values of0.0. If the calling activity doesn’t properly pass these extras, your map will center on(0.0, 0.0)(near the Gulf of Guinea), but leftover cache or accidental use of the commented-out Sydney code could cause partial Australia loading. Add a log to confirm you’re getting valid coordinates:Log.d("MapsActivity", "Received lat: $latitude, lng: $longitude")If the log shows
0.0for both, fix the calling activity to correctly put the latitude/longitude extras into the intent.Double-check your API key configuration
Ensure your API key has the Maps SDK for Android enabled in the Google Cloud Console. Also, confirm your app’s SHA-1 fingerprint (for both debug and release builds) is added to the API key’s restrictions. Incorrect restrictions often cause partial map loading or total failure.Adjust the map zoom level
You’re usingCameraUpdateFactory.newLatLng(restaurant), which sets a very low default zoom. Switch tonewLatLngZoomto zoom in on your target location:mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(restaurant, 15f)) // 15 is a street-level zoom that works well
Issue 2: Markers Not Showing Up
Markers failing to display almost always relate to coordinate issues or map setup oversights. Let’s rule out the most common culprits:
Confirm coordinate order in
LatLng
Google Maps’LatLngclass takes parameters in the order(latitude, longitude). If your calling activity passed coordinates in reverse (longitude first), your marker will be in the wrong location (possibly outside your visible map area). Double-check the intent extra names and values in the calling code.Test with hardcoded coordinates first
To eliminate intent-related issues, temporarily replace the intent-provided coordinates with a known valid location (like Sydney’s(-34.0, 151.0)). If the marker shows up, the problem is definitely with how you’re passing extras from the calling activity.Fix your marker title configuration
Your current marker title is"Marker in "(missing theRestitlevalue). While this won’t hide the marker, it’s worth updating to make it visible and useful:mMap.addMarker(MarkerOptions().position(restaurant).title("Marker in ${Restitle ?: "Unknown Location"}"))
Additional General Checks
Ensure dependencies are up to date
Verify your module-levelbuild.gradlehas the latest Google Maps dependencies and Google Services plugin:// Dependencies block implementation 'com.google.android.gms:play-services-maps:18.2.0' // Bottom of the file apply plugin: 'com.google.gms.google-services'Check Google Play Services availability
Even though theSupportMapFragmentprompts users to install Play Services, adding a quick check inonCreatecan catch outdated versions:val availability = GoogleApiAvailability.getInstance() val resultCode = availability.isGooglePlayServicesAvailable(this) if (resultCode != ConnectionResult.SUCCESS) { if (availability.isUserResolvableError(resultCode)) { availability.getErrorDialog(this, resultCode, 9001).show() } }
内容的提问来源于stack exchange,提问作者ZahraCarthage CHOUCHENE

