0

I am working on display a map with annotation on it. What I have so far is

Annotation.h

#import <MapKit/MKAnnotation.h>
#import <Foundation/Foundation.h>

@interface Annotation : NSObject <MKAnnotation> 

@end

MapViewController.m

Annotation *pin = [[Annotation alloc] init];    
[pin title]       = storeName;   
[pin subtitle]    = storeAddress;   
[pin coordinate]  = region.center;       
[mapView addAnnotation:pin];

However, I got an error like below:

expression is not assignable for title, subtitle and coordinate

Does anyone have any idea about this issue?

2 Answers 2

2

First, these lines try to assign a value to a method call which is what the error is saying you can't do:

[pin title]       = storeName;   
[pin subtitle]    = storeAddress;   
[pin coordinate]  = region.center;       

They should be like this:

pin.title       = storeName;   
pin.subtitle    = storeAddress;   
pin.coordinate  = region.center;       


However, the MKAnnotation protocol defines the properties as readonly. To be able to set them, declare them in your Annotation class as:

@property (nonatomic, assign) CLLocationCoordinate2D coordinate;
@property (nonatomic, copy) NSString *title;
@property (nonatomic, copy) NSString *subtitle;

and add the @synthesize lines for them in Annotation.m.


However, if all you need are the title, subtitle, and coordinate properties, you don't need to create your own class to implement MKAnnotation. Instead, just use the built-in MKPointAnnotation class which already implements those properties as settable:

MKPointAnnotation *pin = [[MKPointAnnotation alloc] init];


Another option, as @macbirdie points out, is just to make your existing Store class (if you have one) implement the MKAnnotation protocol.

Sign up to request clarification or add additional context in comments.

1 Comment

I go for MKPointAnnotation after all. Thanks for your advice
1

Just read the documentation on MKAnnotation protocol. You're not supposed to assign the title, subtitle and coordinate. You have to provide an implementation of these methods in your class conforming to this protocol.

So it's better if you create a StoreAnnotation class that receives storeName, storeAddress and storeCoordinates, or simply a Store class if you have any, and it will return appropriate data in the protocol methods.

4 Comments

but i think these variables are attribute not method, rite ?... so how can we implement them
They are methods as you can see in protocol's reference. And you implement them just like any other objective-c instance method.
i went to MKAnnotation protocol and click on Jump To, there are Properties and Instance Methods. In Properties, they are title, subtitle and coordinate...
Oh, yeah, sorry. They're read only though. That was the general idea. There's only setCoordinate method in addition to those properties. You have to implement this one as well, anyway.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.