检查在mkmapview上点击哪个地图视图注释

时间:2012-12-18 15:12:42

标签: objective-c ios mkmapview

我在知道在MKMapView上点击了哪个注释时遇到了问题。

让我解释一下我的问题,有一个简单的视图控制器,在其上加载了地图视图。

我的注释类“MapViewAnnotation.h”如下

@interface MapViewAnnotation : NSObject <MKAnnotation>
{
    NSString *title;
    CLLocationCoordinate2D coordinate;
    NSString *sID;
    NSString *zipCode;
}
@property (nonatomic, copy) NSString *title;
@property (nonatomic, assign) CLLocationCoordinate2D coordinate;
@property (nonatomic, retain) NSString *sID;
@property (nonatomic, retain) NSString *zipCode;

- (id)initWithTitle:(NSString *)titleOfPin andStoreId:(NSString *)storeIdForDetails andCoordinate:(CLLocationCoordinate2D)coordinateOfPin andZipCode:(NSString *)zip;

这是我的“MapViewAnnotation.m”文件。

#import "MapViewAnnotation.h"
#import <MapKit/MapKit.h>

@implementation MapViewAnnotation

@synthesize title, coordinate,storeId,zipCode;

- (id)initWithTitle:(NSString *)titleOfPin andStoreId:(NSString *)storeIdForDetails andCoordinate:(CLLocationCoordinate2D)coordinateOfPin andZipCode:(NSString *)zip
{
    [super init];
    title = titleOfPin;
    coordinate = coordinateOfPin;
    sID = storeIdForDetails;
    zipCode = zip;
    return self;
}


-(void)dealloc
{
    [title release];
    [super dealloc];
}
@end

这是我的viewcontroller.m文件

- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control
{
    //I want to access the sID property of that annotation here. PLEASE HELP ME HOW CAN I DO THAT

    if (!storeDetailControllerObject) {
        storeDetailControllerObject = [[StoreDetailController alloc]init];
    }
    //    storeDetailControllerObject.storeId = [view.annotation storeId];
    [self.navigationController pushViewController:storeDetailControllerObject animated:YES];

2 个答案:

答案 0 :(得分:1)

来自MKMapViewDelegate protocol reference

- (void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view;

如果您在此方法中处理事件,您可能知道选择了哪个注释 您必须设置MKMapView的委托并声明您的类实现了MKMapViewDelegate。

答案 1 :(得分:0)

这应该有效:

MapViewAnnotation *mapViewAnnotation = (MapViewAnnotation*)view.annotation;
if( [mapViewAnnotation isKindOfClass:[MapViewAnnotation class]] ){
    storeDetailControllerObject.storeId = mapViewAnnotation.storeId;
}