问题
i know that similar questions have been already asked, but i just could find the answer. So, I'm building an app that has over 100 Pins, writing it into the implementation would be just to much. Does anybody know from your previous experience how can i use the property list file to store coordinates and then read them and place Pins on my map ? What is the best way to do this because i did work with plist files(except editing them).
Any tutorials ?
回答1:
This is from one of my apps:
This is in my MapViewController class:
// Add annotations
NSArray *bikePath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *bikeDocumentsDirectory = [bikePath objectAtIndex:0];
NSString *path = [bikeDocumentsDirectory stringByAppendingPathComponent:@"bikes.plist"];
NSDictionary* bikesDictionary = [[NSDictionary alloc] initWithContentsOfFile:path];
GetRacks *racks = [[GetRacks alloc] initWithDictionary:bikesDictionary];
for(RackAnnotation *rackAnnotation in [racks getRacks])
{
[mapView addAnnotation:rackAnnotation];
}
Methods in GetRacks class:
-(NSArray*)_parseXmlDictionary:(NSDictionary *)aDictionary
{
if (aDictionary != NULL && [aDictionary count] > 0) {
NSArray *locations = [aDictionary objectForKey:@"locations"];
if (locations != NULL)
{
NSMutableArray *resultArray = [[NSMutableArray alloc] init];
for (NSDictionary *currentResult in locations)
{
RackAnnotation *annotation = [[RackAnnotation alloc] init];
annotation._address = [currentResult objectForKey:@"address"];
annotation._id = [currentResult objectForKey:@"id"];
annotation._information = [currentResult objectForKey:@"id"];
annotation._stationType = [currentResult objectForKey:@"stationType"];
annotation._readyBikes = [currentResult objectForKey:@"ready_bikes"];
annotation._emptyLocks = [currentResult objectForKey:@"empty_locks"];
annotation._lastUpdated = [currentResult objectForKey:@"last_update"];
NSNumber *online = [currentResult objectForKey:@"online"];
annotation._online = [online boolValue];
CLLocationDegrees latitude = [[[currentResult objectForKey:@"coordinate"] objectForKey:@"latitude"] floatValue];
CLLocationDegrees longitude = [[[currentResult objectForKey:@"coordinate"] objectForKey:@"longitude"] floatValue];
CLLocationCoordinate2D location;
location.latitude = latitude;
location.longitude = longitude;
annotation._coordinate = location;
[resultArray addObject:annotation];
}
return resultArray;
}
else {
return NULL;
}
}
else {
return NULL;
}
}
The rack annotations: RackAnnotation.h
#import <Foundation/Foundation.h>
#import <MapKit/MapKit.h>
@interface RackAnnotation : NSObject <MKAnnotation> {
NSString *_sub;
NSString *_id;
NSString *_address;
NSString *_information;
NSNumber *_stationType;
NSString *_readyBikes;
NSString *_emptyLocks;
NSString *_lastUpdated;
CLLocationCoordinate2D _coordinate;
BOOL _online;
CLLocationDistance _distance;
}
@property (nonatomic, strong) NSString *_sub;
@property (nonatomic, strong) NSString *_id;
@property (nonatomic, strong) NSString *_address;
@property (nonatomic, strong) NSString *_information;
@property (nonatomic, strong) NSNumber *_stationType;
@property (nonatomic, strong) NSString *_readyBikes;
@property (nonatomic, strong) NSString *_emptyLocks;
@property (nonatomic, strong) NSString *_lastUpdated;
@property (nonatomic, assign) CLLocationCoordinate2D _coordinate;
@property (nonatomic, assign) BOOL _online;
@property (nonatomic, assign) CLLocationDistance _distance;
- (NSNumber*)stationType;
- (NSString*)getInformation;
- (void)setSubTitle:(NSString*)newTitle;
- (NSString*)title;
- (NSString*)subtitle;
- (CLLocationCoordinate2D)coordinate;
@end
RackAnnotation.m:
#import "RackAnnotation.h"
@implementation RackAnnotation
@synthesize _sub, _id, _address, _information, _stationType, _readyBikes, _emptyLocks, _lastUpdated, _coordinate, _online, _distance;
#pragma mark Annotation functions
- (CLLocationCoordinate2D)coordinate
{
return _coordinate;
}
- (NSString*)title
{
return _address;
}
- (NSString*)subtitle
{
return _sub;
}
#pragma mark -
@end
The plist looks like this:
<?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>version</key>
<string></string>
<key>locations</key>
<array>
<dict>
<key>id</key>
<string>1</string>
<key>address</key>
<string>Djurgården Allmänna gränd</string>
<key>information</key>
<string></string>
<key>stationType</key>
<real>1</real>
<key>ready_bikes</key>
<string>0</string>
<key>empty_locks</key>
<string>0</string>
<key>last_update</key>
<string>2012-05-03 16:50:35</string>
<key>coordinate</key>
<dict>
<key>latitude</key>
<real>59.3242468</real>
<key>longitude</key>
<real>18.0948995</real>
</dict>
<key>online</key>
<real>0</real>
</dict>
</array>
</dict>
</plist>
回答2:
You could also store your annotations in dictionaries, put them all in an array and then save it in NSUserDefaults.
Like this:
NSString *latitude = [NSString stringWithFormat:@"%.06f", latitude];//latitude is a double here
NSString *longitude = [NSString stringWithFormat:@"%.06f", longitude];//longitude is a double here
NSString *title = [NSString stringWithFormat:@"%@", title];
NSDictionary *annotation = [[NSDictionary alloc] initWithObjects:[NSArray arrayWithObjects:latitude, longitude, title, nil] forKeys:[NSArray arrayWithObjects:@"latitude", @"longitude", @"title", nil]];
NSMutableArray *annotations = [[pref objectForKey:@"annotations"] mutableCopy];
[annotations addObject:annotation];
NSUserDefaults pref = [[NSUserDefaults alloc] init];
[pref setObject:annotations forKey:@"annotations"];
Then to read the annotations you would go like this:
NSMutableArray *annotations = [[pref objectForKey:@"annotations"] mutableCopy];
for (NSDictionary *annotation in annotations)
{
latitude = [annotation objectForKey:@"latitude"]];
longitude = [annotation objectForKey:@"longitude"]];
title = [annotation objectForKey:@"title"]];
//create an MKAnnotation and add to your maview
}
来源:https://stackoverflow.com/questions/10437578/xcode-annotation-from-property-list