iOS: Notification when MKMapView is loaded and annotations/overlays are added?

做~自己de王妃 提交于 2019-12-03 01:29:09

It's an old question, but if you're using iOS 7 just use the mapView mapViewDidFinishRenderingMap delegate.

- (void)mapViewDidFinishRenderingMap:(MKMapView *)mapView fullyRendered:(BOOL)fullyRendered
{
    // Image creation code here

}

The following code works on iOS 8.4 in Swift 1.2

func mapViewDidFinishRenderingMap(mapView: MKMapView!, fullyRendered: Bool) {
    // Your code on rednering completion
}

The problem with mapViewDidFinishRenderingMap is that is only includes the loading of the map tiles themselves, not the tiles plus the annotations. For this, you can use didAddAnnotationViews. To be safe, I would recommend observing both, and using which ever returns later.

Well, you could set three flags in your delegate:

- (void)mapViewDidFinishLoadingMap:(MKMapView *)mapView
{
    didFinishLoadingMap = YES;
    [self createImage];
}

- (void)mapView:(MKMapView *)mapView didAddAnnotationViews:(NSArray *)views
{
    didFinishAddingAnnotationViews = YES;
    [self createImage];
}

- (void)mapView:(MKMapView *)mapView didAddOverlayViews:(NSArray *)overlayViews

{
    didFinishAddingOverlayViews = YES;
    [self createImage];
}

...and then

- (void)createImage
{
    if (didFinishLoadingMap && didFinishLoadingAnnotationViews && didFinishAddingOverlayViews) {
        // Create the image
    }
}

...and then set all three flags back to NO in your mapViewWillStartLoadingMap: method. This might create an image slightly more often than necessary, in the scenario where map tiles are cached but both new overlays and new annotations scroll onto the screen – if you wanted to guard against that, you could use a UIPanGestureRecognizer to detect when the user pans or pinches the map, and reset those two flags to NO accordingly.

This is relatively easy if you get your mind out of the "map view". What I did with graphing something all at once was to put all my Requests into a total. I stored the total number of "requests" and when they finished their loading function, I had a check of "if requests left == 0" or similar, then I called the "Draw everything after".

So what you'd do is make the 3 separate types add to a "running total" (since a queue itself is different in Comp Sci, trying to avoid vocabulary conflict), then when they "run out", via having their "Draw rect" call or "init with coder" if it's a Xib annotation or whatever function you can guarantee since iOS UIView subclasses have absolutely no common initialization function like every other programming language (separate rant), basically that init calls a function on a common class, singleton, static int, delegate, whichever, and then see when it-runs out-.

I realize your project ended, but with so many upvotes, figured an answer wouldn't hurt. This answer would be much simpler if Objective C had a constructor. Either way, this should solve it, just takes some thought and time, as it's per-project on exactly how to implement it.

I have tried all ways from other answers.

It looks like impossible to get the time when it was fully loaded at least for iOS 11.1

I have found undocumented notification VKMapViewDidBecomeFullyDrawnNotification from VKMapView object but even it happens too early.

So it looks like only one way exist - wait couple seconds for full load and cross fingers to hope load the map was done.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!