Reputation: 135
I was looking at the following discussion of a question to try and understand how to read locations from a plist and drop pins an Map view accordingly:
MapKit based app crashing when loading from plist
I am having problems understanding how to make this work. If I hard code in the annotations then I can make it work fine so I don't understand where I'm going wrong. I think it is probably how I am accessing the data in the .plst. My plist has the structure:
<array>
<dict>
<key>rowData</key>
<array>
<dict>
<key>details</key>
<string>Some minor info</string>
<key>latitude</key>
<strong>53.958756</string>
<key>Location</key>
<string>Location One</string>
<key>lontitude</key>
<string>-1.07937</string>
</dict>
</array>
</dict>
</array>
I try to access this data using the code from the answer to the question above like this But with not joy:
-(id)initWithDictionary:(NSDictionary *)dict{
self = [super init];
if(self!=nil){
coordinate.latitude = [[dict objectForKey:@"latitude"] doubleValue];
coordinate.longitude = [[dict objectForKey:@"longitude"] doubleValue];
self.title = [dict objectForKey:@"Location"];
self.subtitle = [dict objectForKey:@"details"];
}
return self;
My viewDidLoad also looks a bit different to the other question I referenced above. It looks like this:
- (void)gotoLocation
{
// set location as York, UK
MKCoordinateRegion newRegion;
newRegion.center.latitude = 53.960025;
newRegion.center.longitude = -1.082697;
newRegion.span.latitudeDelta = 0.0012872;
newRegion.span.longitudeDelta = 0.0159863;
[self.map setRegion:newRegion animated:YES];
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
//Set up map
self.map.mapType = MKMapTypeStandard; // also MKMapTypeSatellite or MKMapTypeHybrid
[self gotoLocation];
// Get the the plist in application bundle
NSString *path = [[NSBundle mainBundle] pathForResource:@"Places" ofType:@"plist"];
// Retrieve the plists root element array
NSArray *array = [[NSArray alloc] initWithContentsOfFile:path];
NSLog(@"Grabbed locations.plist ok");
if (array) {
NSMutableDictionary* myDict = [NSMutableDictionary dictionaryWithCapacity:[array count]];
for (NSDictionary* dict in array) {
MapAnnotations* annotation = [[MapAnnotations alloc]initWithDictionary:dict];
[self.map addAnnotation:annotation];
[annotation release];
}
NSLog(@"The count: %i", [myDict count]);
}
else {
NSLog(@"Plist does not exist");
}
}
If anyone can explain to me where I am going wrong and how to do what I need to do, I'd appreciate it.
Thanks for reading.
Upvotes: 0
Views: 1256
Reputation:
First, there are a couple of problems with the plist (at least with the example in your question):
<strong>
(under latitude
key) should be <string>
lontitude
should be longitude
Next, your plist structure is an array containing one dictionary with one key "rowData" with a value that is an array of dictionaries. But the code loops through the array
variable which contains only the outermost array (which only contains the single dictionary with the "rowData" key). So in initWithDictionary
, the objectForKey
calls all return nil because the location related keys aren't in the outermost dictionary.
You want to either re-structure the plist so that it is just an array of location dictionaries (eliminate the "rowData" dictionary) or update the code to get the array inside the "rowData" key and loop through that:
NSArray *rowDataArray = [[array objectAtIndex:0] objectForKey:@"rowData"];
for (NSDictionary* dict in rowDataArray) //instead of "in array"
Finally, the myDict
dictionary is not being used really. The NSLog of its count will always display zero because the dictionaryWithCapacity
line just allocates memory but doesn't add any objects. I don't see a need for the myDict
variable--I'd remove it.
Upvotes: 1