2016-04-19 10 views
0

私が働いている問題がある:私は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で、私はポイントを指定し、その場所にピンを追加し、それは私が欲しいものではありませんすることができます。私はちょうどオブジェクトをタップし、その名前をポップアップさせたいと思っています。

このような質問は見つかりませんでした。質問が重複している場合はお知らせください。

ありがとうございます。

答えて

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; 
} 
関連する問題