핀 이동 후 MKMapView 새로 고침
-
19-09-2019 - |
문제
사용자 정의 AnnotationView가 새 좌표로 업데이트됩니다.그러나 문제는 MKMapView를 사용한 일부 조작 후에만 시각적으로 업데이트된다는 것입니다.확대/축소 또는 이동.지도의 시각적 위치를 수동으로 업데이트하려면 어떻게 해야 합니까?
추신.지역을 현재 지도의 지역으로 변경하려고 했습니다.하지만 확대/축소가 변경됩니다.이상해요.
[mapView setRegion:[mapView region] animated:YES];
해결책
나는 몇 시간 동안 조사한 후에 약간 충격을 받았습니다.대답은 다음과 같습니다.
[mapView setCenterCoordinate:mapView.region.center animated:NO];
이유를 묻지 마세요. 지도뷰를 업데이트하고 그것이 제가 필요했던 것입니다.
다른 팁
MKMapView
다음을 통해 주석의 좌표 속성을 관찰합니다. KVO
.적절한 관찰만 하면 됩니다 KVO
프로토콜을 작성하고 주석을 보냅니다. willChangeValueForKey:
그리고 didChangeValueForKey:
키패스 포함 @"coordinate"
좌표를 업데이트하기 전과 후.
비슷하게 title
그리고 subtitle
에 의해서도 관찰된다 MKMapView
.따라서 이를 업데이트하고 콜아웃의 값을 사용자의 노력 없이 자동으로 변경하려면 다음과 같이 하십시오.부르다 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가 있습니다.지도에서 원하는 개체의 ID 포인터를 지도 보기에 추가하고 좌표 속성을 새 위치로 업데이트하면 MKMapView가 나머지 작업을 자동으로 수행합니다.
무료 점심을 먹을 수 있는 한 최대한 가까이 다가가세요!
저는 비동기 호출, 최소 0.5 지연으로 이 오류를 해결했습니다.
예: [self performSelector:@selector(redrawPins) withObject:nil afterDelay:0.5];
여기서 "redrawPins"는 핀을 추가하고 제거하는 기능입니다.
주석을 제거했다가 다시 추가할 수 없는 이유는 없습니다.비록 가짜 이동이라 할지라도 전체 지도를 이동하는 것보다 훨씬 더 성능이 좋을 것입니다.
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