zundi
zundi

Reputation: 2457

Maps v2 Info Window doesn't display when marker is touched, but does after touching invisible Info Window

I'm currently displaying a single marker. When touched the info window is NOT displayed. I've confirmed that the title is actually being set in the marker.

However, if I touch the area above the marker in which the info window would be, I do get the OnInfoWindowClickListener event. When this happens I open another activity, and when I return from this activity the info window is displayed (actually, I can see it being displayed a split second before the activity starts).

Any tips on how I can get the info window to display when the marker is touched?

This is my relevant code:

// .... more code up here


final GoogleMap map = mMapFragment.getMap();

if (map == null) {
    mReloadWhenReady = true;
    return;
}

map.clear();

final TileProviderMapsV2 prov = new TileProviderMapsV2(mMapFragment.getActivity());
map.addTileOverlay(new TileOverlayOptions().tileProvider(prov));


final LatLngBounds.Builder bounds = new LatLngBounds.Builder();
final Map<Marker, MyNode> markerMap = J.newHashMap();
mNodes.clear();
mNodes.addAll(nodes);


L.p("Maps v2 nodes: %d. ", mNodes.size());



for (MyNode node : mNodes) {
    final LatLng latlng = new LatLng(node.getLat(), node.getLon());
    final MarkerOptions opts = new MarkerOptions()
            .position(latlng)
            .title("Title is " + node.getName())
            .icon(BitmapDescriptorFactory
            .fromResource(com.mydomain.droid.R.drawable.pin_blue));

    final Marker m = map.addMarker(opts);
    markerMap.put(m, node);
    bounds.include(latlng);
}

map.setOnInfoWindowClickListener(new GoogleMap.OnInfoWindowClickListener() {

    @Override
    public void onInfoWindowClick(Marker marker) {
        final MyNode node = markerMap.get(marker);
        L.p("Maps v2 Clicked Node: %s/%d. ", node.getName(),node.getId());
        ViewNode.Launchr.setNodeId(node.getId()).launch(mMapFragment.getActivity());
    }
});

If I add m.showInfoWindow(); inside the for loop the marker will display the info window, however in the future there will be a lot more markers, so requirement is to only show the info window when the marker is touched.

For debugging purposes I added the following code, and I'm seeing the message in the logs along with the correct title, but I'm not getting the info window:

map.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener() {

    @Override
    public boolean onMarkerClick(Marker marker) {
        L.p("marker clicked " + marker.getTitle());
        marker.showInfoWindow();
        return false;
    }
});

Thanks!

Upvotes: 6

Views: 950

Answers (2)

ThanosFisherman
ThanosFisherman

Reputation: 5859

Not sure why you are having this rendering issue but I'm gonna give you some tips that may or may not fix it.

First of all you should get rid of the if block that checks if map is ready and also get rid of the mReloadWhenReady flag. The correct way to initialize a google map is using the getMapAsync from your Map fragment.

So implement the GoogleMap.OnInfoWindowClickListener and the OnMapReadyCallback in your class And do the following to initialize, setup and use a map.

declare a member map variable

private GoogleMap map = null;

Then somewhere in your class initialize that map

mMapFragment.getMapAsync(this);

then write some code inside those overriden methods

 @Override
    public void onMapReady(GoogleMap googleMap)
    {
        //here you can be sure the map is initialized correctly
        this.map = googleMap;
         //here we set the info window listener
        map.setOnInfoWindowClickListener(this); 
        setupMap() //your custom method for doing whatever you want to do with the map see below
    }

    //Do your stuff on onInfoWindowClick as usual
    @Override
    public void onInfoWindowClick(Marker marker) {
        final MyNode node = markerMap.get(marker);
        L.p("Maps v2 Clicked Node: %s/%d. ", node.getName(),node.getId());
        ViewNode.Launchr.setNodeId(node.getId()).launch(mMapFragment.getActivity());
    }



private void setupMap()
 {

    map.clear();

    final TileProviderMapsV2 prov = new TileProviderMapsV2(mMapFragment.getActivity());
    map.addTileOverlay(new TileOverlayOptions().tileProvider(prov));


    final LatLngBounds.Builder bounds = new LatLngBounds.Builder();
    final Map<Marker, MyNode> markerMap = J.newHashMap();
    mNodes.clear();
    mNodes.addAll(nodes);


    L.p("Maps v2 nodes: %d. ", mNodes.size());



    for (MyNode node : mNodes) {
        final LatLng latlng = new LatLng(node.getLat(), node.getLon());
        final MarkerOptions opts = new MarkerOptions()
                .position(latlng)
                .title("Title is " + node.getName())
                .icon(BitmapDescriptorFactory
                .fromResource(com.mydomain.droid.R.drawable.pin_blue));

        final Marker m = map.addMarker(opts);
        markerMap.put(m, node);
        bounds.include(latlng);
 }

Upvotes: 1

user4909407
user4909407

Reputation:

Try doing this:

map.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener() {

    @Override
    public boolean onMarkerClick(Marker marker) {
        marker = m;
        L.p("marker clicked " + marker.getTitle());
        marker.showInfoWindow();
        return false;
    }
});

Upvotes: 0

Related Questions