简体   繁体   中英

Google Maps Android v2 - Detecting Marker InfoWindow Close

Right now I have markers that when clicked, begin autorefreshing the contents of their info windows in the background.

The thing is, once the user closes the info window (like clicking elsewhere on the map), the background task still goes on.

Is there a way to detect when a marker InfoWindow is closed so I can close the task then?

您可以在地图上使用setOnMapClickListener,并且可以在onMapClick方法内检查任务是否仍在运行,如果是,则结束任务。

You can only have a single InfoWindow open at a time, which means you can track the one that's open:

mMap.setInfoWindowAdapter(new GoogleMap.InfoWindowAdapter() {
    @Override
    public View getInfoWindow(Marker marker) {
        Log.e(TAG, "Info window requested for " + marker);
        mLastMarker = marker;
        return null; // Returning null will load the default InfoWindow
    }

    @Override
    public View getInfoContents(Marker marker) {
        return null;
    }
});

Now on each map click you can check if the task on a specific marker is still running:

mMap.setOnMapClickListener(new GoogleMap.OnMapClickListener() {
    @Override
    public void onMapClick(LatLng latLng) {
        if (mLastMarker != null) {
            mLastMarker = null;
            // Stop task
        }
    }
});

If you want, you can also stop the tasks when changing the InfoWindow :

@Override
public View getInfoWindow(Marker marker) {
    if (mLastMarker != null) {
        // Stop task for mLastMarker
    }

    Log.e(TAG, "Info window requested for " + marker);
    mLastMarker = marker;
    return null; // Returning null will load the default InfoWindow
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM