2013-07-18 3 views
0

Я хочу адаптировать следующие tutorial, чтобы создать аннотации из plist. Я новичок в Objective C и Xcode и пробовал много разных учебных пособий, но, похоже, не могу их собрать.Несколько аннотаций от plist

Annotation.h

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

@interface Annotation : NSObject <MKAnnotation> 

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

@end 

Annotation.m

#import "Annotation.h" 

@implementation Annotation 
@synthesize coordinate, title, subtitle, leftCalloutAccessoryView; 

@end 

ViewController.h

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

@interface ViewController : UIViewController 

@property (weak, nonatomic) IBOutlet MKMapView *myMapView; 

@end 

ViewController.m

#import "ViewController.h" 
#import "Annotation.h" 

@interface ViewController() 

@end 

//Wimbledon Coordinates 
#define WIMB_LATITUDE 51.434783; 
#define WIMB_LONGITUDE -0.213428; 

//Stadium Coordinates 
#define ARSENAL_LATITUDE 51.556899; 
#define ARSENAL_LONGITUDE -0.106403; 

#define CHELSEA_LATITUDE 51.481314; 
#define CHELSEA_LONGITUDE -0.190129; 

//Span 
#define THE_SPAN 0.10f; 

@implementation ViewController 
@synthesize myMapView; 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 

    // Create the region 
    MKCoordinateRegion myRegion; 

    //Center 
    CLLocationCoordinate2D center; 
    center.latitude = ARSENAL_LATITUDE; 
    center.longitude = ARSENAL_LONGITUDE; 

    //Span 
    MKCoordinateSpan span; 
    span.latitudeDelta = THE_SPAN; 
    span.longitudeDelta = THE_SPAN; 

    myRegion.center = center; 
    myRegion.span=span; 

    //Set our mapView 
    [myMapView setRegion:myRegion animated:YES]; 

    //Annotation 
    NSMutableArray * locations = [[NSMutableArray alloc] init]; 
    CLLocationCoordinate2D location; 
    Annotation * myAnn; 

    //Arsenal Annotation 
    myAnn = [[Annotation alloc] init]; 
    location.latitude = ARSENAL_LATITUDE; 
    location.longitude = ARSENAL_LONGITUDE; 
    myAnn.coordinate = location; 
    myAnn.title = @"Arsenal FC"; 
    myAnn.subtitle = @"The Gunners"; 
    //myAnn.image = [UIImage imageNamed:@"location.png"]; 
    [locations addObject:myAnn]; 

    //Chelsea Annotation 
    myAnn = [[Annotation alloc] init]; 
    location.latitude = CHELSEA_LATITUDE; 
    location.longitude = CHELSEA_LONGITUDE; 
    myAnn.coordinate = location; 
    myAnn.title = @"Chelsea FC"; 
    myAnn.subtitle = @"The Blue Lions"; 
    [locations addObject:myAnn]; 

    [self.myMapView addAnnotations:locations]; 
} 

//THIS CODE WORKS FOR CUSTOM ANNOTATIONS 

- (MKAnnotationView *)mapView:(MKMapView *)map viewForAnnotation:(id <MKAnnotation>)annotation 
{ 
    MKPinAnnotationView *newAnnotation = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"redpin"]; 
    newAnnotation.pinColor = MKPinAnnotationColorRed; 
    UIImageView *IconView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"marker.png"]]; 
    newAnnotation.leftCalloutAccessoryView = IconView; 
    newAnnotation.animatesDrop = YES; 
    newAnnotation.canShowCallout = YES; 
    return newAnnotation; 
} 


- (void)didReceiveMemoryWarning 
{ 
    [super didReceiveMemoryWarning]; 
    // Dispose of any resources that can be recreated. 
} 

@end 
+0

Итак, что же это вопрос? Вы даже попытались загрузить его с plist? – Can

+0

Вместо жесткого кодирования координат, заголовка и субтитров, я хотел бы загрузить их из слоя. И да, я пробовал много разных примеров, которые я нашел при поиске, но я не могу понять, как выйти замуж за концепции. –

+0

