0

下面,我为我的地图创建了一个注释,它完美地显示了标题和副标题。

但我希望在注释的左侧添加一个小图像,但无法弄清楚我需要对下面的代码做什么才能使其工作。

// Annotation
CLLocationCoordinate2D poseLocation;
poseLocation.latitude = POSE_LATITUDE;
poseLocation.longitude = POSE_LONGITUDE;

Annotation *myAnnotation = [[Annotation alloc] init];
myAnnotation.coordinate = poseLocation;
myAnnotation.title = @"Pose Beauty Salon";
myAnnotation.subtitle = @"100, Moneyhaw Road";

[self.myMapView addAnnotation:myAnnotation];
4

2 回答 2

2

您需要先设置 myMapView 的委托并实现 viewForAnnotation 委托方法。

在那里,您将返回具有 leftCalloutAccessoryView 属性的 MKAnnotationView 实例:

显示在标准标注气泡左侧的视图。此属性的默认值为 nil。左侧标注视图通常用于显示有关注释的信息或链接到应用程序提供的自定义信息。视图的高度应为 32 像素或更小。

在 leftCalloutAccessoryView 中,您可以在那里分配您的图像。例如:

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

    MKPinAnnotationView *annotationView = (MKPinAnnotationView *)[self.myMapView dequeueReusableAnnotationViewWithIdentifier:@"annotation"];

    if (annotationView == nil) {
        annotationView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"annotation"];            
        UIImageView *imageView = //initialize your image view here
        annotationView.leftCalloutAccessoryView = imageView;
    }

    annotationView.annotation = annotation;
    return annotationView;
}

PS:显然你之前在这里问过类似的问题:Add image to the left of my annotations。我不确定您是否需要发布另一个问题。请先尝试实施。

于 2013-06-03T15:04:28.083 回答
0
-(MKAnnotationView *) mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation{
MKPinAnnotationView *MyPin=[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"current"];

myImage = [UIImage imageNamed:imagename];
CGRect cropRect = CGRectMake(0.0, 0.0,35.0, 35.0);
myImageView = [[UIImageView alloc] initWithFrame:cropRect];
myImageView.clipsToBounds = YES;
myImageView.image = myImage;
MyPin.leftCalloutAccessoryView =myImageView;
MyPin.highlighted=YES;
MyPin.canShowCallout=YES;
return MyPin;

}

对我有用,试试这个

于 2013-06-12T08:45:27.800 回答