【问题标题】:show another view when clicked on pin annotation单击引脚注释时显示另一个视图
【发布时间】:2014-01-04 13:34:42
【问题描述】:

我有很多车库,需要在地图上显示多个车库。

下面是我用于地图显示多个图钉的代码。

CLLocationCoordinate2D annotationCoord;
for (NSMutableDictionary* aDict in Gfeeds) {
    annotationCoord.latitude = [aDict[@"GLatitude"] floatValue];
    annotationCoord.longitude = [aDict[@"GLongitude"] floatValue];

    MKPointAnnotation *annotationPoint2 = [[MKPointAnnotation alloc] init];
    annotationPoint2.coordinate = annotationCoord;
    annotationPoint2.title = [NSString stringWithFormat:@"%@", aDict[@"GName"]];
    annotationPoint2.subtitle = aDict[@"GId"];

    [geomapView addAnnotation:annotationPoint2];

}

现在我想要的是在点击那个地图图钉后显示车库的详细信息……我正在下面这样做。

-(MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation{

    MKPinAnnotationView *pinAnnotation = nil;
    if(annotation != geomapView.userLocation)
    {
        static NSString *defaultPinID = @"myPin";
        pinAnnotation = (MKPinAnnotationView *)[geomapView dequeueReusableAnnotationViewWithIdentifier:defaultPinID];
        if ( pinAnnotation == nil )
            pinAnnotation = [[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:defaultPinID] autorelease];

        pinAnnotation.canShowCallout = YES;

        //instatiate a detail-disclosure button and set it to appear on right side of annotation
        UIButton *infoButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
        pinAnnotation.rightCalloutAccessoryView = infoButton;

    }

    return pinAnnotation;
}

- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control {
    GarageDetailsViewController *secondView = [self.storyboard instantiateViewControllerWithIdentifier:@"GarageDetails"];

    secondView.garageMainId = view.annotation.subtitle;

    [self.navigationController pushViewController:secondView animated:YES];
}

使用这种方式,我可以转到 GarageDetails,但我冒的风险是在字幕中显示车库 ID。我想隐藏这个字幕,以免显示 Id。

知道我该怎么做吗?

  1. 隐藏字幕

  1. 将 id 从地图传递给 GarageDetails...

【问题讨论】:

    标签: objective-c mkmapview mapkit mkpointannotation


    【解决方案1】:

    为什么不将“MKPointAnnotation”子类化(您可以将其命名为“FahimPointAnnotation”之类的名称),并且在此子类中您可以添加“garageID”属性。

    然后您可以将注释添加到您的地图,当它被点击时,您可以检索注释并将“MKPointAnnotation”转换回“FahimPointAnnotation”并从中获取您的garageID(无需担心它会出现在注释视图的字幕字段中)。

    【讨论】:

    • 实际上,子类应该是 MKPointAnnotation(模型)而不是 MKAnnotationView。或者可以创建一个实现 MKAnnotation 协议的自定义类。在 calloutAccessoryControlTapped 中,可以通过 view.annotation 访问注解模型对象。
    • 啊,非常好的澄清@Anna。我现在就去修正我的答案。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-15
    • 2023-03-23
    • 2013-10-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多