Вы пробовали создать plist и прочитать его? См. Http://stackoverflow.com/questions/8206761/trying-to-call-plist-data-in-a-for-loop-ios для одного из способов сделать это. – Anna

ответ

3

Получил его на работу благодаря предложению выше. Также добавляется в пользовательские маркеры и значок слева для вызова. Я прокомментировал старый код координаты и красные значки аннотации с левой иконкой.

Надеюсь, это поможет другим, кто, возможно, сошел с ума, ища решение, какое у меня есть.

Stadiums.plist

<?xml version="1.0" encoding="UTF-8"?> 
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> 
<plist version="1.0"> 
<dict> 
    <key>Stadiums</key> 
    <array> 
     <dict> 
      <key>Title</key> 
      <string>Arsenal</string> 
      <key>Subtitle</key> 
      <string>The Gunners</string> 
      <key>Latitude</key> 
      <string>51.556899</string> 
      <key>Longitude</key> 
      <string>-0.106403</string> 
     </dict> 
     <dict> 
      <key>Title</key> 
      <string>Chelsea</string> 
      <key>Subtitle</key> 
      <string>The Blue Lions</string> 
      <key>Latitude</key> 
      <string>51.481314</string> 
      <key>Longitude</key> 
      <string>-0.190129</string> 
     </dict> 
    </array> 
</dict> 
</plist> 

ViewController.m

#import "ViewController.h" 
#import "Annotation.h" 

@interface ViewController() 

@end 

//Wimbledon Coordinates 
#define WIMB_LATITUDE 51.434783; 
#define WIMB_LONGITUDE -0.213428; 

//Stadium Coordinates 
#define ARSENAL_LATITUDE 51.556899; 
#define ARSENAL_LONGITUDE -0.106403; 

#define CHELSEA_LATITUDE 51.481314; 
#define CHELSEA_LONGITUDE -0.190129; 

//Span 
#define THE_SPAN 0.10f; 

@implementation ViewController 
@synthesize myMapView; 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 

    // Create the region 
    MKCoordinateRegion myRegion; 

    //Center 
    CLLocationCoordinate2D center; 
    center.latitude = ARSENAL_LATITUDE; 
    center.longitude = ARSENAL_LONGITUDE; 

    //Span 
    MKCoordinateSpan span; 
    span.latitudeDelta = THE_SPAN; 
    span.longitudeDelta = THE_SPAN; 

    myRegion.center = center; 
    myRegion.span=span; 

    //Set our mapView 
    [myMapView setRegion:myRegion animated:YES]; 

    /* 
    //Annotation 
    NSMutableArray * locations = [[NSMutableArray alloc] init]; 
    CLLocationCoordinate2D location; 
    Annotation * myAnn; 

    //Arsenal Annotation 
    myAnn = [[Annotation alloc] init]; 
    location.latitude = ARSENAL_LATITUDE; 
    location.longitude = ARSENAL_LONGITUDE; 
    myAnn.coordinate = location; 
    myAnn.title = @"Arsenal FC"; 
    myAnn.subtitle = @"The Gunners"; 
    //myAnn.image = [UIImage imageNamed:@"location.png"]; 
    [locations addObject:myAnn]; 

    //Chelsea Annotation 
    myAnn = [[Annotation alloc] init]; 
    location.latitude = CHELSEA_LATITUDE; 
    location.longitude = CHELSEA_LONGITUDE; 
    myAnn.coordinate = location; 
    myAnn.title = @"Chelsea FC"; 
    myAnn.subtitle = @"The Blue Lions"; 
    [locations addObject:myAnn]; 

    [self.myMapView addAnnotations:locations]; 
    */ 

    NSMutableArray *annotations = [[NSMutableArray alloc]init]; 
    NSString *path = [[NSBundle mainBundle] pathForResource:@"Stadiums" ofType:@"plist"]; 
    NSDictionary *dict = [NSDictionary dictionaryWithContentsOfFile:path]; 
    NSArray *anns = [dict objectForKey:@"Stadiums"]; 
    NSLog(@"read1"); 

    for(int i = 0; i < [anns count]; i++) { 
     NSLog(@"read2"); 
     float realLatitude = [[[anns objectAtIndex:i] objectForKey:@"Latitude"] floatValue]; 
     float realLongitude = [[[anns objectAtIndex:i] objectForKey:@"Longitude"] floatValue]; 
     NSLog(@"read3"); 

     Annotation *myAnnotation = [[Annotation 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"]; 
     [myMapView addAnnotation:myAnnotation]; 
     [annotations addObject:myAnnotation]; 
     //[myAnnotation release]; 
    } 

} 

//THIS CODE WORKS FOR CUSTOM ANNOTATIONS 

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

    MKAnnotationView *annotationView = (MKAnnotationView *)[myMapView dequeueReusableAnnotationViewWithIdentifier:AnnotationViewID]; 

    if (annotationView == nil) 
    { 
     annotationView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:AnnotationViewID]; 
    } 

    //Custom Pin 
    annotationView.image = [UIImage imageNamed:@"toilets.png"]; 

    //Custom Thumbnail (left side) 
    UIImageView *IconView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"toilets.png"]]; 
    annotationView.leftCalloutAccessoryView = IconView; 

    annotationView.canShowCallout = YES; 
    annotationView.annotation = annotation; 

    return annotationView; 

    /* 
    MKPinAnnotationView *newAnnotation = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"redpin"]; 
    newAnnotation.pinColor = MKPinAnnotationColorRed; 
    UIImageView *ThumbView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"toilets.png"]]; 
    newAnnotation.leftCalloutAccessoryView = ThumbView; 
    newAnnotation.animatesDrop = YES; 
    newAnnotation.canShowCallout = YES; 
    return newAnnotation; 
    */ 
} 


