Search code examples
iosgoogle-mapsafnetworking-2

How to refresh marker info window in iOS Google maps with AFnetworking Image cache?


According to the document from Google, the info window was rendered before I get the image.

Note: The info window is rendered as an image each time it is displayed on the map. 
This means that any changes to its properties while it is active will not be immediately visible.     
The contents of the info window will be refreshed the next time that it is displayed.

Are there anyone is using AFNetworking's image cache and successfully update the infowindow view without polluted the marker's snippet? I've searched but I couldn't find any solution for my scenario. I have address and some other information in the marker's snippet, so I can't use it as a flag.

BTW, I'm following this tutorial from Google to create the info window from xib

Refs:

How to force refresh contents of the markerInfoWindow in Google Maps iOS SDK

iOS: Dynamic Marker Info Window

https://developers.google.com/maps/documentation/ios/marker#info_windows


Solution

  • I solved the problem as following

    I use this property as the info window.

    @property (nonatomic) MyInfoWindow* infoWindow 
    

    And this is how I wrote the delegate method of GMSMapViewDelegate

    - (UIView*)mapView:(GMSMapView*)mapView markerInfoWindow:(GMSMarker*)marker
    {
        //Prepare Data
        MyEvent* selectedEvent = [EventManager eventForEventID:marker.objectID];
        //Prepare Thumbnail URL
        NSURLRequest* request = [[NSURLRequest alloc] initWithURL:[selectedEvent getSmallBannerWithSize:@"w50"]];
    
        //Prepare InfoView's UIView
        _infoWindow = [[[NSBundle mainBundle] loadNibNamed:@"infoWindowView" owner:self options:nil] objectAtIndex:0];
        _infoWindow.title.text = selectedEvent.eventName;
    
        //Get cached image from AFNetworking
        UIImage* img = [[UIImageView sharedImageCache] cachedImageForRequest:request];
        if (img == nil) {
            [_infoWindow.thumbnail setImageWithURLRequest:request 
                                   placeholderImage:[UIImage imageNamed:@"placeholder"] 
              success:^(NSURLRequest* request, NSHTTPURLResponse* response, UIImage* image) {
                //Invoke selection to call this delegate method again
                [mapView setSelectedMarker:marker]; 
            } failure:^(NSURLRequest* request, NSHTTPURLResponse* response, NSError* error) {
                NSLog(@"RY fetching thumbnail image error : %@", request);
            }];
        } else {
            // If we can get the cached image, which means the image is downloaded, 
            // then update the info window
            _infoWindow.thumbnail.image = img;
        }
        return _infoWindow;
    }