android different info window for each marker in google maps

喜夏-厌秋 提交于 2020-01-04 12:46:31

问题


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



来源:https://stackoverflow.com/questions/17263097/android-different-info-window-for-each-marker-in-google-maps

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!