Search code examples
androidgoogle-maps-markersgoogle-maps-android-api-2android-listfragmentsupportmapfragment

Changing markers on google maps v2 from other fragment


My problem is as follows - I'm working on an app with one activity and different fragments, the main fragment is a google map v2 and under the map there are static buttons that shows continent on the map, upon clicking on it the map fragment changes into a list fragment that has different categories, upon clicking a category I want to place different markers on the map (In case there are some currently on the map they should be changed). I am new to fragments and so I dont really know how to handle this, I do know that mapactivity (on googl maps v1) requires context in order to place markers on the map and so on a test I made on a test app I made the app kept on crushing when I tried to place new markers on the mapactivity from a listview activity (when I passed the mapactivity context as a parameter it had to be static which didnt worked...).

Will be glad to hear any ideas\suggestions\solutions...


Solution

  • Adding an answer for the benefit of future reference.

    For the above requirement, when a category is selected in the list fragment, it should be communicated to the hosting activity and then the hosting activity should call the map fragment and pass on the details of the chosen category. Now in the map fragment, on receiving the category details, the existing markers should be cleared, new set of markers should be queried based on the category parameter and then displayed. Check this document to understand communication between fragments through activity