点击MkMapKit上的Annotation,返回名称

时间:2016-04-19 15:59:59

标签: ios objective-c mkmapview

我正在处理的问题是:

我有一个MKMapKit,只要用户点击建筑物,街道,就会从mapView中弹出名称,如下所示:

enter image description here

我有自己的类AddressAnnotation,如下所示:

AddressAnnotation.h

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

@interface AddressAnnotation : NSObject <MKAnnotation>

- (id)initWithName:(NSString *)name address:(NSString *)address coordinate:(CLLocationCoordinate2D)coordinate;

@end

AddressAnnotation.m

#import "AddressAnnotation.h"
#import <AddressBook/AddressBook.h>

@interface AddressAnnotation()

@property (nonatomic, copy) NSString *name;
@property (nonatomic, copy) NSString *address;
@property (nonatomic, assign) CLLocationCoordinate2D theCoordinate;

@end

@implementation AddressAnnotation

- (id)initWithName:(NSString*)name address:(NSString*)address coordinate:(CLLocationCoordinate2D)coordinate {
    if ((self = [super init])) {
        if ([name isKindOfClass:[NSString class]]) {
            self.name = name;
        } else {
            self.name = @"";
        }
        self.address = address;
        self.theCoordinate = coordinate;
    }
    return self;
}

- (NSString *)title {
    return _name;
}

- (NSString *)subtitle {
    return _address;
}

- (CLLocationCoordinate2D)coordinate {
    return _theCoordinate;
}

在我的主MapViewController中,我可以指定一个点并在该位置添加一个引脚,但这不是我想要的。我只是希望能够点击一个对象并弹出他们的名字。

我找不到类似的问题;如果我复制了一个问题,请通知我。

谢谢。

1 个答案:

答案 0 :(得分:0)

如果您希望在点按时将气球弹出到注释上方。您可以在控制器中使用MKMapViewDelegate。

-(MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation{
    // do this so you dont run the code for any other annotation type (eg blue dot for where your location is)
    if([annotation isKindOfClass:[AddressAnnotation class]]){

        MKPinAnnotationView* pv = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"spot"];
        pv.pinColor = MKPinAnnotationColorPurple;

        // decorate the balloon
        UIImageView* iv = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"something.png"]];
        iv.frame = CGRectMake(0, 0, 30, 30);
        pv.leftCalloutAccessoryView = iv;
        pv.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
        // (default title and subtitle of the balloon will be taken from the annoation object)

        // allow balloon to show when tapping
        pv.canShowCallout = YES;
        return pv;
    }
    return nil;
}