Iphone 使用地图套件在地图视图中显示左侧和右侧详图索引附件

Iphone 使用地图套件在地图视图中显示左侧和右侧详图索引附件,iphone,ios6,storyboard,mapkit,callouts,Iphone,Ios6,Storyboard,Mapkit,Callouts,我有一个简单的问题,我想不出答案。我正在从plist中提取位置的long和lat,并将它们放置在地图视图中。这部分很好用。pins显示,标题和副标题(同样来自plist)也显示。以下是my view controller.h类的代码: #import "ViewController.h" #import "Annotation.h" @interface ViewController () @end #define WIMB_LATITUDE 51.434783; #define WIM

我有一个简单的问题,我想不出答案。我正在从plist中提取位置的long和lat,并将它们放置在地图视图中。这部分很好用。pins显示,标题和副标题(同样来自plist)也显示。以下是my view controller.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
#导入“ViewController.h”
#导入“Annotation.h”
@界面视图控制器()
@结束
#定义WIMB_纬度51.434783;
#定义WIMB_经度-0.213428;
#定义纬度51.556899;
#定义经度-0.106403;
#定义纬度51.481314;
#定义经度-0.190129;
#定义_跨度0.10f;
@实现视图控制器
@综合myMapView;
-(无效)viewDidLoad
{
[超级视图下载];
MK协调区域myRegion;
位置协调中心;
center.latitude=阿森纳_纬度;
center.longitude=经度;
Mk坐标跨度;
span.latitudeDelta=_span;
span.longitudeDelta=_span;
myRegion.center=中心;
myRegion.span=span;
[myMapView setRegion:myRegion动画:是];
NSMutableArray*注释=[[NSMutableArray alloc]init];
NSString*path=[[NSBundle mainBundle]pathForResource:@“体育场”的类型:@“plist”];
NSDictionary*dict=[NSDictionary Dictionary WithContentsOfFile:path];
NSArray*anns=[dict objectForKey:@“体育场”];
NSLog(@“read1”);
对于(int i=0;i<[ANN计数];i++){
NSLog(@“read2”);
float realLatitude=[[anns objectAtIndex:i]objectForKey:@“纬度”]floatValue];
float realLongitude=[[anns objectAtIndex:i]objectForKey:@“Longitude”]floatValue];
NSLog(@“read3”);
注释*myAnnotation=[[Annotation alloc]init];
CLLOCATION Coordinated2和Coordinate;
坐标纬度=真实纬度;
坐标经度=真实经度;
myAnnotation.coordinate=坐标;
myAnnotation.title=[[anns objectAtIndex:i]objectForKey:@“title”];
myAnnotation.subtitle=[[anns objectAtIndex:i]objectForKey:@“subtitle”];
[myMapView addAnnotation:myAnnotation];
[注释addObject:myAnnotation];
//[myAnnotation release];
}
}
-(MKAnnotationView*)地图视图:(MKMapView*)地图视图注释:(id)注释
{
静态NSString*AnnotationViewID=@“AnnotationViewID”;
MKAnnotationView*annotationView=(MKAnnotationView*)[myMapView出列重用AnnotationViewWithIdentifier:AnnotationViewID];
如果(注释视图==nil)
{
annotationView=[[MKAnnotationView alloc]initWithAnnotation:AnnotationReuseIdentifier:AnnotationViewID];
}
UIButton*rightButton=[UIButton Button类型:UIButtonTypedTailButton];
[右按钮添加目标:无操作:无控制事件:UIControlEventTouchUpInside];
annotationView.rightCalloutAccessoryView=rightButton;
UIImageView*IconView=[[UIImageView alloc]initWithImage:[UIImageName:@“loosts.png”];
annotationView.leftCalloutAccessoryView=IconView;
annotationView.canShowCallout=是;
annotationView.annotation=注释;
返回注释视图;
}
-(无效)未收到记忆警告
{
[超级记忆警告];
//处置所有可以重新创建的资源。
}
@结束
我的问题是,左侧和右侧标注附件从未被调用。当按下别针时,我只得到标题和副标题。我尝试了很多不同的方法,但似乎无法解决这个问题。谁能帮我修一下这个代码,这样我就能把配件拿出来展示了

万分感谢


p、 我使用的是iOS 6和故事板,该项目仅基于iphone。

我认为您需要实现以下方法:

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

啊,明白了。因为Anna在暗示未设置代理时是正确的。我将代理设置为错误的方式。我所要做的就是将添加到我的视图控制器标题视图中,而不是尝试在注释视图中输入它,然后在视图控制器的视图中调用委托并将方法加载到self,如下所示:myMapView.delegate=self;
代码一直在工作,但由于缺少委托,我的注释视图没有被触发。再次感谢Anna Karennina的提示。

您应该在
–mapView:didSelectAnnotationView:
委托方法中实现左右调用。 那么,你的代码

annotationView.leftCalloutAccessoryView = IconView;
annotationView.canShowCallout=YES必须进入上述方法。
尝试调用默认注释。你会明白的


希望这有帮助。:

这是当注释中已经显示附件按钮时调用的方法。我的问题是批注没有显示我在view for annotation中声明的附件按钮。是否确实调用了viewForAnnotation方法?它的编写方式是创建无图像集的普通MkAnnotationView(而不是MkPinAnnotationView),这样它们就不可见了。如果注释显示为红色接点,这是因为首先没有调用该方法(可能是因为没有设置贴图视图的委托),并且贴图视图创建默认接点视图。另外请注意,如果您打算使用CalloutAccessoryControlTapped委托方法,则不应在“附件”按钮上进行手动添加。@Annakarena,谢谢您的建议。今晚我将回到这个项目,我可能会在修改代码后向你寻求帮助。关于代表,你可能是对的。谢谢你的回答,但我已经回答了我的问题。我在错误的类中声明的是委托方法。但是谢谢你的努力。