2

全て、

簡単に事前に決定するのではなく、検索時に注釈ピンのタイトル内にユーザー検索の場所の名前を表示する方法を決定しようとしています。ユーザーが場所を入力する検索バーの現在のコードは次のとおりです。

- (IBAction) showAddress
{
    [addressField resignFirstResponder];
    CLLocationCoordinate2D location = [self addressLocation];

    MKCoordinateRegion region;
    MKCoordinateSpan span;
    span.latitudeDelta=0.2;
    span.longitudeDelta=0.2;

    region.span=span;
    region.center=location;

    [mapView setRegion:region animated:TRUE];
    [mapView regionThatFits:region];

     MapViewAnnotation *addAnnotation = [[MapViewAnnotation alloc] initWithTitle:@"" andCoordinate:location];
     [self.mapView addAnnotation:addAnnotation];
}

タイトル情報を以下に宣言します。

@implementation AddressAnnotation
@synthesize _name;

-(id)initWithName:(NSString*)name address:(NSString*)address coordinate:(CLLocationCoordinate2D)coordinate  {
if ((self = [super init]))
{
    _name = [name copy];
}
    return self;
}

-(NSString *)title
{
  if ([_name isKindOfClass:[NSNull class]])
    return @"Unknown charge";
  else
    return _name;
}

@end

私の理解では、initWithTitleをtitle変数名に置き換える必要がありますが、私の質問はそれがどのように見えるかであり、ユーザーによるこれらのカスタム検索のタイトルを表示するのに十分ですか?

ありがとうございました!!グレッグ

4

1 に答える 1

2

現在の AddressAnnotation 定義に固執する場合は、showAddress の最後の 2 行を次のように変更します。

 AddressAnnotation *addAnnotation = [[AddressAnnotation alloc] initWithName:@"" coordinate:location];
 [self.mapView addAnnotation:addAnnotation];

@"" を任意のタイトルに置き換える必要があります。

titleただし、代わりにAddressAnnotation を使用するように変更し_nameて、@synthsize がすべての作業を行うようにすることもできます。init 関数の実行中に、必要に応じてタイトルを「unknown」に設定できます。

于 2012-11-05T22:35:36.807 に答える