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>