2016-08-27 97 views
2

我想將自定義註釋針添加到多段線的開始點和結束點。但我無法想象如何做到這一點。這是我的地圖的圖像。添加自定義註釋針

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; 
    } 

} 

回答

0

您需要創建兩個註解:一個標註有折線的起點座標和一個註釋與折線的端的座標。將兩個註釋添加到地圖。

註釋視圖集中在相應註釋的座標上。如果您希望圖像中綠色針的底部出現在註釋的座標處(而不是針圖像的中心),則需要使用MKAnnotationView對象的centerOffset屬性向上移動圖像:

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); 

本示例將註釋視圖圖像上移20個點(負y值將圖像向上移動)。但是,您應該爲圖像使用合適的負值而不是-20,例如減去圖像高度的一半或任何看起來合適的圖像。