Cowboy Tech

LBS兴趣点搜索

代码设置启动界面

AppDelegate.m

_window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
ViewController * viewController = [[ViewController alloc] init];
UINavigationController * navigationController = [[UINavigationController alloc] initWithRootViewController:viewController];
[_window setRootViewController:navigationController];
[_window makeKeyAndVisible];

颜色创建图片

- (UIImage *)createImageFromColor:(UIColor *)color imageSize:(CGSize)size {

CGRect frame = CGRectMake(0, 0, size.width, size.height);
UIGraphicsBeginImageContext(size);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetFillColorWithColor(context, [color CGColor]);
CGContextFillRect(context, frame);
UIImage * image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}

UISearchBar设置背景图片

如果不设置背景图片,SearchBar会有默认的银灰色背景方框

[_searchBar setBackgroundImage:[self createImageFromColor:[UIColor orangeColor]
                                               imageSize:CGSizeMake(self.view.frame.size.width, 50)]];

//当用户第一次搜索未完成时,又执行了第二次搜索,这样就需要cancel掉

if(_localSearch.searching){
    [_localSearch cancel];
}

键盘退出

[_searchBar resignFirstResponder];

隐藏导航栏

- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
[self.navigationController setNavigationBarHidden:YES];
}

Cell 配置

- (void)viewDidLoad {
[super viewDidLoad];
[self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:tableViewKey];
}

//这样不需要进行if判断                 
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {  
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:tableViewKey forIndexPath:indexPath];
    NSDictionary *dic = _talbeViewArray[indexPath.row];
    [cell.textLabel setText:dic[@"site"]];
    return cell;
}

自定义导航视图返回按钮

- (void)viewDidLoad {
[super viewDidLoad];
[self setTitle:@"线路导航"];
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"返回"
                                                                         style:UIBarButtonItemStylePlain
                                                                        target:self
                                                                        action:@selector(returnPre)];
                                                                        }

枚举识别数个按钮

typedef NS_ENUM(NSUInteger, FunctionSelet) {
LBS_GAME = 0,
LBS_EAT,
LBS_REST,
LBSE_BUSSION,
LBS_OTHER,
LBS_OTHER1,
LBS_OTHER2,
LBS_OTHER3,
LBS_OTHER4,
};

NSUInteger dataArr[] = {LBS_GAME,LBS_EAT,LBS_REST,LBSE_BUSSION,LBS_OTHER,LBS_OTHER1,LBS_OTHER2,LBS_OTHER3,LBS_OTHER4};

for .... {
[btn setTag:dataArr[i]];
}

- (void)theBtnPressed:(id)sender { 
UIButton * btn = (UIButton *)sender;
NSString * userSelectString = nil;
switch(btn.tag){
    case LBS_GAME:
        userSelectString = @"游戏";
        NSLog(@"游戏");
        break;
    case LBS_EAT:
        userSelectString = @"吃喝";
        NSLog(@"吃喝");
        break;
    case LBS_REST:
        userSelectString = @"休息";
        NSLog(@"休息");
        break;
    case LBSE_BUSSION:
        userSelectString = @"狂街";
        NSLog(@"逛街");
        break;
    default:
        userSelectString = @"other";
        break;
}

[self gotoShowDataView:userSelectString];
}

MKLocalSearch

MKLocalSearchRequest * searchRequest = [[MKLocalSearchRequest alloc] init];
searchRequest.naturalLanguageQuery = self.searchString;
MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(CLLocationCoordinate2DMake(-33.96820, 151.11060), 2000, 2000);
searchRequest.region = region;

_localSearch = [[MKLocalSearch alloc] initWithRequest:searchRequest];
[_localSearch startWithCompletionHandler:^(MKLocalSearchResponse *response, NSError *error) {

    [loadingLabel removeFromSuperview];

    if(response.mapItems.count == 0 || error){
        NSLog(@"error:%@",error);
    }else{
        [_talbeViewArray addObjectsFromArray:response.mapItems];
        self.currentRegion = response.boundingRegion;
        [self.tableView setBounces:YES];
        [self.tableView setSeparatorStyle:UITableViewCellSeparatorStyleSingleLine];
        [self.tableView reloadData];
    }
}];

实现当前位置到兴趣点的导航路线 (略)

  1. MKMapItem属性介绍
  2. MKDirectionsRequest属性设置
  3. MKDirections使用方法
  4. MKDirectionsResponse返回数据处理
  5. MKRoute讲解