2

iPhone/iPadアプリにStartとEndの画像をオーバーレイで入れたい。緯度と経度の開始値と終了値があり、開始点と終了点の間にオーバーレイを描画し、開始点に開始画像を配置し、終了点に終了画像を配置したいと考えています。

私はグーグルで検索しましたが、MapKit が 1 つの画像を取得し、それを開始点と終了点の両方に設定すると、2 番目の画像のヘルプが見つかりませんでした。

お気に入り

annotationView.image=[UIImage imageNamed:@"parkingIcon.png"];

開始点と終了点の両方に 1 つの画像のみを設定します。しかし、両方のポイントに異なる画像を入れたいです。

助けてください。

4

3 に答える 3

8

私はそれを理解しました...私を助けようとしたすべての人に感謝します. 完全な解決策は

クラスを作成する

 @interface MyAnnotationClass : NSObject <MKAnnotation> {
        NSString *_name;
        NSString *_description;
        CLLocationCoordinate2D _coordinate;


    }
    @property (nonatomic, retain) NSString *name;
    @property (nonatomic, retain) NSString *description;
    @property (nonatomic, readonly) CLLocationCoordinate2D coordinate;

    -(id) initWithCoordinate:(CLLocationCoordinate2D) coordinate;

ViewDidLoad メソッド コード:

mapView.delegate = self;
    //Initialize annotation
    MyAnnotationClass *commuterLotAnnotation=[[MyAnnotationClass alloc] initWithCoordinate:CLLocationCoordinate2DMake( 39.047752, -76.850388)];
    commuterLotAnnotation.name = @"1";
    MyAnnotationClass *overflowLotAnnotation=[[MyAnnotationClass alloc] initWithCoordinate:CLLocationCoordinate2DMake(  39.047958, -76.852520)];
    overflowLotAnnotation.name = @"2";

    //Add them to array
    self.myAnnotations=[NSArray arrayWithObjects:commuterLotAnnotation, overflowLotAnnotation, nil];

    //Release the annotations now that they've been added to the array
    [commuterLotAnnotation release];
    [overflowLotAnnotation release];

    //add array of annotations to map
    [mapView addAnnotations:_myAnnotations];

viewForAnnotation コード:

-(MKAnnotationView *)mapView:(MKMapView *)MapView viewForAnnotation:(id<MKAnnotation>)annotation{
    static NSString *parkingAnnotationIdentifier=@"ParkingAnnotationIdentifier";

    if([annotation isKindOfClass:[MyAnnotationClass class]]){

        //Try to get an unused annotation, similar to uitableviewcells
        MKAnnotationView *annotationView=[MapView dequeueReusableAnnotationViewWithIdentifier:parkingAnnotationIdentifier];
        //If one isn't available, create a new one
        if(!annotationView){
            annotationView=[[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:parkingAnnotationIdentifier];
            if([((MyAnnotationClass *)annotation).name isEqualToString: @"1"]){
                annotationView.image=[UIImage imageNamed:@"passenger.png"];
            }
            else if([((MyAnnotationClass *)annotation).name isEqualToString: @"2"]){
                annotationView.image=[UIImage imageNamed:@"place.png"];
            }                                   
        }
        return annotationView;
    }
    return nil;
}

これは、MapKit のすべてのポイントに個別の画像を追加する方法です。

于 2012-05-17T11:20:09.963 に答える
0

-(MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation;MKAnnotationデリゲートメソッドを使用する必要があります

マップ上に特定の注釈を設定できます。

このメソッドはすべての注釈に対して返されます。終了注釈と開始注釈を見つけて、それに画像を設定するだけです。

于 2012-05-14T14:32:34.440 に答える
0

まず、MKMapViewDelegate プロトコルを実装し、デリゲート クラスをマップに設定する必要があります。

MKMapView Delegate で、メソッドを宣言します。

- (MKAnnotationView *)mapView:(id)mapView viewForAnnotation:(id <MKAnnotation>)annotation {
    if (annotation == [mapView userLocation]) // The user's blue point : no customisation.
        return nil;

    MKAnnotationView * annView = [mapView dequeueReusableAnnotationViewWithIdentifier:/* your specific identifier */];
    if (annView == nil) {
        annView = [[[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:/* your specific identifier */] autorelease];
    }
    annView.image = /* your image */;
    return annView
}

ただし、注釈が開始か終了かを検出する必要があります。特定のタイトルを設定して簡単に取得したり (@"start" など)、識別プロパティを追加して MKAnnotations をサブクラス化したりできます。

于 2012-05-14T14:57:10.883 に答える