I am parsing data from a csv file with coordinates and other information. I have the pins plotted and I would like to know how to add other pieces of data in the subtitle object of the pins. Here is my approach (the object I'm trying to add is "temperature"):
ViewController.h
NSString * latitude = [infos objectAtIndex:1];
NSString * longitude = [infos objectAtIndex:2];
NSString * description =[infos objectAtIndex:3];
NSString * address = [infos objectAtIndex:4];
NSString * temperature = [infos objectAtIndex:5];
CLLocationCoordinate2D coordinate;
coordinate.latitude = latitude.doubleValue;
coordinate.longitude = longitude.doubleValue;
Location *annotation = [[Location alloc] initWithName:description address:address temperature:temperature coordinate:coordinate] ;
[mapview addAnnotation:annotation];
Location.h
@interface Location : NSObject {
NSString *_name;
NSString *_address;
NSString *_temperature;
CLLocationCoordinate2D _coordinate;
}
@property (copy) NSString *name;
@property (copy) NSString *address;
@property (copy) NSString *temperature;
@property (nonatomic, readonly) CLLocationCoordinate2D coordinate;
- (id)initWithName:(NSString*)name address:(NSString*)address temperature:
(NSString*)temperature coordinate:(CLLocationCoordinate2D)coordinate;
Location.m
@implementation Location
@synthesize name = _name;
@synthesize address = _address;
@synthesize coordinate = _coordinate;
@synthesize temperature = _temperature;
- (id)initWithName:(NSString*)name address:(NSString*)address temperature:(NSString*)temperature coordinate:(CLLocationCoordinate2D)coordinate {
if ((self = [super init])) {
_name = [name copy];
_address = [address copy];
_temperature = [temperature copy];
_coordinate = coordinate;
}
return self;
}
- (NSString *)title {
if ([_name isKindOfClass:[NSNull class]])
return @"Unknown charge";
else
return _name;
}
- (NSString *)subtitle {
return _address;
return _temperature;
}
As you can see I tried to add the "temperature" object in the "subtitle" method, unfortunately that didn't display in the annotation. Right now I am trying with the temperature object but I need to plot some more so I need a way to add as many objects as possible.
In the subtitle
method:
- (NSString *)subtitle {
return _address;
return _temperature;
}
nothing after the return _address;
line will execute.
That's how a return
statement works (execution returns immediately to the caller).
Don't confuse the arrangement of the code with how the variables mentioned are displayed in the user interface.
Additionally, the built-in callout in MapKit only supports one short line each for the title
and subtitle
. Trying to display more than one line in each property will lead to disappointment.
So you could do:
- (NSString *)subtitle {
return _address;
}
or:
- (NSString *)subtitle {
return _temperature;
}
or:
- (NSString *)subtitle {
//display "address, temperature"...
return [NSString stringWithFormat:@"%@, %@", _address, _temperature];
}
If you must display a callout with a lot more detail than the default, you'll need to code a custom callout view which can get complex. See Custom MKAnnotation callout bubble with button and Show custom and default callout views on different pins with pin clustering on map view for a couple of examples to start with if interested.
Location
class should declare that it implements the MKAnnotation
protocol. This will help you and the compiler avoid or display appropriate warnings and self-documents the code as a side benefit:
@interface Location : NSObject<MKAnnotation> {