Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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!

like image 842
zundi Avatar asked Jan 22 '16 16:01

zundi


People also ask

How do I hide markers on Google Maps?

Step 1 Go to Add or Edit Map and Scroll down to the 'Infowindow Settings' section. Step 2 Enable the box of 'Hide Markers on Page Load' option. Step 3 Click on Save Map and open it in browser.

How do I put InfoWindow on marker?

Call setPosition() on the info window, or. Attach the info window to a new marker using the InfoWindow. open() method. Note: If you call open() without passing a marker, the InfoWindow will use the position specified upon construction through the InfoWindowOptions object literal.

How do you change the InfoWindow position on Google Maps?

An InfoWindow can be placed on a map at a particular position or above a marker, depending on what is specified in the options. Unless auto-pan is disabled, an InfoWindow will pan the map to make itself visible when it is opened. After constructing an InfoWindow, you must call open to display it on the map.


1 Answers

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);
 }
like image 119
ThanosFisherman Avatar answered Sep 21 '22 15:09

ThanosFisherman