百度地圖、高德地圖的定位,搜索,模糊搜索-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);
    }
}      
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章