添加自定义注释引脚

时间:2016-08-27 09:40:59

标签: ios objective-c mkmapview mapkit

我正在尝试在折线的起点和终点添加自定义注释引脚。但我无法想象如何做到这一点。这是我的地图的图像。

Green annotation pin on the current location of user

我想在折线的起点或终点添加绿色注释引脚。

这是我的代码

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation
{
    if (annotation==mapView.userLocation) {

        MKAnnotationView *annotationView = (MKAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:@"currAnno"];

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

        annotationView.canShowCallout=YES;
        annotationView.image = [UIImage imageNamed:@"mapPin.png"];

        return annotationView;


    }else{

        static NSString *viewId = @"MKAnnotationView";
        MKAnnotationView *annotationView = (MKAnnotationView*)
        [mapView dequeueReusableAnnotationViewWithIdentifier:viewId];

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

        annotationView.canShowCallout=YES;
        annotationView.image = [UIImage imageNamed:@"mapPin.png"];//set your image here
        return annotationView;
    }

}

1 个答案:

答案 0 :(得分:0)

您需要创建两个注释:一个注释具有折线起点的坐标,一个注记具有折线末端的坐标。将两个注释添加到地图中。

注释视图以相应注释的坐标为中心。如果您希望图像中绿色图钉的底部出现在注释的坐标处(而不是图像的中心),则需要使用{{1}的centerOffset属性对象向上移动图像:

MKAnnotationView

此示例将注释视图图像向上移动20个点(负y值向上移动图像)。但是,您应该为图像使用合适的负值而不是-20,例如减去图像高度的一半或任何看起来正确的值。

相关问题