- (void)didReceiveMemoryWarning 
{ 
    [super didReceiveMemoryWarning]; 
    // Dispose of any resources that can be recreated. 
} 

@end 
+0

хорошо сделанный парень, помог мне с джем, я был в , –

0

Лично я хотел бы использовать CoreData для хранения аннотаций. Однако, если вы застряли в записи вещей на диск, я бы сделал ваш объект аннотации реализованным протоколом NSCoding и использовал NSKeyedArchiver для сохранения и загрузки ваших объектов.

+ (NSObject *)readArchiveFile:(NSString *)inFileName 
{ 
    NSFileManager *fileMgr = [NSFileManager defaultManager]; 
    NSString *documentsDirectoryPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0]; 
    NSString *filePath = [NSString stringWithFormat:@"%@/%@", documentsDirectoryPath, inFileName]; 

    NSObject *returnObject = nil; 
    if([fileMgr fileExistsAtPath:filePath]) 
    { 
     @try 
     { 
      returnObject = [NSKeyedUnarchiver unarchiveObjectWithFile:filePath]; 
     } 
     @catch (NSException *exception) 
     { 
      [FileUtils deleteFile:inFileName]; 
      returnObject = nil; 
     } 
    } 

    return returnObject; 

} 

+ (void)archiveFile:(NSString *)inFileName inObject:(NSObject *)inObject 
{ 
    NSString *documentsDirectoryPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0]; 
    NSString *filePath = [NSString stringWithFormat:@"%@/%@", documentsDirectoryPath, inFileName]; 
    @try 
    { 
     BOOL didSucceed = [NSKeyedArchiver archiveRootObject:inObject toFile:filePath]; 
     if(!didSucceed) 
     { 
      NSLog(@"File %@ write operation %@", inFileName, didSucceed ? @"success" : @"error"); 
     } 
    } 
    @catch (NSException *exception) 
    { 
     NSLog(@"File %@ write operation threw an exception:%@", filePath, exception.reason); 
    } 

} 
+0

Будучи как я новичок, я не уверен, что делать с этим. Есть ли хорошая ссылка для CoreData и что я хотел бы сделать? Я хочу прокомментировать аннотации для координат, заголовка и субтитров и вместо этого загрузить это из plist или аналогичного (я читал, что CoreData использует SQLite?) Спасибо заранее. –

+0

Вот учебник по CoreData: http://www.raywenderlich.com/934/core-data-on-ios-5-tutorial-getting-started – JonahGabriel

Смежные вопросы