百度地图、高德地图的定位,搜索,模糊搜索-1

//这一段时间因为要做O2O商城,中间要用到地图的定位搜索功能,花了几天看了,其实大致的看文档的话很好解决得,主要说说者集体那遇到的麻烦吧
其中定位用到了

#import <BaiduMapAPI_Search/BMKSearchComponent.h>

开启定位

//开启定位
- (void)startLocationSuccess:(SuccessBlock)success Error:(BMKErrorBlock)error {
    
    if (success) {
        self.successBlock = success;
    }
    
    if (error) {
        self.errorBlock = error;
    }
    
    [self.locationService startUserLocationService];
    
}

//停止定位
- (void)stopLocation {
    [self.locationService stopUserLocationService];
    self.locationService.delegate = nil;
    self.locationService = nil;
}


#pragma --mark Delegate
/**
 *用户位置更新后,会调用此函数
 *@param userLocation 新的用户位置
 */
- (void)didUpdateBMKUserLocation:(BMKUserLocation *)userLocation {
    
    [self stopLocation];
//    if (self.successBlock) {
//        self.successBlock(userLocation.location.coordinate);
//    }
    [self getReverseGeocodeWithCoordinate2D:userLocation.location.coordinate];
    
}

 

//然后就用到反地理编码的

#pragma --mark 反地理编码
- (void)getReverseGeocodeWithCoordinate2D:(CLLocationCoordinate2D)coordinate {
    
    BMKReverseGeoCodeOption *pt = [[BMKReverseGeoCodeOption alloc]init];
    pt.reverseGeoPoint = coordinate;
    
    if ([self.geocodesearch reverseGeoCode:pt]) {
        DBLog(@"反geo检索发送成功");
    }else {
        DBLog(@"反geo检索发送失败");
    }
    
}


#pragma --mark 反地理编码Delegate
/**
 *返回反地理编码搜索结果
 *@param searcher 搜索对象
 *@param result 搜索结果
 *@param error 错误号,@see BMKSearchErrorCode
 */
- (void)onGetReverseGeoCodeResult:(BMKGeoCodeSearch *)searcher result:(BMKReverseGeoCodeResult *)result errorCode:(BMKSearchErrorCode)error {
    
    if (error == BMK_SEARCH_NO_ERROR) {
        if (self.successBlock) {
            self.successBlock(result);
        }
    }else  {
        if (self.errorBlock) {
            self.errorBlock(error);
        }
    }
    
}

到这里定位就结束了,

搜索用到了

#import <BaiduMapAPI_Search/BMKSearchComponent.h>

//搜索的话,这里就说一下模糊搜索,

- (BMKSuggestionSearch *)search {
    if (!_search) {
        _search = [[BMKSuggestionSearch alloc]init];
        _search.delegate = self;
    }
    return _search;
}

//模糊搜索
- (void)searchWithKeywords:(NSString *)keywords {
 
    self.op = [[BMKSuggestionSearchOption alloc]init];
    self.op.cityname = @"深圳市";
    self.op.keyword = keywords;
    if ([self.search suggestionSearch:self.op]) {
        NSLog(@"成功");
    }else{
        NSLog(@"失败");
    }
    
}
//返回 貌似会蹦,去论坛看了 好像是SDK问题 ,后来就舍弃了百度 改用高德了的
- (void)onGetSuggestionResult:(BMKSuggestionSearch*)searcher result:(BMKSuggestionResult*)result errorCode:(BMKSearchErrorCode)error {
    
    NSLog(@"%@",[NSThread currentThread]);
    
    NSLog(@"%@==\n%@==\n%@==",result.keyList,result.cityList,result.districtList);
    
    for (NSString *str in result.keyList) {
        NSLog(@"%@",str);
    }
    NSLog(@"=====================\n");
    for (NSString *str in result.cityList) {
        NSLog(@"%@",str);
    }
    NSLog(@"=====================\n");
    for (NSString *str in result.districtList) {
        NSLog(@"%@",str);
    }
}      
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章