RecyclerView点击跳转地图不显示周边场所标记问题排查
Hey there! Let's tackle this marker not showing up issue step by step— I've been stuck with similar map-related bugs before, so let's break things down.
- Double-check location permissions: Make sure your app has requested
ACCESS_FINE_LOCATIONorACCESS_COARSE_LOCATION, and that the user has granted them. A missing or ungranted permission is the #1 culprit for empty place searches— if you can't get the user's current location, there's no "周边" (surrounding area) to search. - Confirm you're actually getting a valid current location: Use
FusedLocationProviderClientto fetch the user's coordinates, then log theLatLngvalue. If you're seeing(0.0, 0.0)or a nonsensical coordinate, your location fetch logic is broken, and the search will return nothing.
- Validate place type filters: Are you using the correct type constants? For restaurants, make sure it's
TYPE_RESTAURANT; for bars,TYPE_BAR. A typo here will return zero results, so no markers to display. - Adjust search radius: If you set a tiny radius (like 10 meters) and there are no matching places that close, you'll get nothing. Test with a larger radius first (e.g., 1000 meters) to rule this out.
- Ensure marker creation logic is in the right callback: Make sure you're iterating through search results and adding markers only after the map is fully ready. Here's a quick example of how that should look:
placesClient.findCurrentPlace(request, null) .addOnSuccessListener(response -> { for (PlaceLikelihood likelihood : response.getPlaceLikelihoods()) { Place place = likelihood.getPlace(); LatLng placeLatLng = place.getLatLng(); if (placeLatLng != null) { mMap.addMarker(new MarkerOptions() .position(placeLatLng) .title(place.getName().toString()) .snippet(place.getAddress().toString())); } } }) .addOnFailureListener(e -> Log.e("MapError", "Search failed: " + e.getMessage()));
If you run this code before onMapReady fires, mMap will be null, and markers won't be added at all.
You mentioned your old app showed the user's location first, then loaded places when a button was clicked. That logic ensures the map and location are fully ready before searching. In your current app, are you triggering the search immediately after the map screen loads— before the map or location is initialized?
Try moving your nearby search code inside the onMapReady callback, and wait until you've successfully fetched the user's current location before running the search. That way, you know both the map and location data are ready to use.
- Move the camera to the user's location: Even if markers are added, if the map is zoomed out or pointing somewhere else, you won't see them. After getting the user's
LatLng, run:
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(currentLatLng, 15f));
- Check marker visibility: If your marker color blends with the map (e.g., white on light map), it might be invisible. Add a distinct color to test:
markerOptions.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_RED));
内容的提问来源于stack exchange,提问作者user8331144

