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

纵然是瞬间 提交于 2019-12-11 11:16:41

问题


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...!


回答1:


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.




回答2:


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]
}


来源:https://stackoverflow.com/questions/20113551/how-to-get-the-values-by-clicking-specific-annotations-in-ios

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!