2016-05-23 7 views
0

プロジェクトでMapBoxを使い始めました。カスタム画像で注釈を追加するときに、注釈が正しく中央揃えされていません。元のピンの少し下に現れます。 注釈画像の中心点を変更することはできますか?あなたは自分の画像サイズに応じてMKAnnotationViewのオフセットの中心を調整する必要がマップボックス注釈イメージセンタリング

enter image description here

- (MGLAnnotationImage *)mapView:(MGLMapView *)mapView imageForAnnotation:(id <MGLAnnotation>)annotation { 
    MGLAnnotationImage *annotationImage = [mapView dequeueReusableAnnotationImageWithIdentifier:@"driverPinId"]; 

    if (!annotationImage) { 
     UIImage *image = [UIImage imageNamed:@"DriverPin"]; 
     annotationImageWithImage:image reuseIdentifier:@"driverPinId"]; 
    } 

    return annotationImage; 
} 
+0

@riedbunny透明なボトムパッドで新しい画像を作成せずにこれを行うことは可能ですか? – korgx9

答えて

0

それが今立ってどのように

enter image description here

:どのようにそれがあるべき

編集you can also check the MapBox Document Example and description here

ドキュメントが言うように:

- (MGLAnnotationImage *)mapView:(MGLMapView *)mapView imageForAnnotation:(id <MGLAnnotation>)annotation 
{ 
    // Try to reuse the existing ‘pisa’ annotation image, if it exists 
    MGLAnnotationImage *annotationImage = [mapView dequeueReusableAnnotationImageWithIdentifier:@"pisa"]; 

    // If the ‘pisa’ annotation image hasn‘t been set yet, initialize it here 
    if (! annotationImage) 
    { 
     // Leaning Tower of Pisa by Stefan Spieler from the Noun Project 
     UIImage *image = [UIImage imageNamed:@"pisavector"]; 

     // The anchor point of an annotation is currently always the center. To 
     // shift the anchor point to the bottom of the annotation, the image 
     // asset includes transparent bottom padding equal to the original image 
     // height. 
     // 
     // To make this padding non-interactive, we create another image object 
     // with a custom alignment rect that excludes the padding. 
     image = [image imageWithAlignmentRectInsets:UIEdgeInsetsMake(0, 0, image.size.height/2, 0)]; 

     // Initialize the ‘pisa’ annotation image with the UIImage we just loaded 
     annotationImage = [MGLAnnotationImage annotationImageWithImage:image reuseIdentifier:@"pisa"]; 
    } 

    return annotationImage; 
} 
+0

MKAnnotationViewはありません。私はMapBoxを使用しています。デフォルトではありませんmapkit – korgx9

+0

@ korgx9 MapBoxを確認させてください – Mahesh

+0

@ korgx9私の更新された回答を確認しましたか? – Mahesh

0

あなただけの画像の高さを2倍にすることにより、画像を調整する必要があります。私は点検し、それは完全に働いた。

// Adjust image by doubling the height of the image 
- (UIImage *)adjustImage:(UIImage *)image { 

    CGSize newSize = CGSizeMake(image.size.width, image.size.height * 2); 
    UIGraphicsBeginImageContextWithOptions(newSize, false, 0); 

    [image drawInRect:CGRectMake(0,0,image.size.width,image.size.height)]; 
    UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext(); 

    UIGraphicsEndImageContext(); 

    return newImage; 
} 

- (MGLAnnotationImage *)mapView:(MGLMapView *)mapView imageForAnnotation:(id <MGLAnnotation>)annotation { 
    MGLAnnotationImage *annotationImage = [mapView dequeueReusableAnnotationImageWithIdentifier:@"driverPinId"]; 

    if (!annotationImage) { 
     UIImage *image = [UIImage imageNamed:@"DriverPin"]; 

     // The anchor point of an annotation is currently always the center. To 
     // shift the anchor point to the bottom of the annotation, the image 
     // asset includes transparent bottom padding equal to the original image 
     // height. 
     image = [self adjustImage: image]; 

     // To make this padding non-interactive, we create another image object 
     // with a custom alignment rect that excludes the padding. 
     image = [image imageWithAlignmentRectInsets:UIEdgeInsetsMake(0, 0, image.size.height/2, 0)]; 

     // Initialize the ‘DriverPin’ annotation image with the UIImage we just loaded. 
     annotationImage = [MGLAnnotationImage annotationImageWithImage:image reuseIdentifier:@"driverPinId"]; 
    } 

    return annotationImage; 
} 
+0

あなたのケースでのパフォーマンスはどうですか? – korgx9