2

次のコードがあります。

- (void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)annotation{
    annotation.image = [UIImage imageNamed:@"pinIconOn.png"];
}

- (void)mapView:(MKMapView *)mapView didDeselectAnnotationView:(MKAnnotationView *)annotation{
    annotation.image = [UIImage imageNamed:@"pinIconOff.png"];
}

ただし、ユーザーの場所を選択すると、ピン アイコンが表示されます。選択注釈をユーザーの場所では無効に設定し、他のすべての注釈では有効にするにはどうすればよいですか?

4

2 に答える 2

1

デリゲート メソッドでは、選択した注釈が型MKUserLocationであるかどうかを確認できます。型である場合は、画像を変更しません。

MKUserLocationユーザー位置アノテーションの文書化されたクラスです。

これらのデリゲート メソッドでは、2 番目のパラメーターはMKAnnotationView.
そのクラスにはannotation、ビューの基になる注釈モデル オブジェクトを指すプロパティがあります。プロパティのタイプを確認しannotationます。

例えば:

- (void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)annotation{
    
    if ([annotation.annotation isKindOfClass:[MKUserLocation class]])
    {
        //it's the user location, do nothing
        return;
    }
    
    annotation.image = [UIImage imageNamed:@"pinIconOn.png"];
}

- (void)mapView:(MKMapView *)mapView didDeselectAnnotationView:(MKAnnotationView *)annotation{

    if ([annotation.annotation isKindOfClass:[MKUserLocation class]])
    {
        //it's the user location, do nothing
        return;
    }
    
    annotation.image = [UIImage imageNamed:@"pinIconOff.png"];
}

2 つの追加の提案:

  1. annotationこれらのデリゲート メソッドでは、パラメーターに名前を付けないでください。ドキュメントで提案されているのと同じ名前を使用します。これはview、それがパラメーターの実際の名前であるためです。これは注釈のビューオブジェクトであり、注釈モデルオブジェクト自体ではありません。これにより、デリゲート メソッドのコードがわかりにくくなります。

    に変更(MKAnnotationView *)annotationする(MKAnnotationView *)viewと、チェックが になりif ([view.annotation isKindOfClass:[MKUserLocation class]])ます。

  2. 理想的には、ビュー上の画像を変更するだけでなく、これらのデリゲート メソッドが呼び出されたときに、注釈モデル オブジェクトに「選択された」状態を格納する必要があります。次に、 でviewForAnnotation、コードは注釈の状態をチェックし、デリゲート メソッドと同じロジックを使用してそこに画像を設定する必要があります (「選択」されているかどうかに基づいて異なる画像)。ユーザーがマップをズーム/パンすると、イメージが で指定された値に戻る場合がありますviewForAnnotation

于 2014-05-01T12:26:41.023 に答える
0

私があなたの質問を正しく理解していれば、ピンをドロップするたびにすべてのピンを保持する型 (NSMutableArray) の annotationArray を使用する必要があります。例:次のようなもの:

MKPointAnnotation *annotationPoint = [[MKPointAnnotation alloc]init];
annotationPoint.coordinate = annotationCoord;
annotationPoint.title = yourPinName;//add more things...

[annotationArray addObject:annotationPoint];
[self addAnnotation:annotationPoint];//self is your mapView

これはまったく役に立ちますか?

于 2014-05-01T03:06:46.450 に答える