OGeek|极客世界-中国程序员成长平台

标题: ios - 添加自定义注释引脚 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-11 17:22
标题: ios - 添加自定义注释引脚

我正在尝试在折线的起点和终点添加自定义注释图钉。但我不知道该怎么做。这是我的 map 图片。

Green annotation pin on the current location of user

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

这是我的代码

- (MKAnnotationView *)mapViewMKMapView *)mapView viewForAnnotationid <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;
    }

}



Best Answer-推荐答案


您需要创建两个注解:一个带有折线起点坐标的注解和一个带有折线终点坐标的注解。将两个注释都添加到 map 中。

注解 View 以相应注解的坐标为中心。如果您希望图像中绿色引脚的底部出现在注释的坐标处(而不是引脚图像的中心),您需要使用 centerOffset 属性>MKAnnotationView 对象向上移动图像:

annotationView.canShowCallout=YES;
annotationView.image = [UIImage imageNamed"mapPin.png"];
// Move the pin image up 20 points to place the bottom of the pin
// at the annotation coordinate (adjust the value -20 to suit)
annotationView.centerOffset = CGPointMake(0, -20);

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

关于ios - 添加自定义注释引脚,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39179658/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://ogeek.cn/) Powered by Discuz! X3.4