2011-02-10 2 views
1

Я нашел краткое руководство по загрузке «данных аннотации» из plist и добавлению этих аннотаций (контактов) в MapView.Настроить аннотации карт от plist

Вопрос в том, может ли кто-нибудь сказать мне, как я мог бы настроить контакты наиболее легко? В качестве примера я хотел бы определить цвет и изображение в plist, а затем установить его при добавлении булавок.

Штифты добавляются так:

MyAnnotation.m:

@implementation MyAnnotation 
@synthesize title, subtitle, coordinate; 

-(void) dealloc { 
    [super dealloc]; 
    self.title = nil; 
    self.subtitle = nil; 
} 

MyAnnotation.h

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

@interface MyAnnotation : NSObject<MKAnnotation> { 
    CLLocationCoordinate2D coordinate; 
    NSString *title; 
    NSString *subtitle; 
} 

@property (nonatomic, assign) CLLocationCoordinate2D coordinate; 
@property (nonatomic, copy) NSString* title; 
@property (nonatomic, copy) NSString* subtitle; 

Запуск через PLIST в ViewDidLoad в MapViewController. m (массив containg dictiona для каждого булавки) и добавления аннотаций.

NSString *path = [[NSBundle mainBundle] pathForResource:@"Annotations" ofType:@"plist"]; 
    NSMutableArray* anns = [[NSMutableArray alloc] initWithContentsOfFile:path]; 

    for(int i = 0; i < [anns count]; i++) { 
     float realLatitude = [[[anns objectAtIndex:i] objectForKey:@"latitude"] floatValue]; 
     float realLongitude = [[[anns objectAtIndex:i] objectForKey:@"longitude"] floatValue]; 

     MyAnnotation* myAnnotation = [[MyAnnotation alloc] init]; 
     CLLocationCoordinate2D theCoordinate; 
     theCoordinate.latitude = realLatitude; 
     theCoordinate.longitude = realLongitude; 
     myAnnotation.coordinate = theCoordinate; 
     myAnnotation.title = [[anns objectAtIndex:i] objectForKey:@"title"]; 
     myAnnotation.subtitle = [[anns objectAtIndex:i] objectForKey:@"subtitle"]; 
     [mapView addAnnotation:myAnnotation]; 
     [annotations addObject:myAnnotation]; 
     [myAnnotation release]; 
    } 

РЕДАКТИРОВАТЬ:

Я добавил метод viewForAnnotation который задает цвет, но как можно установить цвет каждого пальца в отдельности?

-(MKAnnotationView *)mapView:(MKMapView *)mV viewForAnnotation:(id <MKAnnotation>)annotation { 
    MKPinAnnotationView *pinView = nil; 

    if(annotation != mapView.userLocation) { 
     static NSString *defaultPinID = @"com.invasivecode.pin"; 
     pinView = (MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:defaultPinID]; 
     if (pinView == nil) pinView = [[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:defaultPinID] autorelease]; 

     pinView.pinColor = MKPinAnnotationColorPurple; 
     pinView.canShowCallout = YES; 
     pinView.animatesDrop = YES; 
    } 
    else { 
     [mapView.userLocation setTitle:@"I am here"]; 
    } 

    return pinView; 
} 
+0

Я добавил метод viewForAnnotation который задает цвет, но как я могу установить цвет каждого пальца в отдельности? – simonbs

ответ

0

Решенный! Это может быть сделано, как это в методе viewForAnnotation:

for(MyAnnotation* a in mapView.annotations) { 
      if(annotation == a && annotation != mapView.userLocation) { 
       pinView.image = [UIImage imageNamed:a.annImage]; 
      } 
     } 
Смежные вопросы