0

好吧,我会尽力解释这一点。方法“searchBarSearchButtonClicked:”从提供 MKLocalSearch 的数据源访问单例模式。然后,我们使用共享实例在 MapViewController 中设置点注释。所有这一切都完美无缺。但我不知道如何从任何给定点注释的标题运行 safari 搜索。

我的注释视图中有一个按钮。在该按钮的操作中,我可以打开 safari 并运行搜索,但搜索与注释标题不匹配。现在发生的事情是,它将随机注释标题附加到地图上显示的所有兴趣点。本质上,按钮不知道每个兴趣点对应的标题。

用户故事:假设我在地图上搜索“披萨”,我会为该用户位置附近的每个披萨餐厅获取 Pin 图。当我点击注释视图中的 safari 搜索按钮时,附加到搜索的字符串是比萨餐厅名称之一,但它不是与我单击的注释视图匹配的名称。假设我点击了多米诺骨牌或小凯撒,在 safari 中完成的两项搜索都会以必胜客的形式返回。我知道这里的问题,但我不知道如何解决它。

我在 safari 按钮 (leftButtonAnnotationPressed:) 的操作方法中拥有的代码只是为了测试当我单击按钮时我在控制台中得到的输出。问题在于(我假设)与 *appendString。这是我能得到的尽可能详细的内容,当然,代码如下。您会注意到地图搜索日志的最后一个结果会附加到搜索中,无论您点击什么位置,在 leftButtonAnnoationPressed 方法的输出中。

注释

-(void)searchBarSearchButtonClicked:(UISearchBar *)searchBar{
    [self.searchBarMap resignFirstResponder];

[[DataSource sharedInstance] requestNewItemsWithText:self.searchBarMap.text withRegion:self.mapView.region completion:^{
    for (MKMapItem *items in [DataSource sharedInstance].matchingItems){
        self.pointAnnotation = [[MKPointAnnotation alloc] init];
        self.pointAnnotation.coordinate = items.placemark.coordinate;
        self.pointAnnotation.title = items.name;
        [self.mapView addAnnotation:self.pointAnnotation];
        self.storedItemNames = items.name;
        NSLog(@"%@", self.storedItemNames);
    }
  }];
}

Safari 搜索按钮操作

-(void)leftButtonAnnotationPressed:(UIButton *)sender {
    NSString *appendString = self.pointAnnotation.title;
    NSString *urlString = @"http://www.google.com/search?q=";
    NSString *appendedUrlString = [urlString stringByAppendingString:appendString];
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:appendedUrlString]];

    NSLog(@"%@", appendedUrlString);
}

地图搜索的输出

2015-07-10 17:42:54.092 BlocSpot[1324:256489] Blondie's Pizza
2015-07-10 17:42:54.094 BlocSpot[1324:256489] Uncle Vito's Pizza
2015-07-10 17:42:54.096 BlocSpot[1324:256489] Zero Zero
2015-07-10 17:42:54.098 BlocSpot[1324:256489] Postrio
2015-07-10 17:42:54.100 BlocSpot[1324:256489] Cybelle's Pizza & Ice Cream
2015-07-10 17:42:54.102 BlocSpot[1324:256489] California Pizza Kitchen
2015-07-10 17:42:54.103 BlocSpot[1324:256489] zpizza
2015-07-10 17:42:54.105 BlocSpot[1324:256489] Pachino Trattoria & Pizzeria
2015-07-10 17:42:54.107 BlocSpot[1324:256489] Milan Pizza
2015-07-10 17:42:54.109 BlocSpot[1324:256489] Buca di Beppo Italian Restaurant

leftButtonAnnotationPressed 的输出:(实际附加了哪个字符串)

2015-07-10 17:42:59.555 BlocSpot[1324:256489] http://www.google.com/search?q=Buca di Beppo Italian Restaurant
2015-07-10 17:43:00.967 BlocSpot[1324:256489] http://www.google.com/search?q=Buca di Beppo Italian Restaurant
2015-07-10 17:43:01.982 BlocSpot[1324:256489] http://www.google.com/search?q=Buca di Beppo Italian Restaurant
2015-07-10 17:43:08.141 BlocSpot[1324:256489] http://www.google.com/search?q=Buca di Beppo Italian Restaurant
2015-07-10 17:43:10.374 BlocSpot[1324:256489] http://www.google.com/search?q=Buca di Beppo Italian Restaurant
2015-07-10 17:43:12.111 BlocSpot[1324:256489] http://www.google.com/search?q=Buca di Beppo Italian Restaurant
2015-07-10 17:43:14.395 BlocSpot[1324:256489] http://www.google.com/search?q=Buca di Beppo Italian Restaurant

更新

    MKPointAnnotation *annotation = [self.mapView.selectedAnnotations objectAtIndex:([self.mapView.selectedAnnotations count]) -1];
    NSString *appendString = annotation.title;
    NSString *urlString = @"http://www.google.com/search?q=";
    NSString *appendedUrlString = [urlString stringByAppendingString:appendString];
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:appendedUrlString]];

    NSLog(@"appended string: %@", appendedUrlString);
}

进步

MKPointAnnotation *annotation = [self.mapView.selectedAnnotations objectAtIndex:([self.mapView.selectedAnnotations count]) -1];
NSString *appendString = annotation.title;

这行小代码似乎可以解决问题。截至目前,我正在将正确的输出记录到控制台,所以它可以工作。我对 -1 以及它的作用有点困惑。因此,如果有人对 *annotation 中的 -1 有任何意见,请随时发表评论。一旦新的视图控制器和 webView 执行搜索,就会出现更多。

4

1 回答 1

0

MKMapView 提供了一个名为 .selectedAnnotations 的属性。您可以在 mapView 上调用它以获取选定的标题,并将其附加到字符串中。在这里,我们使用它来运行网络搜索并将其附加到 URL 字符串中。

.selectedAnnotations 的属性声明如下所示:

@property(nonatomic, copy) NSArray *selectedAnnotations

该属性由 MKMapView 委托为您声明。换句话说,您不需要自己声明它。只需将消息发送到您的 mapView:self.mapView.selectedAnnotations,并确保您的类符合 MKMapViewDelegate。

MKPointAnnotation *annotation = [self.mapView.selectedAnnotations objectAtIndex:([self.mapView.selectedAnnotations count]) -1];
NSString *appendString = annotation.title;
NSString *urlString = @"http://www.google.com/search?q=";
NSString *appendedUrlString = [urlString stringByAppendingString:appendString];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:appendedUrlString]];

NSLog(@"appended string: %@", appendedUrlString);
}

*annotation 语句末尾的 -1 如下所示:

.selectedAnnotations 被声明为 NSArray,因此我们可以在其上调用 objectAtIndex。索引是从零开始的,这意味着第一项是 0。计数是从一开始的,这意味着第一项是 1。因此需要 -1 才能从基于 1 的转变为基于零的转变。换句话说,计数和索引需要匹配。

于 2015-07-13T18:29:27.500 回答