ios - 如何在 MKAnnotation 上设置标题

标签 ios mkmapview mkannotation

全部,

尝试确定如何在搜索时在注释图钉标题中显示用户搜索位置的名称,而不是预先确定它们,这很容易。用户输入位置的搜索栏当前代码如下:

- (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 替换为标题变量名称,但我的问题是它会是什么样子,这足以显示用户这些自定义搜索的标题吗?

谢谢!! 格雷格

最佳答案

如果您想坚持使用当前的 AddressAnnotation 定义,请将 showAddress 的最后两行更改为

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

您需要将 @""替换为您想要的标题。

但是,您可能希望更改 AddressAnnotation 以使用 title 而不是 _name,以便 @synthsize 为您完成所有工作。在初始化函数期间,如果需要,您可以将标题设置为“未知”。

关于ios - 如何在 MKAnnotation 上设置标题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13241238/

相关文章:

ios - 在 Swift 中每次动画迭代后延迟

iphone - 在注释 View 上使用 canShowCallout 时出现 MKMapKit 异常

ios - MapView 添加多个不同颜色的注解

ios - 缩放时如何计算 MapView 上显示的数量和哪些注释?

ios - 如何使用 mapView 和 CLLocationManager 在 iOS Swift 中绘制两个位置之间的方向

ios - 在 MKMapView 中标记位置

ios - 快捷方式是什么/如何在 Xcode 中编写函数后创建函数

ios - 无法从Firebase中删除用户

ios - 在 Swift 中对 UITabBarController 进行 segue 后如何摆脱 UINavigationController?

ios - 即使使用 set delegate,MKMapview delegate 也从未被调用