I’m trying to add pin/address annotations in my Google maps view. When I do this everyting works fine:
[self addPin: CLLocationCoordinate2DMake(lat, lng) title: @"test" subtitle: @"test"];
with:
- (void) addPin: (CLLocationCoordinate2D) position title: (NSString *) pinTitle subtitle: (NSString *) pinSubtitle{
AddressAnnotation *addAnnotation = [[AddressAnnotation alloc] initWithTitle:pinTitle andCoordinate:position andSubtitle:pinSubtitle];
[mapView addAnnotation:addAnnotation];
[addAnnotation autorelease];
}
and
#import "AddressAnnotation.h"
@implementation AddressAnnotation
@synthesize title, coordinate, subtitle;
- (id)initWithTitle:(NSString *)ttl andCoordinate:(CLLocationCoordinate2D)c2d andSubtitle:(NSString *)sbtitle {
[super init];
title = ttl;
coordinate = c2d;
subtitle = sbtitle;
return self;
}
- (void)dealloc {
[title release];
[super dealloc];
}
@end
with:
#import <Foundation/Foundation.h>
#import <MapKit/MapKit.h>
@interface AddressAnnotation : NSObject <MKAnnotation> {
NSString *title;
CLLocationCoordinate2D coordinate;
NSString *subtitle;
}
@property (nonatomic, copy) NSString *title;
@property (nonatomic, copy) NSString *subtitle;
@property (nonatomic, readonly) CLLocationCoordinate2D coordinate;
- (id)initWithTitle:(NSString *)ttl andCoordinate:(CLLocationCoordinate2D)c2d andSubtitle:(NSString *)sbtitle;
@end
things go wrong when I do this:
NSString *detailTitle = [NSString stringWithFormat:@"%@", [key objectForKey:@"name"]];
NSString *detailSubtitle = [NSString stringWithFormat:@"%@", [key objectForKey:@"vicinity"]];
NSLog(@"%@", detailSubtitle);
[self addPin: CLLocationCoordinate2DMake(lat, lng) title: detailTitle subtitle: detailSubtitle];
The app chrases somethimes when the pins are supposed to go in my view and somethimes this error:
‘NSInvalidArgumentException’, reason: ‘-[NSCFNumber length]: unrecognized selector sent to instance 0x5851c80’
Try changing your
initfunction fromAddressAnnotation.mtoYou are using the instance variables directly to set the values in the class instance and not the properties so the actual values that you pass are not copied hence not retained.