How to check if an Google Maps InfoWindow is still displayed before updating it?

前端 未结 2 789
长发绾君心
长发绾君心 2021-02-04 18:16

I started working on an InfoWindowAdapter that is displaying location images inside the InfoWindow. I have a lot of different locations, therefore it is possible that an locatio

相关标签:
2条回答
  • 2021-02-04 18:59

    I've just been having a similar problem with AsyncTasks downloading and updating an InfoWindow and after much banging my head against the wall this morning I've come up with this little workaround that should hopefully service your needs until Google sort this one out.

    I was calling marker.showInfoWindow() in the OnPostExecute() method of my AsyncTask, which re-called the InfoWindowAdapter methods, which was ending up in a loop and never propagating my changes correctly.

    The solution I used was to store the currently selected marker and the InfoWindow View I wanted displayed. I've chucked in an example below where the TextView is being updated on the DownloadBubbleInfo AsyncTask (similar to your image thread I believe).

                // Setting a custom info window adapter for the google map
            gMap.setInfoWindowAdapter(new InfoWindowAdapter() {
    
                // Use default InfoWindow frame
                public View getInfoWindow(Marker arg0) {
                    return null;
                }
    
                // Defines the contents of the InfoWindow
                public View getInfoContents(Marker arg0) {
                    if (selectedMarker.isInfoWindowShown()) {
                        return infoWindowView;
                    } else {
                        // Getting view from the layout file info_window_layout
                        infoWindowView = getLayoutInflater().inflate(
                                R.layout.bubblewindowlayout, null);
                        // Stash the base view in infoWindowView
                        // Getting reference to the TextView to set latitude
                        TextView tvTit = (TextView) infoWindowView
                                .findViewById(R.id.tv_title);
                        tvTit.setText("Fetching data...");
    
                        // Async the update so we're not slowed down waiting for
                        // the
                        // bubble to populate
                        new DownloadBubbleInfo(context, infoWindowView, arg0)
                                .execute(arg0.getTitle(), arg0.getSnippet());
    
                        // Returning the view containing InfoWindow contents
                        return infoWindowView;
                    }
                }
            });
            gMap.setOnMarkerClickListener(new OnMarkerClickListener() {
    
                public boolean onMarkerClick(Marker marker) {
                    // When a marker is clicked set it as the selected marker so
                    // we can track it for the InfoWindow adapter. This will
                    // make sure that the correct marker is still displayed when
                    // the callback from DownloadBubbleInfo is made to
                    // marker.showInfoWindow() which is needed to update the
                    // InfoWindow view.
                    selectedMarker = marker;
                    infoWindowView = null;
                    return false;
                }
            });
    

    And the relevant lines from the DownloadBubbleInfo AsyncTask:

        @Override
    protected String[] doInBackground(String... queryparts) {
        // Do the query and stash the results in queryResults and pass to
        // onPostExecute to attach to the mainview (the current view from the
        // main code) and then call showInfoWindow on the marker to re-launch
        // the InfoWindowAdapter methods again to repopulate the InfoWindow view
        // and attach it.
        return queryResults;
    }
    protected void onPostExecute(String[] results) {
        ((TextView) mainview.findViewById(R.id.tv_title)).setText(results[0]);
        ((TextView) mainview.findViewById(R.id.tv_info)).setText(results[1]);
    
        marker.showInfoWindow();
        Log.i("Chris-Debug", "Reshowing InfoWindow");
    }
    

    Now, all of this should make sure the correct marker is being populated with the correct information returned from your AsyncTask and hey presto another corner of the extremely awkward GoogleMaps v2 for Android API successfully circumnavigated!

    0 讨论(0)
  • 2021-02-04 19:00

    Might Marker's boolean isInfoWindowShown() method be what you are looking for?

    Marker#isInfoWindowShown() - Google Maps Android API v2 documentation

    It can't help when the InfoWindow was scrolled away from, though, for that, I think, you'll probably have to convert Marker's LatLng coordinates to screen coordinates or use this to check whether the marker is still on screen:

    How to get Latitude/Longitude span in Google Map V2 for Android

    I'm not sure whether it's possible at all to check if the window itself is still in map view bounds or not.

    0 讨论(0)
提交回复
热议问题