0

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 ?

dsafa
  • 783
  • 2
  • 8
  • 29

2 Answers2

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>
Paul Peelen
  • 10,073
  • 15
  • 85
  • 168
  • thanks for the answer. I'm gonna try it. But can you tell me, is it possible to use property list with coordinates by adding rows(strings) or it must be XML ? – dsafa May 03 '12 at 19:18
  • Sorry... I added the wrong plist data... I updated my answer now. It is using a plist file (which acutally is XML) – Paul Peelen May 03 '12 at 19:22
  • just one question. so in my project i have several cites on my map, and they all have several poi's . Should i set the cities as dictionary and coordinates in them as strings ? – dsafa May 03 '12 at 19:46
  • thats one way, just make sure you set it as floats (key),.. not as strings. – Paul Peelen May 03 '12 at 19:51
0

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
 }
Rafael Moreira
  • 1,759
  • 2
  • 19
  • 20