1

So, I have a problem with making positions for annotation pins on MKMapView.

I just write a class heritable from NSObject, called "Annotation", which uses protocol.

so, here's code:

Annotation.h

#import <Foundation/Foundation.h>

@interface Annotation : NSObject <MKAnnotation>
{
    CLLocationCoordinate2D _coordinate;
    NSUInteger _pinID;
}

@property (nonatomic, readonly) CLLocationCoordinate2D coordinate;

- (id)initWithCoordinate:(CLLocationCoordinate2D)coordinate andID:(NSUInteger)pinID;

@end

Annotation.m

#import "Annotation.h"

@implementation Annotation

@synthesize coordinate=_coordinate;

- (id)initWithCoordinate:(CLLocationCoordinate2D)coordinate andID:(NSUInteger)pinID{
    self = [super init];
    if (self != nil) {
        _coordinate = coordinate;
        _pinID = pinID;
    }
    return self;
}

- (NSString *)title {
    return [NSString stringWithFormat:@"Pin %u", _pinID];
}

- (NSString *)subtitle {
    return [NSString stringWithFormat:@"%.4f, %.4f", _coordinate.latitude, _coordinate.longitude];
}

@end

Then, I tried to connect it to my map with foloowing code

MapViewController.m

- (void)viewDidLoad
{
    [super viewDidLoad];
    CLLocationCoordinate2D coordinate;
    coordinate.latitude = 49.2802;
    coordinate.longitude = -123.1182;
    mapView.delegate = self;
    mapView.region = MKCoordinateRegionMakeWithDistance(coordinate, 2000, 2000); 


    CLLocationCoordinate2D newCoord = {2000, 2000};
    Annotation* annotation = [[Annotation alloc] initWithCoordinate:newCoord andID:1];
    [mapView addAnnotation:annotation];
}

But this don't make any sence, pins not even shows. What am I doing wrong? I looked to help and lessons, but no working results.

Karoly S
  • 3,180
  • 4
  • 35
  • 55
user1496491
  • 453
  • 2
  • 11
  • 23

2 Answers2

0

The coordinates of the annotation are invalid:

CLLocationCoordinate2D newCoord = {2000, 2000};
Annotation* annotation = [[Annotation alloc] initWithCoordinate:newCoord ...

A latitude,longitude of 2000, 2000 is not valid.


You probably want to use the coordinate variable instead (which you defined a few lines up in viewDidLoad):

Annotation* annotation = [[Annotation alloc] initWithCoordinate:coordinate andID:1];
  • Annotations still not working... I think maybe some options needed to be activated, because I have no error messages or something – user1496491 Jul 03 '12 at 07:29
  • Is the mapView an IBOutlet (is it in the xib)? If it is an IBOutlet, make sure the map view control in the xib is connected to its outlet in File's Owner. If it's not added in the xib and you intend to create it programmatically, then in viewDidLoad you'll need to first alloc/init it, set its frame, and add it as a subview to the view controller's view. –  Jul 03 '12 at 13:54
0

Try this

- (void)viewDidLoad
{
    [super viewDidLoad];
    CLLocationCoordinate2D coordinate;
    coordinate.latitude = 49.2802;
    coordinate.longitude = -123.1182;
    mapView.delegate = self;
    mapView.region = MKCoordinateRegionMakeWithDistance(coordinate, 2000, 2000); 

    Annotation* annotation = [[Annotation alloc] initWithCoordinate:coordinate andID:1];
    [mapView addAnnotation:annotation];
}
Lithu T.V
  • 19,955
  • 12
  • 56
  • 101