3

マップ上の注釈用のカスタム注釈ビューを作成しようとしています。私はプロトコルMKMapViewDelegateを適応させ、関数を上書きすることでそれを行っていますmapView:viewForAnnotation:。それはすべて機能しますが、唯一の問題は、私も にshowsUserLocation設定してTRUEいることです。つまり、メソッドで取得する「注釈」の 1 つmapView:viewForAnnotation:が classMKUserLocationです。

ユーザー位置注釈にカスタム注釈ビューを持たせたくありません。デフォルトのユーザー位置注釈ビューを表示したいのです! ユーザーロケーションのデフォルトのユーザーロケーションアノテーションビューを返す、またはアノテーションから除外するにはどうすればよいmapView:viewForAnnotation:ですか?

メソッドでUserLocation をキャッチしようとしましたが、mapView:viewForAnnotation:何を返せばよいかわかりません。(この例では、標準の MKAnnotationView を返していますが、それはデフォルトの UserLocation Annotation のようには見えません(明らかに)。)

    if (![[annotation class] isEqual:[MKUserLocation class]]) {

        MKAnnotationView *view = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"customAnnotation"];
        // edit the custom view
        return view;
    }

    MKAnnotationView *view = [[MKAnnotationView alloc] init];
    return view;
4

4 に答える 4

8

ユーザーの場所のデフォルトの注釈を表示するにはnil、その場合に返すだけです。次のようにしました。

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation
{
    // use your custom annotation
    if ([annotation isKindOfClass:[MyAnnotationClass class]]) {
        ...

        return annotationView;
    }

    // use default annotation
    return nil;
}
于 2013-03-19T09:06:41.100 に答える
2

viewForAnnotation メソッド内に次のコードを記述します。ここで、var 'map' は MKMapview のアウトレットです。

   - (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id < MKAnnotation >)annotation
    {



        //Annoation View for current Location

        if(map.userLocation != annotation)

        {
            UIImage *image = [UIImage imageNamed:@"image.png"];
            annotation.image = image;

            return annotation; 


        }

        //Annotation View for current location

         return nil;

    }
于 2013-03-19T09:04:32.060 に答える
1

カスタムを作成AnnotationView:

#import <MapKit/MapKit.h>

@interface AnnotationView : MKPlacemark

@property (nonatomic, readwrite, assign) CLLocationCoordinate2D coordinate;

@property (nonatomic, strong) NSString *title;
@property (nonatomic, strong) NSString *subtitle;

// you can put here any controllers that you want. (such like UIImage, UIView,...etc)

@end

そして、.m file

#import "AnnotationView.h"

@implementation AnnotationView

- (id)initWithCoordinate:(CLLocationCoordinate2D)coordinate addressDictionary:(NSDictionary *)addressDictionary
{
    if ((self = [super initWithCoordinate:coordinate addressDictionary:addressDictionary]))
    {
        self.coordinate = coordinate;
    }
    return self;
}

@end

// 関連する Annotation Add を使用#import "AnnotationView.h"します.m file:

CLLocationCoordinate2D pCoordinate ;
pCoordinate.latitude = LatValue;
pCoordinate.longitude = LanValue;

// Create Obj Of  AnnotationView class  

AnnotationView *annotation = [[AnnotationView alloc] initWithCoordinate:pCoordinate addressDictionary:nil] ;

    annotation.title = @"I m Here";
    annotation.subtitle = @"This is Sub Tiitle";

[self.mapView addAnnotation:annotation];

上記は の作成方法の簡単な例ですAnnotationView

于 2013-03-19T09:05:29.877 に答える
0

注釈パラメーターのオブジェクトが MKUserLocation クラスのインスタンスである場合、ユーザーの場所を示すカスタム ビューを提供できます。デフォルトのシステム ビューを使用してユーザーの位置を表示するには、nil を返します。このメソッドを実装しない場合、またはユーザー位置アノテーション以外のアノテーションに対して実装から nil を返す場合、マップ ビューは標準のピン アノテーション ビューを使用します。

   - (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id < MKAnnotation >)annotation
    {

       // if it's the user location, just return nil or custom annotation view.
       if ([annotation isKindOfClass:[MKUserLocation class]]){
          return nil;
       } else {
          //return other annotations
       }

    }
于 2013-03-20T00:41:12.443 に答える