0

我正在调整以下教程以从 plist 创建注释。我是 Objective C 和 Xcode 的新手,尝试了许多不同的教程,但似乎无法将这些结合在一起。

注释.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

注释.m

#import "Annotation.h"

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

@end

视图控制器.h

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

@interface ViewController : UIViewController

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

@end

视图控制器.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
4

2 回答 2

3

由于上面的建议,它可以工作。还添加了自定义标记和一个左图标来调用。我用左图标注释掉了旧的坐标代码和注释红色引脚。

希望这可以帮助其他可能像我一样疯狂寻找解决方案的人。

体育场.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>

视图控制器.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
于 2013-07-19T21:12:07.090 回答
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);
    }

}
于 2013-07-18T19:55:52.580 回答