Changing a Marker's text in Android GoogleMaps

2019-07-06 23:13发布

问题:

Is it possible to change the text inside a GoogleMap Marker after it's already been set? I'm using MarkerOptions to set the title and snippet originally like this:

SupportMapFragment theMapFragment = (SupportMapFragment)getSupportFragmentManager().findFragmentById(R.id.map);
GoogleMap theMap = theMapFragment.getMap();
MarkerOptions theMarker = new MarkerOptions();
theMarker.position(theLatLng);
theMarker.title("My title");
theMarker.snippet("This is my snippet");
theMarker.visible(true);
theMap.addMarker(theMarker);

Later, when the user taps on something, I'd like to perform the reverse geocode lookup and change the title/snippet to contain address information.

回答1:

Is it possible to change the text inside a GoogleMap Marker after it's already been set?

Marker has setTitle() and setSnippet() methods. You will need a Marker object representing this marker, probably one that you held onto from the addMarker() call.



回答2:

Try this:

  @Override
public boolean onMarkerClick(final Marker marker) {

    if (marker.equals(myMarker)) 
    {
         googleMap.addMarker(new MarkerOptions()
                .position(marker.getPosition())
                .title("Onother title")
                .snippet("snippet")
                .icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_AZURE)));
    }
}