Search code examples
objective-cgoogle-maps-sdk-ios

Google Maps iOS SDK : access InfoWindow after creation


When I click on a marker, I'd like to show quickly an InfoWindow with basic information, then call a webservice that returns me the distance / path to the marker.
I have to call the webservice and not directly the Google Maps SDK because the path is modified to go to some points.

I tried that code :

- (void)fillInfoWindowForMarker:(GMSMarker *)marker {
    // get infos
    NSURL * url = [WSHelper.sharedHelper getPathURLForOrigin:origin destination:dest inGreenPath:greenPath];

    NSDictionary * result = [WSHelper.sharedHelper getPathAtUrl:url];

    GMSPath * path = [result valueForKey:@"path"];
    NSString * distance = [result valueForKey:@"distance"];

    dispatch_async(dispatch_get_main_queue(), ^{
        pathLine = [GMSPolyline polylineWithPath:path];
        pathLine.strokeWidth = 2;
        pathLine.strokeColor = UIColor.blackColor;
        pathLine.map = map;
        infoWindow.distanceLabel.text = @"something"
    });
}

- (UIView *)mapView:(GMSMapView *)mapView markerInfoWindow:(GMSMarker *)marker {
    if (infoWindow) {
        pathLine.map = nil;
        infoWindow = nil;
        pathLine = nil;
    }

    InfoWindowView * view =  [[[NSBundle mainBundle] loadNibNamed:@"InfoWindowView" owner:self options:nil] objectAtIndex:0];

    view.distanceLabel.text = @"0 km";

    infoWindow = view;
    view.tag = 678;
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_LOW, 0), ^{
        [self fillInfoWindowForMarker:marker];
    });

    return view;
}

Where infoWindow is a UIView subclass attribute of my controller.

The path is drawn on the map, but the label is not updated. I tried to print the map subviews, but there is no InfoWindow inside.
How could I achieve this ?


Solution

  • The view returned by markerInfoWindow is not used as a real view. Instead the SDK takes a screenshot of it, and then draws that screenshot.

    There's a workaround described here to force an info window to be refreshed:

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

    So, you could fetch your data, cache it somewhere, and then force a refresh. The new call to markerInfoWindow triggered by the refresh could then access the cached data and return the updated view.