Starting activity with clicking on google map marker infoWindow - android

I have to start some activity with clicking on infoWindow (I can do this with setOnInfoWindowClickListener()), but this activity can be different, and I need something else. I should have a possibility to set an ID to every map marker. Can I pass custom ID to every Marker on map? Or how I can create button in infoWindow with this id (if I can do this, I can pass this id).

I suggest using a HashMap or something similar. As you iterate over your list of objects and create markers for them, also add the Marker to a list, using the ID of the object as the key, and the marker as the value:
private HashMap<Integer, Marker> markerMap = new HashMap<Integer, Marker>();
...
for(MarkerObject obj : this.markerObjects)
{
//If the marker isn't already being displayed
if(!markerMap.containsKey(obj.getId()))
{
//Add the Marker to the Map and keep track of it
this.markerMap.put(obj.getId(), this.mMap.addMarker(getMarkerForObject(obj)));
}
}
Then you can use a OnInfoWindowClickListener to find the object id of the tapped marker in your Map and do something with the corresponding data, like open a new activity with details.

Related

Unable to hide Google Maps marker on Android

I have an issue with the Maps API on Android.
When I create a marker and add it to the map it is correctly displayed. See code below:
// Create a list for referencing markers
val markers: MutableList<Marker> = mutableListOf()
// Add marker to map
val markerOptions = MarkerOptions().position(position).icon(anIcon).title("A title")
val marker = map.addMarker(markerOptions)
// Save marker in list
markers.add(marker)
Later on, when I tried to hide each marker by iterating through the list of markers and setting the isVisible attribute to false (see code below) it does not work. All the markers are still displayed.
for(marker in markers)
marker.isVisible = false
What can explain this behavior?
Extra information: If I call marker.remove(), the markers are properly removed from the map
Extra information: If I set the visibility to false when I add the marker to the map in the first place the marker is not displayed.
The reason appears to be that isVisible is a getter method that checks whether a marker is visible or not. To make the marker invisible you should use marker.setVisible(false). You can learn more on this in Google's Marker class Reference.
Hope this helps!

Map marker won't leave screen once multiple are set on there, only if its single

I have a problem with using Google maps API on Android. I have a button which removes the selected marker off the maps interface, and information about it from the SQLite db that I have set up. Although my only problem is once multiple markers are on the map this feature stops and doesn't remove them:
Below shows the method which removes the marker from the maps and replaces them. As I said this works perfectly fine with a single marker and my testing has been a success, but with multiple it does not work.
I have an onclicklisterner for the markers which displays the information and a popup box for the marker, for the user to remove the marker they must click the marker which sets the global variable to that object then once the remove button is pressed the removeMarker() method is called. The getAllMarkers() method loops through a SQLite db and pulls information and adds to the maps.
End problem: Removing a marker when multiple markers are placed on the map doesn't work. Only works when a single marker is placed on the map.
Marker lastOpened = null;
To remove the information from the db the condition in the if statement returns a boolean value if it has been done:
if(this.mDbHelper.deleteLine(lastOpened.getTitle()))
Remove method
public void removeMarker(){
if(this.lastOpened != null){
if(this.mDbHelper.deleteLine(lastOpened.getTitle())){
lastOpened.remove();
getAllMarkers();
}
}
}
Thanks
You can either use googleMap.clear() or you can store your Markers in a collection of kind and remove them in a loop:
private ArrayList<Marker> mMarkers;
...
private void removeMarkers() {
for (Marker marker: mMarkers) {
marker.remove();
}
mMarkers.clear();
}
Here's a related ticket discuss how to remove marker: Remove a marker from a GoogleMap

How to manage Markers well using google maps api v2 on android

