2011-11-04 2 views
0

내 MapView에서 SQLite의 데이터를 읽고 핀을 표시합니다 (최대 5000 레코드).iphone MapView의 조건부 맞춤 주석보기

데이터베이스의 ID 구조는 | 경도 | 위도 | 제목 | 자막 내가 핀 클릭 할 수 있도록이 코드를 사용

: 나는 데이터베이스에 새 열 (클릭 가능한)를 추가하고, 핀을 만들 필요가

pin.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure]; 

을 "클릭 가능한"값이 ON 단지 경우 클릭 가능한 .

그럴 수있는 가장 좋은 아이디어에 대한 자세한 제안이 있으십니까?

+0

? 맞춤 클래스 또는 MKPointAnnotation과 같이 미리 정의 된 클래스입니까? – Anna

+0

난이 사용 MKPinAnnotationView customPinView * = [[[MKPinAnnotationView의 ALLOC]을 \t \t \t \t \t \t \t \t \t \t \t \t initWithAnnotation : 주석 reuseIdentifier : UserAnnotationId] 오토 릴리즈]; customPinView.pinColor = MKPinAnnotationColorPurple; customPinView.animatesDrop = YES; customPinView.canShowCallout = YES; customPinView.rightCalloutAccessoryView = [UIButton buttonWithType : UIButtonTypeDetailDisclosure]; return customPinView; – asnanak

답변

2

주석 (제목, 부제목, 이미지, 액세서리 버튼)의 속성을 설정하지 않은 상태에서 핀을 탭하면 제 한자의 설명 선이 표시되지 않습니다. 액세서리 버튼을 누를 때 콜 아웃을 표시하고 액션을 호출하지 않으려면

(데이터를 db에서 다운로드 한 후 NSDictionary으로 저장할 수 있습니다. 그리고 내가 예를 들어 문자열을 사용 물론 NSArray의 모든 항목)

- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control{ 

    NSString *clickable=[[yourArray objectAtIndex:yourIndex] objectForKey:@"clickable"]; 
    if(![clickable isEqualToString:@"YES"]){ 
     return; 
    } 
} 

은 또한 NSNumber의 또는 BOOL의를 사용할 수 있습니다.

희망 사항은 귀하의 질문을 이해했습니다.

+0

UIButtonTypeDetailDisclosure를 숨기고 싶습니다. 그렇기 때문에 이전에 사용해야합니다. – asnanak

+0

및이 코드 줄을 읽으려고 할 때 프로그램 crach : NSString * clickable = [[yourArray objectAtIndex : yourIndex] objectForKey : @ "clickable"]; 배열과 인덱스를 올바르게 넣었다는 것을 알고 있습니다. – asnanak

+0

그것은 당신이 할 수있는 일의 예일뿐입니다. 단추를 숨기려면 설정하지 마십시오. – Mat

0

콜 아웃에 테이블의 "clickable"플래그를 기반으로 액세서리 버튼이 있는지 여부를 제어하려면 clickable 속성을 주석 클래스에 추가하고 주석을 추가 할 때 설정하는 것이 좋습니다. 그런 다음 viewForAnnotation 메소드에서 주석보기를 만들 때이 속성을 확인할 수 있습니다.

은 (당신이 addAnnotation를 호출 할 때 즉.) 현재 MKPointAnnotation 같은 미리 정의 된 클래스를 사용하는 경우, 당신은 MKAnnotation 프로토콜을 구현하는 대신 자신 만의 클래스를 정의해야합니다지도보기에 주석을 추가하려면 . 맞춤 클래스가 이미있는 경우 clickable 속성을 추가하십시오. 여기
//CustomAnnotation.h... 
@interface CustomAnnotation : NSObject<MKAnnotation> 
@property (nonatomic, assign) CLLocationCoordinate2D coordinate; 
@property (nonatomic, copy) NSString *title; 
@property (nonatomic, copy) NSString *subtitle; 
@property (nonatomic, assign) int annotationId; 
@property (nonatomic, assign) BOOL clickable; 
@end 

//CustomAnnotation.m... 
@implementation CustomAnnotation 
@synthesize coordinate; 
@synthesize title; 
@synthesize subtitle; 
@synthesize annotationId; 
@synthesize clickable; 
- (void)dealloc { 
    [title release]; 
    [subtitle release]; 
    [super dealloc]; 
} 
@end 

이 (당신의 SQL 행에서 올 것이다 주석 속성에 대한 실제 값)이 주석을 작성하고 추가 할 방법의 예 : 여기

사용자 정의 주석 클래스의 예

CustomAnnotation *ca1 = [[CustomAnnotation alloc] init]; 
ca1.annotationId = 1; 
ca1.coordinate = CLLocationCoordinate2DMake(lat1,long1); 
ca1.title = @"clickable"; 
ca1.subtitle = @"clickable subtitle"; 
ca1.clickable = YES; 
[myMapView addAnnotation:ca1]; 
[ca1 release]; 

CustomAnnotation *ca2 = [[CustomAnnotation alloc] init]; 
ca2.annotationId = 2; 
ca2.coordinate = CLLocationCoordinate2DMake(lat2,long2); 
ca2.title = @"not clickable"; 
ca2.subtitle = @"not clickable subtitle"; 
ca2.clickable = NO; 
[myMapView addAnnotation:ca2]; 
[ca2 release]; 

그런 다음 viewForAnnotation는 다음과 같이 표시됩니다

-(MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation 
{ 
    static NSString *reuseId = @"CustomAnnotation"; 

    MKPinAnnotationView* customPinView = (MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:reuseId]; 
    if (customPinView == nil) { 
     customPinView = [[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:reuseId] autorelease]; 
     customPinView.pinColor = MKPinAnnotationColorPurple; 
     customPinView.animatesDrop = YES; 
     customPinView.canShowCallout = YES; 
    } 
    else { 
     customPinView.annotation = annotation; 
    } 

    CustomAnnotation *ca = (CustomAnnotation *)annotation; 
    if (ca.clickable) 
     customPinView.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure]; 
    else 
     customPinView.rightCalloutAccessoryView = nil; 

    return customPinView; 
} 

마지막으로, 당신은 calloutAccessoryControlTapped 대리자 메서드에있는 버튼을 눌러를 처리하고 사용자 정의 주석의 속성에 액세스 할 수 있습니다 :

당신이 MKAnnotation 객체를 생성하는 데 사용하는 어떤 클래스
-(void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control 
{ 
    CustomAnnotation *ca = (CustomAnnotation *)view.annotation; 
    NSLog(@"annotation tapped, id=%d, title=%@", ca.annotationId, ca.title); 
}