infoWindow's image not showing on first click, but it works on second click

前端 未结 3 1927
逝去的感伤
逝去的感伤 2020-12-21 14:49

My android using the Google map android API,InfoWindow\'s image not showing on first click, but it works on second click

I customize the infoWindow using

         


        
相关标签:
3条回答
  • 2020-12-21 15:31

    I think Google has been listening and here's the solution that works for me. While setting up the cluster,

    getMap().setOnMapLoadedCallback(mOnMapLoaded);
    

    And once the map gets loaded, all the markers can be retreived from the clusterManager,

    private GoogleMap.OnMapLoadedCallback mOnMapLoaded = () -> {
        LogUtil.i(TAG, "Map has been loaded.");
        showInfoWindow();
    };
    
    private boolean showInfoWindow() {
        final WorkHeader selected = mWorkContainer.getSelectedHeader();
        Collection<Marker> markers = mClusterManager.getMarkerCollection().getMarkers();
        for (Marker marker : markers) {
            if (marker.getTitle().contains(selected.siteName)) {
                if (marker.getTitle().contains(selected.siteAddress)) {
                    mClusterManager.onMarkerClick(marker);
                    return true;
                }
            }
        }
        return false;
    }
    
    0 讨论(0)
  • 2020-12-21 15:47

    First you can make a custom callback class to implement the com.squareup.picasso.Callback:

     private class InfoWindowRefresher implements Callback {
            private Marker markerToRefresh;
    
            private InfoWindowRefresher(Marker markerToRefresh) {
                this.markerToRefresh = markerToRefresh;
            }
    
            @Override
            public void onSuccess() {
                markerToRefresh.showInfoWindow();
            }
    
            @Override
            public void onError() {}
        }
    

    Second, declare a boolean variable in your activity:

    boolean not_first_time_showing_info_window;
    

    Third, implement the public View getInfoContents(Marker marker) method:

       @Override
       public View getInfoContents(Marker marker) {
          View v = getLayoutInflater().inflate(R.layout.custom_window, null);
          ImageView image = (ImageView)v.findViewById(R.id.image_view);
    
          if (not_first_time_showing_info_window) {
              Picasso.with(MainActivity.this).load("image_URL.png").into(image);
    
          } else {
              not_first_time_showing_info_window = true;                         
              Picasso.with(MainActivity.this).load("image_URL.png").into(image, new InfoWindowRefresher(marker));
          }
          return v;
       }
    

    You can also visit this GitHub page for completed implementation.

    0 讨论(0)
  • 2020-12-21 15:48

    i had the same problem using Glide rather than Picasso but i made a simple solution i don't know how efficient it will be but it works fine with me , anyway i would like to share it with you

    private Marker lastClicked;
    private HashSet<String> markerIdSet=new HashSet<>();
    
    @Override
    public boolean onMarkerClick(Marker marker) {
        lastClicked=marker;
        if(!markerIdSet.contains(marker.getId()))
        {
            marker.showInfoWindow();
            marker.hideInfoWindow();
            Handler handler=new Handler();
            handler.postDelayed(new Runnable() {
                @Override
                public void run() {
                    lastClicked.showInfoWindow();
                }
            },150);
            markerIdSet.add(marker.getId());
            return true;
        }
        else
        {
            marker.showInfoWindow();
            return true;
        }
    }
    

    so basically you will creat a HashSet of String (markerID) if the HashSet does not contain that marker id so we add it to the HashSet, and call the marker.showInfoWindow(); to show the infowindow and marker.hideInfoWindow(); to hide it then using Handler object, code execution will wait 150ms and then show the infowindow calling marker.showInfoWindow(); again ,doing that you will get the photo from the first click.

    technically you are forcing the code to click the marker twice automatically for the first time of each marker.

    please let me know if it worked.

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