菜鸟教程小白 发表于 2022-12-11 17:22:46

ios - 添加自定义注释引脚


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

<p> <a href="/image/lmQPf.png" rel="noreferrer noopener nofollow"><img src="/image/lmQPf.png" alt="Green annotation pin on the current location of user"/></a> </p>

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

<p>这是我的代码</p>

<pre><code>- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id &lt;MKAnnotation&gt;)annotation
{
    if (annotation==mapView.userLocation) {

      MKAnnotationView *annotationView = (MKAnnotationView *);

      if (annotationView == nil) {
            annotationView = [ initWithAnnotation:annotation reuseIdentifier:@&#34;currAnno&#34;];
      }

      annotationView.canShowCallout=YES;
      annotationView.image = ;

      return annotationView;


    }else{

      static NSString *viewId = @&#34;MKAnnotationView&#34;;
      MKAnnotationView *annotationView = (MKAnnotationView*)
      ;

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

      annotationView.canShowCallout=YES;
      annotationView.image = ;//set your image here
      return annotationView;
    }

}
</code></pre></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>您需要创建两个注解:一个带有折线起点坐标的注解和一个带有折线终点坐标的注解。将两个注释都添加到 map 中。</p>

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

<pre><code>annotationView.canShowCallout=YES;
annotationView.image = ;
// 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);
</code></pre>

<p>此示例将注释 View 图像向上移动 20 点(负 y 值将图像向上移动)。但是,您应该为您的图像使用合适的负值,而不是 -20,例如减去图像高度的一半或任何您认为合适的值。</p></p>
                                   
                                                <p style="font-size: 20px;">关于ios - 添加自定义注释引脚,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/39179658/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/39179658/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - 添加自定义注释引脚