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

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
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>

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
于 2013-07-19T21:12:07.090 に答える
0

個人的には、コアデータを使用して注釈を保存します。ただし、ディスクへの書き込みに行き詰まっている場合は、注釈オブジェクトに 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 に答える