2

私は多くのピンを作成しました。ピンを押すと、タイトルが表示される必要がありますが、サブタイトルは非表示にする必要があります。これは、長いテキストであり、UItextView に表示されるためです。問題は、サブタイトルを非表示にする方法が見つからなかったことです。そのため、タイトルの下に、次で終わる長いテキストがあります: ...

- (void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view
{
    MKPointAnnotation *myAnnot = (MKPointAnnotation *)view.annotation;
    field.text = myAnnot.subtitle;
}

残念ながら、MKPointAnnotation にタグを割り当てる方法が見つからないため、この方法を使用する必要がありました。これは私がそれを作成する方法です:

MKPointAnnotation *annotationPoint2 = [[MKPointAnnotation alloc] init];
annotationPoint2.coordinate = anyLocation;

annotationPoint2.title = [NSString stringWithFormat:@"%@", obj];
annotationPoint2.subtitle = [NSString stringWithFormat:@"%@", key];
4

2 に答える 2

3

組み込みクラスを使用する代わりに、コールアウトに表示したくないデータを保持する追加のプロパティ ( という名前ではない) をMKPointAnnotation実装するカスタム アノテーション クラスを作成します。MKAnnotationsubtitle

この回答には、単純なカスタム アノテーション クラスの例が含まれています。

その例では、@property (nonatomic, assign) float myValue;各注釈で追跡するデータに置き換えます (例: @property (nonatomic, copy) NSString *keyValue;)。

次に、次のように注釈を作成します。

MyAnnotation *annotationPoint2 = [[MyAnnotation alloc] init];
annotationPoint2.coordinate = anyLocation;

annotationPoint2.title = [NSString stringWithFormat:@"%@", obj];
annotationPoint2.subtitle = @"";  //or set to nil
annotationPoint2.keyValue = [NSString stringWithFormat:@"%@", key];

次に、didSelectAnnotationViewメソッドは次のようになります。

- (void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view
{
    if ([view.annotation isKindOfClass:[MyAnnotation class]])
    {
        MyAnnotation *myAnnot = (MyAnnotation *)view.annotation;
        field.text = myAnnot.keyValue;
    }
    else
    {
        //handle other types of annotations (eg. MKUserLocation)...
    }
}

MKPointAnnotationまた、アノテーションが であると想定する、またはアノテーションを使用するコードの他の部分を更新する必要がある場合がありますsubtitle(そのコードは をチェックしMyAnnotationて使用する必要がありますkeyValue)。

于 2012-10-12T12:33:02.033 に答える
0

この簡単な方法を試すことができます。

MKPointAnnotation *point= [[MKPointAnnotation alloc] init];
point.coordinate= userLocation.coordinate;
point.title= @"Where am I?";
point.subtitle= @"u&me here!!!";
[myMapView addAnnotation:point];
于 2015-08-31T09:57:14.447 に答える