2009-07-27 29 views
9

用新座標更新自定義AnnotationView。但問題在於,它只能在使用MKMapView進行一些操作後直觀更新,例如縮放或移動。 手動更新地圖上的視覺位置應該怎麼做?引腳移動後MKMapView刷新

PS。我試圖將區域更改爲當前地圖的區域。但它確實會改變縮放。真奇怪。

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

請在以下鏈接中查看我的答案: http://stackoverflow.com/a/24564868/665961 – 2014-07-03 23:56:42

回答

19

經過幾個小時的研究,我有點sh sh不安。答案只是:

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

不要問我爲什麼,但它更新了一個mapview,這就是我所需要的。

+1

UPD:它對SDK 3.1沒有幫助。仍在研究中。 – slatvick 2009-10-07 23:04:17

+1

也不適用於3.1.2。 – bradheintz 2009-11-12 00:12:55

+0

這只是修復了我的錯誤!謝謝!!!!! – 2015-06-21 20:45:28

-7

這裏是接口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  

這裏是實現:

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

MKMapView觀察通過KVO座標標註的財產。您只需遵守適當的KVO協議,並在更新座標之前和之後發送註釋willChangeValueForKey:didChangeValueForKey:以及@"coordinate"的鍵路徑。

同樣titlesubtitle也被MKMapView觀察到。所以如果你更新了這些,並希望標註中的值自動更改而不需要費力,請執行以下操作:致電willChangeValueForKey:didChangeValueForKey:

-2

沒有理由不能刪除然後重新添加註釋。這可能比移動整個地圖更高效,即使它是假的移動。

2

這裏的答案不是刷新MapView或Annotation!

MKAnnotation的座標屬性上有KVO。如果您只是將地圖上想要的對象的id指針添加到mapview中,並使用新位置更新座標屬性,則MKMapView將爲您完成剩下的工作。

儘可能地接近免費的午餐!

9

如果你從一個線程添加你的annoations它不會工作。 我有同樣的問題,只是我的包裹功能與下面添加註釋,工作

[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.5延遲。

例如爲:[self performSelector:@selector(redrawPins) withObject:nil afterDelay:0.5];

其中「redrawPins」是指添加和刪除引腳的功能。