Map Pin title problem

I have a frustrating issue with Map Pin annotations.

If i create a single view application with the following code it works fine. No issues.

I have encountered a problem when i attempt to use it in as a scene in my application.


The basis of the application is adding multiple map annotations from a property list.

I create an NSMutableArray from the property list, these are then added through an NSObject and the map is updated with pins and titles.


The Issue.

The mappins are displayed correctly in their correct positions " If i do not add a title".

When I add the title the app crashes.


The view .h

#import <UIKit/UIKit.h>

#import <MapKit/MapKit.h>

@interface Citys : UIViewController

@property (nonatomic,strong) IBOutlet MKMapView *mapview;

@end


The View .m

#import "Citys.h"

#import "MapViewAnnotation.h"

@interface Citys ()

@end

@implementation Citys

@synthesize mapview;


- (NSMutableArray *)createAnnotations

{

NSMutableArray *annotations = [[NSMutableArray alloc] init];

/

NSString *path = [[NSBundle mainBundle] pathForResource:@"Locations" ofType:@"plist"];

NSArray *locations = [NSArray arrayWithContentsOfFile:path];

for (NSDictionary *row in locations) {

NSString *title = [row objectForKey:@"title"];

NSNumber *latitude = [row objectForKey:@"latitude"];

NSNumber *longitude = [row objectForKey:@"longitude"];


/

CLLocationCoordinate2D coord;

coord.latitude = latitude.doubleValue;

coord.longitude = longitude.doubleValue;

MapViewAnnotation *annotation = [[MapViewAnnotation alloc] initWithTitle:title AndCoordinate:coord];

[annotations addObject:annotation];

}

return annotations;

}

- (void)viewDidLoad

{

[super viewDidLoad];

/


[mapview setMapType:MKMapTypeStandard];

[mapview setZoomEnabled:YES];

[mapview setScrollEnabled:YES];


MKCoordinateRegion region = { {0.0,0.0},{0.0,0.0} };


region.center.latitude = 32.760645;

region.center.longitude = -86.635437;

region.span.latitudeDelta = 7.0f;

region.span.longitudeDelta = 7.0f;;

[mapview setRegion:region animated:YES];


[self.mapview addAnnotations:[self createAnnotations]];

}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation

{

return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);

}

- (void)didReceiveMemoryWarning {

[super didReceiveMemoryWarning];

/

}

@end




The annotation NSOject .h

#import <Foundation/Foundation.h>

#import <MapKit/MapKit.h>

@interface MapViewAnnotation : NSObject <MKAnnotation>

@property (nonatomic,copy) NSString *title;

@property (nonatomic, readonly) CLLocationCoordinate2D coordinate;

-(id) initWithTitle:(NSString *) title AndCoordinate:(CLLocationCoordinate2D)coordinate;

@end



The annotation NSObject .m

#import "MapViewAnnotation.h"

@implementation MapViewAnnotation

@synthesize title = _title;

@synthesize coordinate =_coordinate;

-(id) initWithTitle:(NSString *) title AndCoordinate:(CLLocationCoordinate2D)coordinate;

{

self = [super init];

_title = title;

_coordinate = coordinate;

return self;

}

@end


The Property List Structure

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE plist PUBLIC "-/

<plist version="1.0">

<array>

<dict>

<key>title</key>

<string>Auburn</string>

<key>latitude</key>

<real>32.609857</real>

<key>longitude</key>

<real>-85.480782</real>

</dict>

<dict>

<key>title</key>

<string>Birmingham</string>

<key>latitude</key>

<real>33.520661</real>

<key>longitude</key>

<real>-86.80249</real>

</dict>

</array>

</plist>


The Error

#import <UIKit/UIKit.h>

#import "AppDelegate.h"

int main(int argc, char *argv[])

{

@autoreleasepool {

return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));

}

}

If i run this code as a single view application, this does not happen.

Please Help?

ossieleigh

That's not the error. The error message will be printed in the console log (maybe something like "unrecognized selector").


Set a breakpoint and step through your code in the debugger. Check that everything has the values you expect. (title string is non-nil, in particular.)


You can also set an exception breakpoint in Xcode and it will stop where the exception is thrown, rather than where it is caught (which is what you posted here).

Hi there,

After the information you gave me I checked things out a bit further.


It appears to be when the Map is loaded.


I am getting differing error messages. Please see below.



2015-07-06 18:00:00.078 USAShoppingMalls[4481:60b] -[__NSArrayM length]: unrecognized selector sent to instance 0x200a2910

(lldb)


2015-07-06 18:01:57.798 USAShoppingMalls[4490:60b] -[VGLResourceImpl length]: unrecognized selector sent to instance 0x1d1a9e90

(lldb)

Everything loads into the array correctly but it crashes when the mapview is displayed.

I corrected a code error of 2 off ;; after setting the "region.span.longitudeDelta = 7.0f;;".

I also get different results depending on whether I load the Mapview and then the Annotations or the Other way around.

I am not fully aware of what should be loaded first or if it should be loaded in ViewDidLoad or [super viewDidLoad];

Thanks Ossie

Post your code for viewForAnnotation:.


You're probably not checking that the annotation being passed is your own class.

Sorry,

This is the total code that runs as a stand alone project.


I am not experienced enough to know what is required when you say "post your code for viewForAnnotation".


Cheers ossie

Map Pin title problem
 
 
Q