簡體   English   中英

使用地圖套件在地圖視圖中顯示左右標注附件

[英]show left and right callout accessory in map view using map kit

我有一個簡單的問題,我無法解決。 我正在從plist中提取位置的經度和緯度,並將其放置在地圖視圖上。 這部分工作正常。 引腳顯示,標題和副標題(同樣來自plist)也顯示。 這是我的視圖控制器.h類的代碼:

#import "ViewController.h"
#import "Annotation.h"

@interface ViewController ()

@end


#define WIMB_LATITUDE 51.434783;
#define WIMB_LONGITUDE -0.213428;


#define ARSENAL_LATITUDE 51.556899;
#define ARSENAL_LONGITUDE -0.106403;

#define CHELSEA_LATITUDE 51.481314;
#define CHELSEA_LONGITUDE -0.190129;


#define THE_SPAN 0.10f;

@implementation ViewController
@synthesize myMapView;

- (void)viewDidLoad
{
[super viewDidLoad];

MKCoordinateRegion myRegion;
CLLocationCoordinate2D center;
center.latitude = ARSENAL_LATITUDE;
center.longitude = ARSENAL_LONGITUDE;
MKCoordinateSpan span;
span.latitudeDelta = THE_SPAN;
span.longitudeDelta = THE_SPAN;
myRegion.center = center;
myRegion.span=span;
[myMapView setRegion:myRegion animated:YES];

NSMutableArray *annotations = [[NSMutableArray alloc]init];
NSString *path = [[NSBundle mainBundle] pathForResource:@"Stadiums" ofType:@"plist"];
NSDictionary *dict = [NSDictionary dictionaryWithContentsOfFile:path];
NSArray *anns = [dict objectForKey:@"Stadiums"];
NSLog(@"read1");

for(int i = 0; i < [anns count]; i++) {
    NSLog(@"read2");
    float realLatitude = [[[anns objectAtIndex:i] objectForKey:@"Latitude"] floatValue];
    float realLongitude = [[[anns objectAtIndex:i] objectForKey:@"Longitude"] floatValue];
    NSLog(@"read3");

    Annotation *myAnnotation = [[Annotation alloc] init];
    CLLocationCoordinate2D theCoordinate;
    theCoordinate.latitude = realLatitude;
    theCoordinate.longitude = realLongitude;
    myAnnotation.coordinate = theCoordinate;
    myAnnotation.title = [[anns objectAtIndex:i] objectForKey:@"Title"];
    myAnnotation.subtitle = [[anns objectAtIndex:i] objectForKey:@"Subtitle"];
    [myMapView addAnnotation:myAnnotation];
    [annotations addObject:myAnnotation];
    //[myAnnotation release];
}

}

- (MKAnnotationView *)mapView:(MKMapView *)map viewForAnnotation:(id <MKAnnotation>)annotation
{
static NSString *AnnotationViewID = @"annotationViewID";

MKAnnotationView *annotationView = (MKAnnotationView *)[myMapView dequeueReusableAnnotationViewWithIdentifier:AnnotationViewID];

if (annotationView == nil)
{
    annotationView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:AnnotationViewID];
}

UIButton *rightButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
[rightButton addTarget:nil action:nil forControlEvents:UIControlEventTouchUpInside];
annotationView.rightCalloutAccessoryView = rightButton;

UIImageView *IconView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"toilets.png"]];
annotationView.leftCalloutAccessoryView = IconView;

annotationView.canShowCallout = YES;
annotationView.annotation = annotation;
return annotationView;
}


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

@end

我的問題是左右標注配件永遠不會被調用。 當按下圖釘時,我只獲得標題和副標題。 我嘗試了許多不同的方法,但似乎無法解決此問題。 誰能幫我修復此代碼,以便我可以顯示配件。

太感謝了

ps我正在使用iOS 6和Storyboard,並且該項目僅基於iphone。

您應該在– mapView:didSelectAnnotationView:委托方法中實現左右標注。 所以,你的代碼

annotationView.leftCalloutAccessoryView = IconView;

annotationView.canShowCallout = YES; 必須采用上述方法。 嘗試調用默認注釋。 您會明白的。

希望這可以幫助。:)

我認為您需要實現以下方法:

-(void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control

啊,明白了。 因為安娜在暗示沒有任命代表的提示中是對的。 我以錯誤的方式設置了代表。 我要做的就是將其添加到我的視圖控制器頭視圖中,而不是嘗試在我的注釋視圖中輸入它,然后在視圖控制器的視圖中調用該委托將方法加載到self,例如:myMapView.delegate = self; 該代碼一直在工作,但是由於缺少委托,我的注解視圖沒有被激發。 再次感謝Anna Karennina的提示。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM