大佬教程收集整理的这篇文章主要介绍了Objective-C – 根据用户输入的查询搜索街道,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我在搜索中找不到任何相关示例,我不知道是否应该使用CLLOCATIOn或MKLocalSearch.
根据文档,@R_899_10675@用MKLocalSearch:
但我尝试了两种方法,它只给我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尝试:
@H_102_31@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个响应,但这些与非街道名称搜索的地方有关.
提前致谢.
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"]); } } }
可能的类型似乎是
>路线 – >街道名称
>地点 – >城市/位置名称
>政治 – >国家等
>地理编码 – >纬度/长度可用
您可以使用仅包含路由作为地址类型的预测来填充表视图.这可行.
以上是大佬教程为你收集整理的Objective-C – 根据用户输入的查询搜索街道全部内容,希望文章能够帮你解决Objective-C – 根据用户输入的查询搜索街道所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。