2012-11-16 27 views
2

我試圖在mapview加載後顯示標註標註。我確定我錯過了一件簡單的事情,但找不到它。如何在視圖上顯示mapview標註加載

感謝您的任何幫助。

這裏是我的代碼:

我的.h:

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

@interface MapDetailViewController : UIViewController <MKMapViewDelegate> { 

    MKMapView *mapView; 

} 


// map 
@property (strong, nonatomic) IBOutlet MKMapView *mapView; 

@property (nonatomic) double latDouble; 
@property (nonatomic) double lngDouble; 

@property (nonatomic) NSString *vendorStr; 

@property (nonatomic) NSNumber *costAmount; 


@end 

我的.m:

#進口 「MapDetailViewController.h」

@interface MapDetailViewController() 

@end 

#define THESPAN 0.01f; 

@implementation MapDetailViewController 
@synthesize mapView; 
@synthesize latDouble, lngDouble; 
@synthesize vendorStr, costAmount; 

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil 
{ 
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; 
    if (self) { 
     // Custom initialization 
    } 
    return self; 
} 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 

} 

-(void)viewWillAppear:(BOOL)animated { 

    [super viewWillAppear:YES]; 

    // mapView.showsUserLocation = YES; 
    mapView.delegate = self; 

    MKCoordinateRegion region; 

    MKCoordinateSpan span; 
    span.longitudeDelta = THESPAN; 
    span.latitudeDelta = THESPAN; 

    CLLocationCoordinate2D annotationCoord; 
    annotationCoord.latitude = latDouble; 
    annotationCoord.longitude = lngDouble; 


    MKPointAnnotation *annotationPoint = [[MKPointAnnotation alloc] init]; 
    annotationPoint.coordinate = annotationCoord; 
    annotationPoint.title = vendorStr; 

    //convert cost amount/ price to string value for the subtitle 
    NSString *costAmountStr = [costAmount stringValue]; 


    annotationPoint.subtitle = costAmountStr; 
    [mapView addAnnotation:annotationPoint]; 


    region.center = annotationCoord; 
    region.span = span; 

    [mapView setRegion:region animated:YES]; 


} 

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation 
{ 
    MKPinAnnotationView *newAnnotation=[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"annotation1"]; 

    UIButton *disclosureButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure]; 
    [disclosureButton addTarget:self action:@selector(mapCallOutPressed:) forControlEvents:UIControlEventTouchUpInside]; 

    newAnnotation.rightCalloutAccessoryView = disclosureButton; 
    newAnnotation.animatesDrop = YES;  
    newAnnotation.canShowCallout = YES; 
    newAnnotation.annotation = annotation; 
    newAnnotation.draggable = YES; 
    newAnnotation.enabled = YES; 
    newAnnotation.exclusiveTouch = YES; 
    newAnnotation.highlighted = YES; 
    newAnnotation.multipleTouchEnabled = YES; 
    newAnnotation.pinColor = MKPinAnnotationColorRed; 
    newAnnotation.userInteractionEnabled = YES; 


    //button on the right for popup for pins 
     UIButton* rightButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure]; 
    [rightButton setTitle:annotation.title forState:UIControlStateNormal]; 
    [rightButton addTarget:self 
        action:@selector(mapCallOutPressed:) 
      forControlEvents:UIControlEventTouchUpInside]; 
    newAnnotation.rightCalloutAccessoryView = rightButton; 


    //Create and add the right button to the callout 
    UIButton* rightCalloutButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure]; 

    rightCalloutButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure]; 
    [newAnnotation setRightCalloutAccessoryView:rightButton]; 


    //zoom button on the left of popup for pins 
    UIButton* leftButton = [UIButton buttonWithType:UIButtonTypeContactAdd]; 
    [leftButton setTitle:annotation.title forState:UIControlStateNormal]; 
    [leftButton addTarget:self 
        action:@selector(zoomToLocation:) forControlEvents:UIControlEventTouchUpInside]; 
    newAnnotation.leftCalloutAccessoryView = leftButton; 

    UIImageView *profileIconView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"tag.png"]]; 
    newAnnotation.leftCalloutAccessoryView = profileIconView; 


    return newAnnotation; 
} 


- (void)mapView:(MKMapView *)aMapView didAddAnnotationViews:(NSArray *)views{ 
    for (id<MKAnnotation> currentAnnotation in mapView.annotations) { 
      [mapView selectAnnotation:currentAnnotation animated:YES]; 

    } 
} 

// method for the annotation detail disclosure button 
-(void)mapCallOutPressed:(id)sender { 

    NSLog(@"mapcallout pressed"); 

} 

- (void)didReceiveMemoryWarning 
{ 
    [super didReceiveMemoryWarning]; 
    // Dispose of any resources that can be recreated. 
} 

@end 

回答

1

你應該稍微延遲從mapView:(MKMapView *)aMapView didAddAnnotationViews:(NSArray *)views

- (void)mapView:(MKMapView *)aMapView didAddAnnotationViews:(NSArray *)views{ 
    int64_t delayInSeconds = 0.1; 
    dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds * NSEC_PER_SEC); 
    dispatch_after(popTime, dispatch_get_main_queue(), ^(void){ 
     for (id<MKAnnotation> currentAnnotation in mapView.annotations) { 
      [mapView selectAnnotation:currentAnnotation animated:YES]; 

     } 
    }); 
} 

*編輯

試過6.0.1設備上,6.0和5.1模擬器調用它時派遣[MKMapView selectAnnotation:animated:]方法。它絕對有效,我懷疑你沒有正確設置你的註釋標題,通過零。

嘗試改變標題,以一個固定值,如下所示:

annotationPoint.title = @"Hello world"; 

* EDIT2:

你的財產申報應該看起來可能更像

@property (nonatomic, copy) NSString *vendorStr; 
@property (nonatomic, strong) NSNumber *costAmount; 
+0

謝謝,但沒有奏效。不知道它是否有幫助,但如果我註釋掉這一行// newAnnotation.draggable = YES;我無法觸摸顯示標註的引腳。 – hanumanDev

+0

我複製粘貼你的代碼,在模擬器上試過,它正在工作。嘗試增加延遲並檢查annotation.title屬性是否爲零。 –

+0

註釋顯示何時觸摸標題和副標題的引腳。它不會在負載上彈出。另一個奇怪的是,如果我禁用newAnnotation.draggable,那麼你不能觸摸該引腳。 – hanumanDev

1

嘗試加入這一行的在添加註釋之後立即將代碼添加到viewWillAppear方法中:

[mapView selectAnnotation:annotationPoint animated:YES]; 
+0

感謝,但沒有奏效。 – hanumanDev

+1

這太糟糕了,這正是我在我的項目中所做的,並且對我有用。嘗試將其從視圖移動到視圖出現在屏幕上。我的代碼被調用後,視圖已經完全顯示 – Bek

1

我已經等了0.8秒後視圖確實出現。

[self performSelector:@selector(testMethod) withObject:nil afterDelay:.8]; 

-(void) testMethod 
{ 
    [self.mapView selectAnnotation:annotation animated:NO]; 
} 

動畫:NO的伎倆

相關問題