gpt4 book ai didi

ios - 从 MKLocalSearch 创建注释标题

转载 作者:行者123 更新时间:2023-11-29 10:48:11 26 4
gpt4 key购买 nike

我已经添加了一个 MKLocalSearch,并且引脚显示正确。唯一的问题是别针标题有名字和地址,我只想要名字。我将如何改变这个。这是我正在使用的代码 -

- (void)mapView:(MKMapView *)mapView regionDidChangeAnimated:(BOOL)animated
{

MKLocalSearchRequest *request = [[MKLocalSearchRequest alloc] init];
request.naturalLanguageQuery = @"School";
request.region = mapView.region;

MKLocalSearch *localSearch = [[MKLocalSearch alloc] initWithRequest:request];
[localSearch startWithCompletionHandler:^(MKLocalSearchResponse *response, NSError *error) {

NSMutableArray *annotations = [NSMutableArray array];

[response.mapItems enumerateObjectsUsingBlock:^(MKMapItem *item, NSUInteger idx, BOOL *stop) {

// if we already have an annotation for this MKMapItem,
// just return because you don't have to add it again

for (id<MKAnnotation>annotation in mapView.annotations)
{
if (annotation.coordinate.latitude == item.placemark.coordinate.latitude &&
annotation.coordinate.longitude == item.placemark.coordinate.longitude)
{
return;
}
}

// otherwise, add it to our list of new annotations
// ideally, I'd suggest a custom annotation or MKPinAnnotation, but I want to keep this example simple
[annotations addObject:item.placemark];
}];

[mapView addAnnotations:annotations];
}];
}

最佳答案

由于 item.placemarktitle 不能直接修改,您需要创建自定义注释或 MKPointAnnotation 使用来自 item.placemark 的值。

(addObject 行上方代码中的注释提到了“MKPinAnnotation”,但我认为它的意思是“MKPointAnnotation”。)

下面的示例使用了简单的选项,即使用 SDK 提供的预定义 MKPointAnnotation 类来创建您自己的简单注释。

替换这一行:

[annotations addObject:item.placemark];

这些:

MKPlacemark *pm = item.placemark;

MKPointAnnotation *ann = [[MKPointAnnotation alloc] init];
ann.coordinate = pm.coordinate;
ann.title = pm.name; //or whatever you want
//ann.subtitle = @"optional subtitle here";

[annotations addObject:ann];

关于ios - 从 MKLocalSearch 创建注释标题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21617414/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com