2

マップビューが読み込まれた直後に注釈の吹き出しを表示しようとしています。私は単純なものが欠けていると確信していますが、それを見つけることができません。

助けてくれてありがとう。

ここに私のコードがあります:

私の .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:

#import "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
4

3 に答える 3

1

[MKMapView selectAnnotation:animated:]から呼び出すときは、少し遅れてメソッドをディスパッチする必要があります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シミュレーターで試してみました。それは間違いなく機能します。nilを渡してアノテーションタイトルを適切に設定していないのではないかと思います。

次のように、タイトルを修正値に変更してみてください。

annotationPoint.title = @"Hello world";

* EDIT2:

あなたの財産宣言はおそらくもっと似ているはずです

@property (nonatomic, copy) NSString *vendorStr;
@property (nonatomic, strong) NSNumber *costAmount;
于 2012-11-16T13:57:57.333 に答える
1

viewWillAppear注釈を追加した直後に、次のコード行をメソッドに追加してみてください。

    [mapView selectAnnotation:annotationPoint animated:YES];
于 2012-11-16T13:55:47.433 に答える
1

ビューが表示されてから 0.8 秒待ちました。

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

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

アニメーション:いいえ、うまくいきました

于 2012-11-16T15:05:11.183 に答える