Objective-C - 根据用户输入的查询搜索街道

标签 objective-c xcode cllocation mklocalsearch

我想让用户搜索街道名称并将结果显示在 UITableView 中。 目前地域不重要,可以是任意地域。

我在搜索中找不到任何相关示例,我不知道我应该使用 CLLocation 还是 MKLocalSearch。

根据文档,我应该使用 MKLocalSearch:

Although local search and geocoding are similar, they support different use cases. Use geocoding when you want to convert between map coordinates and a structured address, such as an Address Book address. Use local search when you want to find a set of locations that match the user’s input.

但是我已经尝试了这两种方法,它只给了我 1 个结果(即使返回了 NSArray。

这是 CLGeocoder 方法:

CLGeocoder *geocoding = [[CLGeocoder alloc] init];
[geocoding geocodeAddressString:theTextField.text completionHandler:^(NSArray *placemarks, NSError *error) {
    if (error) {
        NSLog(@"%@", error);
    } else {
        NSLog(@"%i", [placemarks count]);
        for(CLPlacemark *myStr in placemarks) {
            NSLog(@"%@", myStr);
    }
    }
}];

这是我的 MKLocalSearch 尝试:

MKLocalSearchRequest *request = [[MKLocalSearchRequest alloc] init];
request.naturalLanguageQuery = theTextField.text;
request.region = self.region;

localSearch = [[MKLocalSearch alloc] initWithRequest:request];

[localSearch startWithCompletionHandler:^(MKLocalSearchResponse *response, NSError *error){

    if (error != nil) {
        [[[UIAlertView alloc] initWithTitle:NSLocalizedString(@"Map Error",nil)
                                    message:[error localizedDescription]
                                   delegate:nil
                          cancelButtonTitle:NSLocalizedString(@"OK",nil) otherButtonTitles:nil] show];
        return;
    }

    if ([response.mapItems count] == 0) {
        [[[UIAlertView alloc] initWithTitle:NSLocalizedString(@"No Results",nil)
                                    message:nil
                                   delegate:nil
                          cancelButtonTitle:NSLocalizedString(@"OK",nil) otherButtonTitles:nil] show];
        return;
    }
    self.streets = response;
    [self.streetsTableView reloadData];
}];

在某些情况下,MKLocalSearch 似乎返回超过 1 个响应,但这些与地点相关而不是街道名称搜索。

提前致谢。

最佳答案

这是我能得到的最接近的。这涉及使用 google places API Web Service .

注意:您可能会使用他们的 Google Maps API 等。我相信还有其他方法可以从各种 Google API 获取此信息。

 NSURL *googlePlacesURL = [NSURL URLWithString:[NSString stringWithFormat:@"https://maps.googleapis.com/maps/api/place/autocomplete/json?input=%@&location=%f,%f&sensor=true&key=API_KEY", formattedSearchText, location.coordinate.latitude,
                                                   location.coordinate.longitude]];

响应是一个 JSON 对象。将其转换为字典。

 NSDictionary *response = [NSJSONSerialization JSONObjectWithData:_googlePlacesResponse
                                                                    options:NSJSONReadingMutableContainers error:&error];

if([[response objectForKey:@"status"] isEqualToString:@"OK"])
{
    NSArray *predictions = [response objectForKey:@"predictions"];
    for(NSDictionary *prediction in predictions)
    {
        NSArray *addressTypes = [prediction objectForKey:@"types"];
        if([addressTypes containsObject:@"route"])
        {
            //This search result contains a street name. 
            //Now get the street name.
            NSArray *terms = [prediction objectForKey:@"terms"];
            NSDictionary *streetNameKeyValuePair = [terms objectAtIndex:0];
            NSLog(@"%@",[streetNameKeyValuePair objectForKey@"value"]);
        }
    }
}

可能的类型似乎是

  • 路线->街道名称
  • 地区 -> 城市/地点名称
  • 政治 -> 国家等
  • 地理编码 -> 可用的纬度/经度
  • 您可以使用那些预测填充 TableView ,这些预测包含route作为地址类型。这可行。

    关于Objective-C - 根据用户输入的查询搜索街道,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30461880/

    相关文章:

    ios - 来自 google api 的所有国家和美国各州

    objective-c - 如何在扩展中定义的 Objective-C 中使用静态方法

    iphone - 不必要的失真:核心图像滤镜正在旋转我的图像

    ios - UIToolbarButton 转至 UITabBarController 中的特定 View

    ios - 函数类型作为参数类型,如何更改此函数中的 UI 元素值?

    iphone - 我可以获得 iOS 中 GPS 位置的准确性吗?

    ios - 等待进入 Controller ,直到 wifi 启用 IOS

    ios - 桥接头文件找不到导入的 Objective-C 项目

    iphone - CLLocationCoordinate2D 不知道数组中有多少?

    ios - 当 iOS 设备靠近某个 map 点时运行操作