MKMapView refresh after pin moves

2019-01-22 22:37发布

A custom AnnotationView is updated with new coordinates. But the problem is that it visually updates only after some manipulations with MKMapView, e.g. zooming or moving. What should I do to manually update visual position on a map?

PS. I've tried to change region to current map's region. But it does change zoom. It's strange.

[mapView setRegion:[mapView region] animated:YES];

7条回答
做个烂人
2楼-- · 2019-01-22 23:04

if your adding your annoations from a thread it wont work. i had the same problem and just wrapping my function that was adding the annotations with the following worked

[self performSelectorOnMainThread:@selector(addCameraIconOnMain:) obj waitUntilDone:true];  

-(void) addCameraIconOnMain:(myobjecttype*)obj
{
    // this isnt the entire function, customize for your own purpose.....
    [mapView addAnnotation:annotation];
}
查看更多
太酷不给撩
3楼-- · 2019-01-22 23:05

There's no reason you can't remove and then re-add the annotation. That's probably way more performant than moving the entire map, even if its a fake move.

查看更多
啃猪蹄的小仙女
4楼-- · 2019-01-22 23:05

Here is the interface to MapAnnotation:

//  CSMapAnnotation.h
//  mapLines
//  Created by Craig on 5/15/09.
//  Copyright 2009 Craig Spitzkoff. All rights reserved.

#import <Foundation/Foundation.h>
#import <MapKit/MapKit.h>    

// types of annotations for which we will provide annotation views. 
typedef enum {
    MapAnnotationTypeStart = 0,
    MapAnnotationTypeEnd   = 1,
    MapAnnotationTypeImage = 2
} MapAnnotationType;

@interface MapAnnotation : NSObject <MKAnnotation>
{
    CLLocationCoordinate2D _coordinate;
    MapAnnotationType      _annotationType;
    NSString*              _title;
    NSString*              _subtitle;
    NSString*              _userData;
    NSString*              speed;
    NSString*              identifier;
}

@property (nonatomic, retain) NSString *speed;
@property (nonatomic, retain) NSString *identifier;

-(id) initWithCoordinate:(CLLocationCoordinate2D)coordinate 
    annotationType: (MapAnnotationType) annotationType
    title: (NSString*) title
    subtitle: (NSString*) subtitle
    speed: (NSString *) speed
    identifier: (NSString *) identifier;    
-(id) setWithCoordinate: (CLLocationCoordinate2D) coordinate 
    annotationType: (MapAnnotationType) annotationType
    title: (NSString*) title
    subtitle: (NSString*) subtitle
    speed: (NSString*) speed
    identifier: (NSString*) identifier;    
@property MapAnnotationType annotationType;
@property (nonatomic, retain) NSString* userData;    
@end    

And here is the implementation:

//  CSMapAnnotation.m
//  mapLines
//  Created by Craig on 5/15/09.
//  Copyright 2009 Craig Spitzkoff. All rights reserved.

#import "MapAnnotation.h"    

@implementation MapAnnotation

@synthesize coordinate     = _coordinate;
@synthesize annotationType = _annotationType;
@synthesize userData       = _userData;
@synthesize speed;
@synthesize identifier;

-(id) initWithCoordinate:(CLLocationCoordinate2D)coordinate 
    annotationType: (MapAnnotationType) annotationType
    title: (NSString*)title
    subtitle: (NSString*) subtitle
    speed: (NSString *) speedz
    identifier: (NSString *) identifierz
{
    self = [super init];
    _coordinate = coordinate;
    _title  = [title retain];
    _subtitle = [subtitle retain];
    _annotationType = annotationType;
    speed = speedz;
    identifier = identifierz;
    return self;
}    
-(id) setWithCoordinate:(CLLocationCoordinate2D)coordinate 
    annotationType: (MapAnnotationType) annotationType
    title: (NSString*) title
    subtitle: (NSString*) subtitle
    speed: (NSString*) speedz
    identifier: (NSString*) identifierz
{
    _coordinate = coordinate;
    _title = [title retain];
    _subtitle = [subtitle retain];
    _annotationType = annotationType;
    speed = speedz;
    identifier = identifierz;       
    return self;
}    
-(NSString*) title
{
    return _title;
}    
-(NSString*) subtitle
{
    return _subtitle;
}    
-(void) dealloc
{
    [_title    release];
    [_userData release];
    [super dealloc];
}    
@end
查看更多
放荡不羁爱自由
5楼-- · 2019-01-22 23:16

I solved this error with an asynchronous call, at least 0.5 delay.

e.g.: [self performSelector:@selector(redrawPins) withObject:nil afterDelay:0.5];

Where "redrawPins" is the function which adds and removes pins.

查看更多
Juvenile、少年°
6楼-- · 2019-01-22 23:25

I am a little shoked after hours of research. The answer is just:

[mapView setCenterCoordinate:mapView.region.center animated:NO];

Do not ask me why, but it updates a mapview and it's what i was need.

查看更多
别忘想泡老子
7楼-- · 2019-01-22 23:27

MKMapView observes the coordinate property of annotations via KVO. You simply need to observe proper KVO protocol and send the annotation willChangeValueForKey: and didChangeValueForKey: with keypath of @"coordinate" before and after you update the coordinates.

Likewise title and subtitle are also observed by MKMapView. so if you update those and want the value in the callout to change automatically without any effort on your part, just do the same: call willChangeValueForKey: and didChangeValueForKey:

查看更多
登录 后发表回答