2009-07-27 36 views
9

AnnotationView tùy chỉnh được cập nhật với các tọa độ mới. Nhưng vấn đề là nó chỉ cập nhật trực quan sau một số thao tác với MKMapView, ví dụ: phóng to hoặc di chuyển. Tôi nên làm gì để cập nhật vị trí hình ảnh trên bản đồ theo cách thủ công?Làm mới MKMapView sau khi di chuyển pin

PS. Tôi đã cố gắng thay đổi khu vực thành khu vực bản đồ hiện tại. Nhưng nó thay đổi zoom. Nó thật kì lạ.

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

Vui lòng xem câu trả lời của tôi tại liên kết sau: http://stackoverflow.com/a/24564868/665961 –

Trả lời

19

Tôi hơi bị sốc sau nhiều giờ nghiên cứu. Câu trả lời chỉ là:

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

Đừng hỏi tôi tại sao, nhưng nó cập nhật bản đồ và đó là những gì tôi cần.

+1

UPD: không hỗ trợ SDK 3.1. Vẫn còn trong nghiên cứu. – slatvick

+1

Không hoạt động trong 3.1.2. – bradheintz

+0

Điều này vừa sửa lỗi của tôi! Cảm ơn!!!!! –

-7

Dưới đây là giao diện để 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  

Và đây là việc thực hiện:

// 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 quan sát tài sản của chú thích phối hợp qua KVO. Bạn chỉ cần quan sát giao thức KVO thích hợp và gửi chú thích willChangeValueForKey:didChangeValueForKey: với đường phím của @"coordinate" trước và sau khi bạn cập nhật tọa độ.

Tương tự, titlesubtitle cũng được quan sát bởi MKMapView. vì vậy, nếu bạn cập nhật những điều đó và muốn giá trị trong chú dẫn tự động thay đổi mà không cần nỗ lực nào từ phía bạn, chỉ cần làm như vậy: gọi willChangeValueForKey:didChangeValueForKey:

+0

Điều đó thực sự tốt để biết, cảm ơn. – samvermette

+0

Trong khi 'setCenterCoordinate: animated:' works, cách KVO ở đây cũng hoạt động và chắc chắn là thích hợp hơn và cho phép 'MKMapView' phối hợp các cập nhật từ nhiều quan sát tốt hơn. –

-2

Không có lý do gì bạn không thể xóa và sau đó thêm lại chú thích . Đó có lẽ là cách hiệu quả hơn di chuyển toàn bộ bản đồ, ngay cả khi nó là một động thái giả mạo.

+0

Ai đó muốn giải thích lý do tại sao điều này đã được bình chọn? Đây là ít nhất là một thay thế và tôi sẽ cho rằng nó có hiệu suất cao hơn. –

+0

Tôi chắc chắn rằng hầu hết những người tìm thấy chủ đề này đã thử điều này; Tôi biết tôi làm được. Điều này gây ra hành vi kỳ quái. –

2

Câu trả lời ở đây KHÔNG làm mới MapView hoặc Chú thích!

thuộc tính tọa độ của MKAnnotation có KVO trên đó. Nếu bạn chỉ cần thêm con trỏ id, của đối tượng bạn muốn trên bản đồ, vào mapview và cập nhật thuộc tính tọa độ với một vị trí mới, MKMapView sẽ làm phần còn lại cho bạn.

Gần như bạn có thể dùng bữa trưa miễn phí!

9

nếu bạn thêm các annoations của bạn từ một chủ đề nó sẽ không hoạt động. tôi đã có cùng một vấn đề và chỉ gói chức năng của tôi đã được bổ sung thêm các chú thích với những điều sau đây làm việc

[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

tôi giải quyết lỗi này với một cuộc gọi không đồng bộ, ít nhất là 0,5 chậm trễ.

ví dụ .: [self performSelector:@selector(redrawPins) withObject:nil afterDelay:0.5];

đâu "redrawPins" là chức năng cho phép thêm và loại bỏ chân.

Các vấn đề liên quan