2016-04-19 6 views
0

Das Problem Ich arbeite an ist:Tippen Sie auf Annotation auf MkMapKit, Rückname

Ich habe einen MKMapKit und wenn ein Benutzer auf ein Gebäude tippt, Straße, öffnet sich der Name aus dem mapView oben, etwa so:

enter image description here

ich meine eigene Klasse AddressAnnotation haben, etwa so:

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

Und in meinem Haupt-MapViewController kann ich einen Punkt angeben und einen Stift zu dieser Stelle hinzufügen, aber das ist nicht das, was ich will. Ich möchte nur auf ein Objekt tippen und seinen Namen anzeigen lassen.

Ich konnte eine ähnliche Frage nicht finden; Bitte informieren Sie mich, wenn ich eine Frage dupliziert habe.

Vielen Dank.

Antwort

0

Wenn Sie möchten, dass eine Sprechblase über Ihrer Anmerkung erscheint, wenn Sie darauf tippen. Sie können MKMapViewDelegate in Ihrem Controller verwenden.

-(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; 
} 
Verwandte Themen