android different info window for each marker in g

2019-07-08 02:30发布

问题:

I am using a for loop to make markers on maps based on locations retrieved from database.

I have a layout with two TextViews that i want to fill with the marker's name and address(retrieved from database).

I tried using googleMap.setInfoWindowAdapter() but all it did was changing the layout for all markers altogether (which is obviously NOT what i need).

So is there a way to allow me to change the info window of each marker independently?

回答1:

Implement onMarkerClickListener. Get the marker object as follows

Marker marker = map.addMarker(new MarkerOptions().position(latLong)
            .title(string1).snippet(string2)));

You will get your marker in overridden method when you click on it. Get the title and snippet from it as follows.

@Override
public boolean onMarkerClick(Marker marker) {
    map.setInfoWindowAdapter(new MarkerAdapter(getActivity(), marker
            .getTitle(), marker.getSnippet()));
    return false;
}

Good luck :)



回答2:

The simplest way to show two lines of text is to use built-in support for info window. When setting title and snippet on each marker, you will have them shown after clicking Marker if you don't set your own InfoWindowAdapter.

If your address is multiline text, then the above won't work correctly and you need to provide you own InfoWindowAdapter implementation. When doing that just use Marker parameter sent to getInfoContents or getInfoWindow: retrieve title and snippet from it and put inside your TextViews using setText.

When having more than 2 texts to show or other data types, it complicates even futher, but if you need it at some point, take a look at this answer: Android google maps add to marker own tag