我知道已经问过类似的问题了,但我才能找到答案。所以,我正在构建一个拥有超过100个Pins的应用程序,将其写入实现将会很多。有没有人从你以前的经验中知道如何使用属性列表文件来存储坐标然后读取它们并将Pins放在我的地图上?这样做的最佳方法是什么,因为我使用了plist文件(编辑它们除外)。
任何教程?
答案 0 :(得分:1)
这是我的一个应用程序:
这是我的MapViewController类:
// 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];
}
GetRacks类中的方法:
-(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;
}
}
机架注释: 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
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>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>
答案 1 :(得分:0)
您还可以将注释存储在词典中,将它们全部放在一个数组中,然后将其保存在NSUserDefaults中。
像这样:
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"];
然后阅读注释,你会这样:
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
}