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

6

26

I'm returning a UIImageView in - (UIView *)mapView:(GMSMapView *)mapView markerInfoWindow:(GMSMarker *)marker;

This UIImageView loads images dynamically through a URL using SDWebImage.

As the documentation says about marker info windows:

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.

The point is that after downloading the image the info window doesn't refresh and keep showing the placeholder image until user hides then show it back again..

So I need to force refresh the contents of the markerInfoWindow in the block of image downloaded..

Motherhood answered 27/6, 2013 at 11:47 Comment(0)
M
6

This is how I fixed the problem using the workaround bellow:

UIImage *img = [[SDWebImageManager sharedManager] imageWithURL:[NSURL URLWithString:Img_URL]]; //Img_URL is NSString of your image URL
    if (img) {       //If image is previously downloaded set it and we're done.
        [imageView setImage:img];
    }else{
        [imageView setImageWithURL:[NSURL URLWithString:Img_URL] placeholderImage:[UIImage imageNamed:@"defaultPin"] success:^(UIImage *image, BOOL cached) {
            if (!marker.snippet || !cached) {
                [marker setSnippet:@""];                 //Set a flag to prevent an infinite loop
                if (mapView.selectedMarker == marker) {  //Only set if the selected marker equals to the downloaded marker
                    [mpVu setSelectedMarker:marker];
                }
            }
        } failure:^(NSError *error) {

        }];
    }
Motherhood answered 28/6, 2013 at 18:53 Comment(1)
This answer is no longer correct, see @Enrico Susatyo's answer below (https://mcmap.net/q/520440/-how-to-force-refresh-contents-of-the-markerinfowindow-in-google-maps-ios-sdk).Antonantone
C
21

In GoogleMaps iOS SDK 1.13.0 and above, they have added this functionality. To enable this, simply set the tracksInfoWindowChanges property on GMSMarker to YES.

Example:

- (UIView *)mapView:(GMSMapView *)mapView markerInfoWindow:(GMSMarker *)marker {
    marker.tracksInfoWindowChanges = YES;

    MyInfoWindow *infoWindow = [[MyInfoWindow alloc] init];
    [infoWindow.imageView sd_setImageWithURL:imageUrl];
    return infoWindow;
}

Source:

Churchwoman answered 6/5, 2016 at 5:30 Comment(1)
why did it take them 3 years to implement this from the time of the suggestion? thats scaryFiducial
M
6

This is how I fixed the problem using the workaround bellow:

UIImage *img = [[SDWebImageManager sharedManager] imageWithURL:[NSURL URLWithString:Img_URL]]; //Img_URL is NSString of your image URL
    if (img) {       //If image is previously downloaded set it and we're done.
        [imageView setImage:img];
    }else{
        [imageView setImageWithURL:[NSURL URLWithString:Img_URL] placeholderImage:[UIImage imageNamed:@"defaultPin"] success:^(UIImage *image, BOOL cached) {
            if (!marker.snippet || !cached) {
                [marker setSnippet:@""];                 //Set a flag to prevent an infinite loop
                if (mapView.selectedMarker == marker) {  //Only set if the selected marker equals to the downloaded marker
                    [mpVu setSelectedMarker:marker];
                }
            }
        } failure:^(NSError *error) {

        }];
    }
Motherhood answered 28/6, 2013 at 18:53 Comment(1)
This answer is no longer correct, see @Enrico Susatyo's answer below (https://mcmap.net/q/520440/-how-to-force-refresh-contents-of-the-markerinfowindow-in-google-maps-ios-sdk).Antonantone
H
3

Based on your explanation , I understand you are looking to refresh your currently displayed infowindow after an image download so that the infowindow is redrawn with the new details.

You can do the below steps:

1, Programatically close the infowindow

2, Programatically show the infowindow for the same marker again

Hypermeter answered 27/6, 2013 at 18:57 Comment(0)
S
2

Swift based on Enrico Susatyo solution:

by setting the tracksInfoWindowChanges property of GMSMarker object to true

marker.title = "my marker"
marker.tracksInfoWindowChanges = true
Stavro answered 12/9, 2017 at 14:15 Comment(0)
C
0

@Shady Elyaski's answer is great, here's a swift reinterpretation with Parse, incase anyone else needs a had.

    var eventThumb:UIImage?
    ...

    func mapView(mapView: GMSMapView!, markerInfoWindow marker: GMSMarker!) -> UIView! {


    ...
    //set up the rest of your view...
        if let eventImgExists = eventThumb {
                    eventImg.image = eventImgExists
                    self.eventThumb = nil
                } else {
                    if let currentEventThumb = currentEvent["thumbnail"] as? PFFile {
                        currentEventThumb.getDataInBackgroundWithBlock {
                            (imageData, error) -> Void in
                            if error == nil {
                                let image = UIImage(data: imageData!)
                                eventImg.image = image
                                self.eventThumb = image
                                self.eventsMap.selectedMarker = self.eventsMap.selectedMarker
                            }
                    }
                }
            }
Commendation answered 14/11, 2015 at 1:38 Comment(0)
H
0

There you are . just a few lines of simple code . This work perfectly for me . Add these lines of code right after you finish download an image I mean in your completionBlock .

     let image = UIImage(contentsOfFile: fileURL.path!)

      marker.image  = image

            if(self.mapView.selectedMarker != nil)
            {
              if(self.mapView.selectedMarker == marker)
              {
                self.mapView.selectedMarker = nil
                self.mapView.selectedMarker =  marker
              }
            }

Then it works so freaking awesome . The content are reloaded with new image immediately Basically you still have to assign new image to your marker.image to use it in your "markerInfoContents" method

Hiatus answered 5/4, 2016 at 16:8 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.