MKMapView refresh after pin moves

前端 未结 7 1244
猫巷女王i
猫巷女王i 2020-12-14 23:42

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. Wh

相关标签:
7条回答
  • 2020-12-14 23:55

    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:

    0 讨论(0)
  • 2020-12-15 00:00

    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
    
    0 讨论(0)
  • 2020-12-15 00:02

    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];
    }
    
    0 讨论(0)
  • 2020-12-15 00:03

    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.

    0 讨论(0)
  • 2020-12-15 00:04

    The answer here is NOT to refresh the MapView or the Annotation!

    the coordinate property of MKAnnotation has KVO on it. If you just add the id pointer, of the object you want on the map, to the mapview and update the coordinate property with a new location, MKMapView will do the rest for you.

    As close as you can get to a free lunch!

    0 讨论(0)
  • 2020-12-15 00:16

    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.

    0 讨论(0)
提交回复
热议问题