chandru
chandru

Reputation: 417

How to get the values by clicking specific annotations in iOS?

I am having a map view and 2 text boxes in my app. Both the text boxes display the latitude and longitude.
I have plotted or mapped few annotations in my map view, and when each annotation is clicked, the callout is displayed with the corresponding latitude and longitude value.
I just want to display those latitude and longitude value in my text boxes. I tried the following code,

latitude_value.text=myAnnotation.title;
longitude_value.text=myAnnotation.subtitle;

latitude_value and longitude_value are the text boxes
myAnnotation is the annotation name
title and subtitle contains latitude and longitude values.
Thanks in advance...!

Upvotes: 1

Views: 2250

Answers (2)

Vishwa Patel
Vishwa Patel

Reputation: 484

You can give a tag for myAnnotation custom object, if it's a custom give property like

Suppose myAnnotation has below properties,

@property (nonatomic, copy) NSString *title;
@property (nonatomic, copy) NSString *subTitle;
@property (readwrite) int tag; // Tag to identify annotation

After this when you are adding annotations, suppose you have array aLocations of objects so give a tag like,

for(int i=0; i< [aLocations count]; i++)
{
   // Create MyAnnotation object and initialize it, suppose created object  is myAnnotation, now set it's tag
   myAnnotation.tag=i;
}

After that you can access tag value in,

-(void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view
{
   MyAnnotation *temp = (MyAnnotation*) view.annotation;
   NSLog(@"%d",temp.tag);
   // and now use 
   [aLocations objectAtIndex:temp.tag]
}

Upvotes: 2

Joiningss
Joiningss

Reputation: 1320

you can set latitude_value and longitude_value by this code:

float latitude = [myAnnotation coordinate].latitude;
float longitude = [myAnnotation coordinate].longitude;
latitude_value.text=[NSString stringWithFormat:@"%f",latitude];
longitude_value.text=[NSString stringWithFormat:@"%f",longitude];

Because CLLocationCoordinate2D is a struct, so it has no concept of methods or of getters. The C dot syntax for accessing struct members is the only correct method.

Upvotes: 1

Related Questions