IOS SDK如何在百度地图中实现地图地点自动补全

How to Implement Map Places autocomplete in Baidu Map for IOS SDK

第一次使用百度API。我在我的项目中实现百度放置自动完成 API 时遇到问题。我用的是百度开发者link到http://lbsyun.baidu.com/index.php?title=iossdk.

谁能给我一些这方面的教程?

我正在学习本教程。 link

但在本教程中我无法接收 json 文件,给我一个错误

{ "Status": 102, "message": "MCODE parameter is not present, mobile type mcode required parameter"}

看来你应该使用 BaiduMapKit.Try 这个的 POI 搜索模块。

BMKCitySearchOption *citySearchOption = [[BMKCitySearchOption alloc]init];
citySearchOption.pageIndex = curPage;//here is the page index , you can set it to 0
citySearchOption.pageCapacity = 10;
citySearchOption.city= @"上海";//here is the city where you want to search the road 
citySearchOption.keyword = @"淮海路";//here is the road name or someplace name you want to search
BOOL flag = [_poisearch poiSearchInCity:citySearchOption];
if(flag) {
   _nextPageButton.enabled = true;
   NSLog(@"success");
}
else {
   _nextPageButton.enabled = false;
   NSLog(@"fail");
}

使用百度网在百度地图中实现自动完成API

- (void)viewDidLoad {
    BaseString = @"http://api.map.baidu.com/place/v2/suggestion?query=";
    ak = @"56dIEtBAp1CU7u8ZMcq8DyUH2mVsn38x";    mcode = @"com.baidu.Baidu-Map-Demo";
    regionkey = @"中国";
    PathString = @"http://api.map.baidu.com/direction/v2/transit?origin=";
    self .mapView .userTrackingMode  = BMKUserTrackingModeFollow;
    // 2. Set the map type    self.mapView.mapType = BMKMapTypeStandard;
    // 3. Set Agent    self.mapView.delegate = self;
    [super viewDidLoad];
    mapView.frame = CGRectMake(0,0,self.view.frame.size.width,self.view.frame.size.height);
    mapView.delegate = self;    anotation = [[BMKPointAnnotation alloc]init];
    destination = [[BMKPointAnnotation alloc]init];
    PathUrl = [[NSURL alloc]init];
    finalPathArray = [[NSMutableArray alloc]init];
    session = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
    downloadURL = [[NSURL alloc]init];
    path = [[BMKPolyline alloc]init];
    flag = 0;
}

-(void)GetSuggestion: (NSString *)query {
    NSString *stringUrl = [NSString stringWithFormat:@"%@%@&page_size=10&page_num=0&scope=1&region=%@&output=json&ak=%@&mcode=%@",BaseString,query,regionkey,ak,mcode];    stringUrl = [stringUrl stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLFragmentAllowedCharacterSet]];
    downloadURL = [NSURL URLWithString:stringUrl];
    if (downloadURL != nil) {
        if (DownloadTask != nil) {
            [DownloadTask suspend];
        }
        DownloadTask = [session dataTaskWithURL:downloadURL completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
            NSDictionary *AutocompleteData = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil];
            resultArray = AutocompleteData[@"result"];
            tbl_result.hidden = NO;
            [tbl_result reloadData];
        }];
        [DownloadTask resume];
    }
}

MCODE 参数意味着您的包 ID 必须将包 ID 与 url 分开例如写 url 用于自动完成 FOR 自动完成使用此功能