How to get same search hints as apple maps in my application

半城伤御伤魂 提交于 2019-12-10 14:56:24

问题


I implemented search bar in iOS application. I want to get the same (partial result) search hints as Apple Maps application. I tried to find out how apple implemented it but I was not successful on google or here on stackoverflow.

It doesn't matter what is displayed exactly in my UITableView *searchHintTableView. I just don't get correct number of search hints. I am using my application and Apple Maps application on same device and on the same 3G wifi network(3G SIM router).

What everything I tried:

  • I tried to specify region with radius.
  • To cancel geocode from previous request.

in method:

- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText;

i have following piece of code:

    ...
    CLGeocoder *geocoder = [[CLGeocoder alloc] init];
    NSString *location = [NSString stringWithFormat:@"%@", searchText];


//    CLRegion *region = [[CLRegion alloc] initCircularRegionWithCenter:coord
//                                                               radius:100000000 identifier:@"Hint Region"];

//    [geocoder cancelGeocode];
    [geocoder geocodeAddressString:location
                 completionHandler:^(NSArray *placemarks, NSError *error) {
                     if (error != nil) {
                         DDLogCVerbose(@"Error %@", [error description]);
                         [searchHintArray removeAllObjects];
                         [searchHintTableView reloadData];
                         return;
                     }
                     DDLogCVerbose(@"CLGeocoder >> %@", [placemarks description]);
                     //to get rid of unwanted results when searchText is inadequate
                     //even when previous partial results are not removed I don't get that many results as Apple Maps application.
                     [searchHintArray removeAllObjects]; 
                     [placemarks enumerateObjectsUsingBlock:^(CLPlacemark *placemark, NSUInteger idx, BOOL *stop) {
                         MKPlacemark *mkPlacemark = [[MKPlacemark alloc] initWithPlacemark:placemark];
                         [searchHintArray addObject:mkPlacemark];
                     }];
                     [searchHintTableView reloadData];
                 }
    ];
...

input parameters are following

  • searchText: Ess
  • latitude: 51.46
  • longitude: 7.01

Instead of 10 results(address related) from Apple Maps I get 2 at most(NSArray *placemarks).

With complete name of city "Essen" this code returns just 1 result. Apple maps 10 again.

Am I using the right API to display partial results as search hints? If not then how to do it correctly or which API should I use?

EDIT: As I was requested to explain why this is not duplicate I am editing this post. My Question was how to achieve same results as Apple Maps iOS app. I get accurate location when I enter bigger part of address or full address. I just don't get the same number of results when I enter small parts of address.

来源:https://stackoverflow.com/questions/20101240/how-to-get-same-search-hints-as-apple-maps-in-my-application

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!