I'm trying to implement app in which you can add your own marker which will be given a shutdown time. To do so I need to know how to manage my markers. Let's say I have array list of users with assigned unique ID.
private ArrayList<User> userList = new ArrayList<>();
Then I will create array list of markers which will contain information like Latitude, Longitude, Title, owner's ID and deadline.
private ArrayList<MyMarker> mMarkersArray = new ArrayList<MyMarker>();
Next whenever user will activate add marker method, new marker will be pushed to my list of markers. Ideologically everything seems nice and easy, furthermore creating new object looks like this:
Marker mMarker = mMap.addMarker(new MarkerOptions() (...) );
but when it comes to managing specific markers it seems like I'm missing something. There will be some trigger method which will check the deadline of all markers (or rather first one on the 'sorted by deadline' list) and then it should remove (not hide, because I think it would be inefficient from the memory point of view). How to achieve this? I can't add some custom variable like ID to markers (so I could then find the one I'm interested in) and I'm a bit lost.
There is a way to achieve this by clearing whole map and then rendering again all markers except the inactive, but as far as I'm concerned it's very inefficient and there has to be better solution.
If you want to remove a specific marker from the map, you can just call the remove(). method.
Sample code to remove a marker from MapView and HashMap:
for (Marker marker : hm.keySet()) {
if (hm.get(marker).equals(deadline)) {
marker.remove();
hm.remove(marker);
}
}
You dont need to clear the entire MapView, if you just call remove() method on specific marker.

Multiple InfoWindowAdatper's

I have a map in which I am creating different types of markers. I cannot assign an info window adapter to a marker (gee wouldn't that be nice), I can only assign on InfoWindowAdapter for the entire map (at least I think).
My problem is that I want to show a different type of info widow depending on what I clicked. Id the only way to set one InfoWindowAdapter that will handle creating the correct type of info window based on the marker that I am passed?
Am I missing something easy?
When you add a marker to the map, you are receiving back an ID, which uniquely identifies your marker.
You can create an instance of your InfoWindowAdapter immediately after you add the marker and put it in a map, which keeps the ID as key and your InfoWindowAdapter as value.
Marker marker = map.addMarker(options);
// Create your special infoWindowAdapter for this marker
// ...
adapterMap.put(marker.getId(), youSpecialInfoWindowAdapter);
In the one central InfoWindowAdapter, which you register at the map, you can just use the ID of the marker to get the specific InfoWindowAdapter and delegate to the methods of that.
Access to the map can e.g. be provided in the constructor of the InfoWindowAdapter (to avoid global or static variables):
class CentralInfoWindowAdapter implements InfoWindowAdapter {
Map<String, GoogleMap.InfoWindowAdapter> adapterMap;
public CentralInfoWindowAdapter(
Map<String, GoogleMap.InfoWindowAdapter> adapterMap) {
this.adapterMap = adapterMap;
}
#Override
public View getInfoContents(Marker marker) {
InfoWindowAdapter adapter = adapterMap.get(marker.getId());
return adapter.getInfoContents(marker);
}
#Override
public View getInfoWindow(Marker marker) {
InfoWindowAdapter adapter = adapterMap.get(marker.getId());
return adapter.getInfoWindow(marker);
}
}
You can vary this principle of course. If you have only a few different InfoWindowAdapters depending on the "type" of the marker, you may put an enumeration into the map, which identifies the type of your marker and lets you decide, which kind of real InfoWindowAdapter to use inside the central InfoWindowAdapter, or you may still put instances of your special InfoWindowAdapter into the map, but use the same instance for the same type of marker.
if i am right you want to show a different window adapter on each marker?.. if yes you can add a tag on each marker then inside one of the two infowindow function either infowindow() or infocontents() checks the marker tag and add the appropriate layout.

Android Google Maps V2 Marker Ids not updating on fragment re-entry

I have a weird problem which didn't seem to be an issue until recently. I have a map fragment which adds markers to a map, and associates them with a java Map collection of '<'marker id's, object they are associated with'>' for the getInfoWindow call.
This works fine, 9 items are added to the map with ids m0-m8. However when I leave the fragment and re-enter it, the markers are added to the map with unique numbers again, so second time around the ids are m9-m17 etc.
The problem is that the marker's onInfoClick retains the old ids.
CustomInfoWindowAdapter() {
mWindow = getActivity().getLayoutInflater().inflate(R.layout.custom_info_window, null);
}
#Override
public View getInfoWindow(final Marker marker) {
//Here marker.getId() is usually always a value between m0-m8
Business markerBusiness = mMarkersMap.get(marker.getId());
//Render and do other things
return mWindow();
}
How can I either force the addMarker() call to forget the id's from before OR make sure the new markers match the onInfoClick final marker ids? To further complicate things it seems this problem occurs only certain markers which I can't understand at all!
Let me know if more code is needed, I'm not sure what is necessary, I set a new infoWindowAdapter every-time the map fragment is recreated:
mMap.setInfoWindowAdapter(new CustomInfoWindowAdapter());
//Add markers to the map
mMarkersMap.put(newMarker.getId(), business);

Categories

Resources