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