尝试从MKMapItem获取坐标时出错



一些快速的后台,我执行了一个搜索位置搜索请求。搜索请求的代码如下所示:

搜索

-(void)performSearch {
    // activate search inidicator
    [self.completingSearchIndicator startAnimating];
    self.completingSearchIndicator.hidesWhenStopped = YES;
    self.doneButton.enabled = NO;
    self.cancelButton.enabled = NO;
    self.searchButton.enabled = NO;
    // Create a search request
    MKLocalSearchRequest *request = [[MKLocalSearchRequest alloc] init];
    request.naturalLanguageQuery = self.searchText.text;
    // adjust the region of search so it is about 5000m x 5000m
    // about 2.5x bigger than viewing region
    // we should allow users to adjust this ****
    MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(self.mapHandle.userLocation.location.coordinate, 5000, 5000);;
    request.region = region;
    MKLocalSearch *search = [[MKLocalSearch alloc] initWithRequest:request];
    [search startWithCompletionHandler:^ (MKLocalSearchResponse *response, NSError *error)
     {  // a block which loads each item into an array for us to use
        // an array of MKMapItem objects
         NSMutableArray *placemarks = [NSMutableArray array];
         for (MKMapItem *item in response.mapItems) {
             [placemarks addObject:item.placemark];
         }
         // save results in an instance variable
         self.searchResults = placemarks;
         // reactivate everything!
         [self.completingSearchIndicator stopAnimating];
         self.doneButton.enabled = YES;
         self.cancelButton.enabled = YES;
         self.searchButton.enabled = YES;
     }];
}

从中我收到一个MKMapItems数组,我选择要保存的,并将它们存储在另一个数组中。从这里我想打印出它们的坐标,然而,在试图访问它们时,我一直遇到错误。我用来访问它们的代码是:

    for (MKMapItem *item in self.selectedPlaces) {
        MKPlacemark *temp = item.placemark;
        CLLocationCoordinate2D coords = temp.coordinate;
        NSString *coordinateString = [NSString stringWithFormat:@"%f", coords.latitude];
        NSLog(@"%@",coordinateString);
    }

我得到的错误是:

2014-04-21 20:15:30.931 iTasks[2759:60b] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[MKPlacemark placemark]: unrecognized selector sent to instance 0x193a5610'

任何决议或替代策略都将不胜感激!非常感谢!

也许你想要这个?

for (MKPlacemark *item in self.selectedPlaces) {
    CLLocationCoordinate2D coords = item.location.coordinate;
    NSString *coordinateString = [NSString stringWithFormat:@"%f", coords.latitude];
    NSLog(@"%@",coordinateString);
}

相关内容

  • 没有找到相关文章

最新更新