You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

在MapsActivity中为定位标记添加点击显示地址功能求助

Hey there! I see you’ve already got the location-updating marker working, but just need to hook up that address info to the marker’s info window. Let’s fix that step by step:

First, connect your address data to the marker

Right now you’re successfully fetching the full address, but you aren’t assigning it to your marker or even adding the marker to the map properly. Here’s how to update your code:

  1. Add the missing Locale import (you’re using it but haven’t imported it yet):
import java.util.Locale;
  1. Update the onLocationChanged method to use the address data and create the marker with proper info:
@Override
public void onLocationChanged(Location location) {
    lastLocation = location;
    // Remove old marker if it exists
    if(currentLocationMarker != null){
        currentLocationMarker.remove();
    }
    LatLng latLng = new LatLng(location.getLatitude(), location.getLongitude());
    MarkerOptions markerOptions = new MarkerOptions();
    markerOptions.position(latLng);
    markerOptions.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_AZURE));
    
    geocoder = new Geocoder(this, Locale.getDefault());
    try {
        addresses = geocoder.getFromLocation(location.getLatitude(), location.getLongitude(), 1);
        if (addresses != null && !addresses.isEmpty()) {
            String address = addresses.get(0).getAddressLine(0);
            String area = addresses.get(0).getLocality();
            String city = addresses.get(0).getAdminArea();
            String fullAddress = address + ", " + area + ", " + city;
            
            // Set marker title and address snippet (shows in info window)
            markerOptions.title("Current Location");
            markerOptions.snippet(fullAddress);
        } else {
            markerOptions.title("Current Location");
            markerOptions.snippet("Could not retrieve address");
        }
    } catch (IOException e) {
        e.printStackTrace();
        markerOptions.title("Current Location");
        markerOptions.snippet("Error fetching address");
    }
    
    // Add the marker to the map and save reference for future updates
    currentLocationMarker = mMap.addMarker(markerOptions);
    // Optional: Zoom map to current location for better visibility
    mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(latLng, 15));
}

What this fixes:

  • We add null checks for the address list to avoid crashes
  • Assign the fetched full address to the marker’s snippet (this is the text that appears below the title in the info window)
  • Properly add the marker to the map and keep a reference to update it as the user moves
  • Handle error cases where the address can’t be fetched

Optional: Customize the info window (for a nicer look)

If you want to replace the default info window style, create a custom layout:

  1. Make a layout file res/layout/custom_info_window.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:padding="16dp"
    android:background="#FFFFFF">

    <TextView
        android:id="@+id/info_title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="16sp"
        android:textStyle="bold"
        android:textColor="#000000"/>

    <TextView
        android:id="@+id/info_snippet"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="14sp"
        android:textColor="#666666"
        android:layout_marginTop="4dp"/>

</LinearLayout>
  1. Add this to your onMapReady method to use the custom layout:
mMap.setInfoWindowAdapter(new GoogleMap.InfoWindowAdapter() {
    @Nullable
    @Override
    public View getInfoWindow(@NonNull Marker marker) {
        return null; // Use the default window frame
    }

    @Nullable
    @Override
    public View getInfoContents(@NonNull Marker marker) {
        View infoView = getLayoutInflater().inflate(R.layout.custom_info_window, null);
        TextView titleView = infoView.findViewById(R.id.info_title);
        TextView snippetView = infoView.findViewById(R.id.info_snippet);
        
        titleView.setText(marker.getTitle());
        snippetView.setText(marker.getSnippet());
        
        return infoView;
    }
});

That’s all! Now when you click your current location marker, it’ll display the full address instead of just the default text.

内容的提问来源于stack exchange,提问作者Tanner Tattini

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.28 06:26:05