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

Objective-C – 根据用户输入的查询搜索街道,第1张

概述我想允许用户搜索街道名称并将结果显示在UITableView中. 目前该地区并不重要,它可以来自任何地区. 我在搜索中找不到任何相关示例,我不知道是否应该使用CLLocation或MKLocalSearch. 根据文档,我应该使用MKLocalSearch: Although local search and geocoding are similar, they support differen 我想允许用户搜索街道名称并将结果显示在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);    }    }}];@H_404_31@  

这是我的MKLocalSearch尝试:

MKLocalSearchRequest *request = [[MKLocalSearchRequest alloc] init];request.naturalLanguage@R_403_5962@ = 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];}];@H_404_31@  

在某些情况下,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]];@H_404_31@  

响应是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"]);        }    }}@H_404_31@  

可能的类型似乎是

>路线 – >街道名称
>地点 – >城市/位置名称
>政治 – >国家等
>地理编码 – >纬度/长度可用

您可以使用仅包含路由作为地址类型的预测来填充表视图.这可行.

总结

以上是内存溢出为你收集整理的Objective-C – 根据用户输入的查询搜索街道全部内容,希望文章能够帮你解决Objective-C – 根据用户输入的查询搜索街道所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1060439.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-25
下一篇 2022-05-25

发表评论

登录后才能评论

评论列表(0条)

保存