카테고리 없음

[iOS] MapView 다른 색상으로 여러 주석 추가

행복을전해요 2021. 1. 21. 15:30

MKAnnotation 프로토콜도 구현하는 사용자 정의 클래스는 자체 변수를 사용할 수있는 기회이기도합니다. 원하는대로 이름을 지정할 수있는 redColor라는 속성을 만들었습니다.

MyAnnotation.h

#import <Foundation/Foundation.h>
#import <MapKit/MapKit.h>

@interface MyAnnotation : NSObject<MKAnnotation>

@property (nonatomic, assign, getter = isRedColor) BOOL redColor;
@property (nonatomic, assign) CLLocationCoordinate2D coordinate;
@property (nonatomic, copy) NSString *title;
@property (nonatomic, copy) NSString *subtitle;

- (id)initWithCoordinate:(CLLocationCoordinate2D)coordinate title:(NSString*)title;


@end

MyAnnotation.m

#import "MyAnnotation.h"

@implementation MyAnnotation

- (id)initWithCoordinate:(CLLocationCoordinate2D)coordinate title:(NSString*)title{
    if(self = [super init]){
        _coordinate = coordinate;
            _title = title;
              }
                return self;
                }
                
                @end
                

ViewController.h

#import "ViewController.h"
#import <MapKit/MapKit.h>
#import "MyAnnotation.h"

@interface ViewController ()<MKMapViewDelegate>
@property (weak, nonatomic) IBOutlet MKMapView *mapView;


@end

@implementation ViewController

- (void)viewDidLoad
{
  [super viewDidLoad];
  
    NSMutableArray *allAnnotations = [NSMutableArray array];
      CLLocationCoordinate2D coordinate = CLLocationCoordinate2DMake( 50.8500,4.3500);
        MyAnnotation *annotation = [[MyAnnotation alloc] initWithCoordinate:coordinate title:@"Brussels"];
          annotation.redColor = YES;
          
            [allAnnotations addObject:annotation];
            
            
              coordinate = CLLocationCoordinate2DMake(28.61389, 77.20889);
                annotation = [[MyAnnotation alloc] initWithCoordinate:coordinate title:@"New Delhi"];
                  annotation.redColor = NO;
                  
                    [allAnnotations addObject:annotation];
                    
                    
                      coordinate = CLLocationCoordinate2DMake(60.1708, 24.9375);
                        annotation = [[MyAnnotation alloc] initWithCoordinate:coordinate title:@"Helsinki"];
                          annotation.redColor = YES;
                          
                            [allAnnotations addObject:annotation];
                            
                              coordinate = CLLocationCoordinate2DMake(51.5072, 0.1275);
                                annotation = [[MyAnnotation alloc] initWithCoordinate:coordinate title:@"London"];
                                  annotation.redColor = NO;
                                  
                                    [allAnnotations addObject:annotation];
                                    
                                      coordinate = CLLocationCoordinate2DMake(39.9139, 116.3917);
                                        annotation = [[MyAnnotation alloc] initWithCoordinate:coordinate title:@"Beijing"];
                                          annotation.redColor =YES;
                                            [allAnnotations addObject:annotation];
                                            
                                              [self.mapView showAnnotations:allAnnotations animated:YES];
                                              
                                                [self.mapView addAnnotations:allAnnotations];
                                                
                                                }
                                                 #pragma mark - MKMapViewDelegate
                                                 
                                                 - (MKAnnotationView*)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation{
                                                   MKPinAnnotationView *pinView = (MKPinAnnotationView*)[mapView dequeueReusableAnnotationViewWithIdentifier:@"PinView"];
                                                     if(!pinView){
                                                         pinView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"PinView"];
                                                                 pinView.canShowCallout = YES;
                                                                   }
                                                                     MyAnnotation *myannotation = (MyAnnotation*)annotation;
                                                                       if(myannotation.redColor)
                                                                           pinView.pinColor = MKPinAnnotationColorRed;
                                                                             else
                                                                                 pinView.pinColor = MKPinAnnotationColorGreen;
                                                                                   return pinView;
                                                                                   }
                                                                                   @end
                                                                                   
-------------------

여기서 문제는 addAnnotations메서드가 아마도 동기식이 아니라는 것입니다. 즉, viewForAnnotation대리자 메서드는에 대한 호출 직후에 추가되지 않습니다 addAnnotations.

이 문제를 해결하는 올바른 방법은 주석에 대한 클래스를 만들고 여기에 색상 속성을 추가하는 것입니다. 귀하의 코드에서는 주석 클래스를 볼 수 없습니다.

아래 절차에 따라 문제를 해결하십시오.

  1. MKAnnotation프로토콜 을 구현하는 클래스를 만듭니다 .
  2. BOOL이름이 지정된 속성 fromSelectedTab(또는 해당 사항에 대한 다른 항목)을 추가합니다 .
  3. 이러한 클래스의 배열을 만들고 올바른 색상 속성을 설정합니다.
  4. 지도보기에 주석을 추가합니다.
  5. 에서 viewForAnnotation방법, 주석 클래스를 캐스팅하고 색상 속성을 얻을.
  6. MKPinAnnotationView pinColor주석 클래스의 속성을 기반으로 속성을 설정 합니다.

이제 핀이 올바른 색상이어야합니다.

또한 dequeueReusableAnnotationViewWithIdentifier:새로운 MKAnnotationView를 할당 할 때 메서드 를 사용하는 것을 잊지 마십시오 .



출처
https://stackoverflow.com/questions/22009782