سؤال

يتم تحديث AnnotationView مخصص مع إحداثيات جديدة. ولكن المشكلة هي أنه تحديثات بصريا فقط بعد بعض التلاعب مع mkmapview، مثل التكبير أو التحرك. ماذا علي أن أفعل لتحديث الموضع المرئي يدويا على الخريطة؟

ملاحظة. لقد حاولت تغيير المنطقة إلى منطقة الخريطة الحالية. لكنه يغير التكبير. هذا غريب.

[mapView setRegion:[mapView region] animated:YES];
هل كانت مفيدة؟

المحلول

أنا شوك قليلا بعد ساعات من البحث. الجواب هو فقط:

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

لا تسألني عن السبب، لكنها تقوم بتحديث MAPView وهي ما كنت بحاجة إليه.

نصائح أخرى

MKMapView يلاحظ خاصية تنسيق التعليقات التوضيحية عبر KVO. وبعد تحتاج ببساطة إلى مراقبة السليم KVO البروتوكول وإرسال التوضيح willChangeValueForKey: و didChangeValueForKey: مع keypath of. @"coordinate" قبل وبعد تحديث الإحداثيات.

بطريقة مماثلة title و subtitle ويلاحظ أيضا من قبل MKMapView. وبعد لذلك إذا قمت بتحديث أولئك وأريد أن تتغير القيمة في Callout تلقائيا دون أي جهد من جانبك، فما عليك سوى القيام بذلك: willChangeValueForKey: و didChangeValueForKey:

إذا قمت بإضافة عقهاتك من مؤشر ترابط، فلن تعمل. كان لدي نفس المشكلة وفترة التفاف لوظيفتي التي كانت تضيف التعليقات التوضيحية مع ما يلي

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

-(void) addCameraIconOnMain:(myobjecttype*)obj
{
    // this isnt the entire function, customize for your own purpose.....
    [mapView addAnnotation:annotation];
}

الجواب هنا هو عدم تحديث mapview أو التعليق التوضيحي!

ملكية الإحداثيات من Mkannotation لديها KVO على ذلك. إذا أضفت فقط مؤشر الهوية، والكائن الذي تريده على الخريطة، إلى MAPView وتحديث خاصية الإحداثيات بموقع جديد، فإن MKMAPView ستقوم بالباقي لك.

أقرب ما تستطيع الوصول إلى غداء مجاني!

أنا حل هذا الخطأ مع مكالمة غير متزامنة، على الأقل 0.5 تأخير.

على سبيل المثال: [self performSelector:@selector(redrawPins) withObject:nil afterDelay:0.5];

حيث "Redrawpins" هي الوظيفة التي تضيفها وتزيل دبابيس.

لا يوجد سبب لا يمكنك إزالته ثم إعادة إضافة التوضيح. ربما يكون هذا الأمر أكثر أداء من تحريك الخريطة بأكملها، حتى لو كانت خطوة وهمية.

هنا هي الواجهة إلى MAPANNOTITION:

//  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    

وهنا هو التنفيذ:

//  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
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top