首页 > 代码库 > 总结一下百度地图 定位 大头针 和划线和城市检索的功能

总结一下百度地图 定位 大头针 和划线和城市检索的功能

- (void)viewDidLoad {
    [super viewDidLoad];
   
   
    //初始化BMKLocationService
    _locService = [[BMKLocationService alloc]init];
    _locService.delegate = self;
    //启动LocationService
    [_locService startUserLocationService];
}
 
在delegate中更新位置和对应方向
#pragma mark--BMKLocationServiceDelegate 定位当前位置
- (void)didUpdateUserHeading:(BMKUserLocation *)userLocation
{
    NSLog(@"heading is %@",userLocation.heading);
}
//处理位置坐标更新
- (void)didUpdateBMKUserLocation:(BMKUserLocation *)userLocation
{
    NSLog(@"didUpdateUserLocation lat %f,long %f",userLocation.location.coordinate.latitude,userLocation.location.coordinate.longitude);
    _handleView.mapView.showsUserLocation = YES;//显示定位图层
    [_handleView.mapView updateLocationData:userLocation];
   
    CGFloat longitude = userLocation.location.coordinate.longitude;
    CGFloat latitude = userLocation.location.coordinate.latitude;
   
    BMKPointAnnotation* item = [[BMKPointAnnotation alloc]init];
    item.coordinate = CLLocationCoordinate2DMake(latitude, longitude);
   
    [self.handleView.mapView removeAnnotations:self.handleView.mapView.annotations];
   
    [PublicTools doItInAsynMainThread:^{
       
        [self.handleView.mapView addAnnotation:item];
       
        [self.handleView.mapView setCenterCoordinate:CLLocationCoordinate2DMake(latitude, longitude) animated:YES];
        [_locService stopUserLocationService];
    }];

   
}
 
添加大头针
- (void)bmk {
   
    CGFloat longitude = [self.handleView.model.longitude floatValue];
    CGFloat latitude = [self.handleView.model.latitude floatValue];
   
    BMKPointAnnotation* item = [[BMKPointAnnotation alloc]init];
    item.coordinate = CLLocationCoordinate2DMake(latitude, longitude);
   
    [self.handleView.mapView removeAnnotations:self.handleView.mapView.annotations];
   
    [PublicTools doItInAsynMainThread:^{
       
        [self.handleView.mapView addAnnotation:item];
       
        [self.handleView.mapView setCenterCoordinate:CLLocationCoordinate2DMake(latitude, longitude) animated:YES];
 
自定义大头针
/ 根据anntation生成对应的View
- (BMKAnnotationView *)mapView:(BMKMapView *)mapView viewForAnnotation:(id <BMKAnnotation>)annotation
{
    NSString *AnnotationViewID = [NSString stringWithFormat:@"renameMark%d",i];
   
    newAnnotation = [[BMKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:AnnotationViewID];
    // 设置颜色
    ((BMKPinAnnotationView*)newAnnotation).pinColor = BMKPinAnnotationColorPurple;
    // 从天上掉下效果
    ((BMKPinAnnotationView*)newAnnotation).animatesDrop = YES;
    // 设置可拖拽
    ((BMKPinAnnotationView*)newAnnotation).draggable = YES;
    //设置大头针图标
    ((BMKPinAnnotationView*)newAnnotation).image = [UIImage imageNamed:@"zhaohuoche"];
   
    UIView *popView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 100, 60)];
    //设置弹出气泡图片
    UIImageView *image = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"wenzi"]];
    image.frame = CGRectMake(0, 0, 100, 60);
    [popView addSubview:image];
    //自定义显示的内容
    UILabel *driverName = [[UILabel alloc]initWithFrame:CGRectMake(0, 3, 100, 20)];
    driverName.text = @"张XX师傅";
    driverName.backgroundColor = [UIColor clearColor];
    driverName.font = [UIFont systemFontOfSize:14];
    driverName.textColor = [UIColor whiteColor];
    driverName.textAlignment = NSTextAlignmentCenter;
    [popView addSubview:driverName];
   
    UILabel *carName = [[UILabel alloc]initWithFrame:CGRectMake(0, 25, 100, 20)];
    carName.text = @"京A123456";
    carName.backgroundColor = [UIColor clearColor];
    carName.font = [UIFont systemFontOfSize:14];
    carName.textColor = [UIColor whiteColor];
    carName.textAlignment = NSTextAlignmentCenter;
    [popView addSubview:carName];
    BMKActionPaopaoView *pView = [[BMKActionPaopaoView alloc]initWithCustomView:popView];
    pView.frame = CGRectMake(0, 0, 100, 60);
    ((BMKPinAnnotationView*)newAnnotation).paopaoView = nil;
    ((BMKPinAnnotationView*)newAnnotation).paopaoView = pView;
    i++;
    return newAnnotation;
   
}
 
百度地图画线
//描点画线
    if (self.lastCoor.latitude) {
    CLLocationCoordinate2D coors[2] = {0};
    coors[0].latitude = self.lastCoor.latitude;
    coors[0].longitude = self.lastCoor.longitude;
    coors[1].latitude = touchMapCoordinate.latitude;
    coors[1].longitude = touchMapCoordinate.longitude;
    BMKPolyline *polyline = [BMKPolyline polylineWithCoordinates:coors count:2];
    [self.handleView.mapView addOverlay:polyline];
   
    }
    self.lastCoor = touchMapCoordinate;
    }];
 
在delegate中画线
// 在地图上画线
- (BMKOverlayView *)mapView:(BMKMapView *)mapView viewForOverlay:(id <BMKOverlay>)overlay
   if ([overlay isKindOfClass:[BMKPolyline class]]) {
        BMKPolylineView *overlayView = [[BMKPolylineView alloc] initWithOverlay:overlay];
        overlayView.strokeColor = [[UIColor redColor] colorWithAlphaComponent:1];
        overlayView.lineWidth = 3.0;
        return overlayView;
    }
 
    return nil;
}
百度地图实现模糊搜索


//
//  MZVagueSearchController.m
//  Minshuku
//
//  Created by Nicholas on 16/11/9.
//  Copyright ? 2016年 Nicholas. All rights reserved.
//

#import "MZVagueSearchController.h"
#import "SearchModel.h"

@interface MZVagueSearchController ()<UITableViewDelegate,UITableViewDataSource,BMKPoiSearchDelegate>
{
    BMKPoiSearch *_poisearch;//poi搜索
   
   
    BMKGeoCodeSearch *_geocodesearch;//geo搜索服务
   
}

@property (nonatomic,strong) NSMutableArray *addressArray;

@property (nonatomic,strong) SearchModel *model;

@end

@implementation MZVagueSearchController

- (UITableView *)displayTableView
{
    if (!_displayTableView) {
        _displayTableView = [[UITableView alloc] initWithFrame:self.view.bounds];
        _displayTableView.dataSource = self;
        _displayTableView.delegate = self;
        [self.view addSubview:_displayTableView];
       
       
    }
    return _displayTableView;
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    //自动让输入框成为第一响应者,弹出键盘
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
    [self.inputAddTF becomeFirstResponder];
    });
   
}
-(void)viewWillDisappear:(BOOL)animated
{
 
    [super viewWillDisappear:animated];
    _poisearch.delegate = nil; // 不用时,置nil
    _geocodesearch.delegate = nil;

}


- (void)viewDidLoad {
    [super viewDidLoad];
    [self setUpNav];
   
}
- (void)setUpNav
{
    _inputAddTF = [[UITextField alloc] initWithFrame:CGRectMake(0, 10, ScreenWidth - 60, 30)];
   
   
    [self.inputAddTF addTarget:self action:@selector(inputAddTFAction:) forControlEvents:UIControlEventEditingChanged];
    self.inputAddTF.borderStyle =UITextBorderStyleBezel;
    self.inputAddTF.backgroundColor = [UIColor grayColor];
   
    self.inputAddTF.textAlignment = NSTextAlignmentLeft;
    self.inputAddTF.font = [UIFont boldSystemFontOfSize:18];
     self.inputAddTF.textColor = [UIColor colorWithWhite:20 alpha:7];
    self.navigationItem.titleView = _inputAddTF;
    self.view.backgroundColor = [UIColor whiteColor];
   
}


#pragma mark TableViewDelegate


//设置 row


-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section


{
   
   
    return self.addressArray.count;
   
   
}


//设置 tableViewCell


-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath


{
   
   
    static NSString *cell_id = @"cell_id";
   
   
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cell_id];
   
   
    if (!cell) {
       
       
        cell = [[UITableViewCell alloc] initWithStyle:3 reuseIdentifier:cell_id];
       
       
    }
   
   
    SearchModel *sModel = [[SearchModel alloc] init];
   
   
    sModel = self.addressArray[indexPath.row];
   
   
    cell.textLabel.text = sModel.name;
   
   
    cell.textLabel.font = [UIFont systemFontOfSize:15];
   
   
    cell.textLabel.textColor = [UIColor grayColor];
   
   
    cell.detailTextLabel.text = sModel.address;
   
   
    cell.detailTextLabel.textColor = [UIColor colorWithRed:157/255.0 green:157/255.0 blue:157/255.0 alpha:1];
   
   
    return cell;
   
   
}


//设置选中事件


-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath


{
   
   
    SearchModel *model = [[SearchModel alloc] init];
   
   
    model = self.addressArray[indexPath.row];
   
   
    [self.navigationController popViewControllerAnimated:YES];
   
   
    //block 传值

   
    self.block(model.address,model.pt);
   
   
}


#pragma mark ---- 设置高度


-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath


{
   
   
    return 50;
   
   
}

#pragma mark 监听输入文本信息


-(void)inputAddTFAction:(UITextField *)textField


{
   
   
    //延时搜索
   
   
    [self performSelector:@selector(delay) withObject:self afterDelay:0.5];
   
   
}

#pragma mark ----- 延时搜索


- (void)delay {
   
   
    [self nameSearch];
   
   
}

#pragma mark ---- 输入地点搜索


-(void)nameSearch


{
   
   
    _poisearch = [[BMKPoiSearch alloc]init];
   
   
    _poisearch.delegate = self;
   
   
    BMKCitySearchOption *citySearchOption = [[BMKCitySearchOption alloc]init];
   
   
    citySearchOption.pageIndex = 0;
   
   
    citySearchOption.pageCapacity = 30;
   
   
    citySearchOption.city= _locationCity;
   
   
    citySearchOption.keyword = self.inputAddTF.text;
   
   
    BOOL flag = [_poisearch poiSearchInCity:citySearchOption];
   
   
    if(flag)
       
       
    {
       
       
        NSLog(@"城市内检索发送成功");
       
       
    }
   
   
    else
       
       
    {
       
       
        NSLog(@"城市内检索发送失败");
       
       
    }
   
   
}


#pragma mark --------- poi 代理方法


-(void)onGetPoiResult:(BMKPoiSearch *)searcher result:(BMKPoiResult *)poiResult errorCode:(BMKSearchErrorCode)errorCode


{
   
   
    if(errorCode == BMK_SEARCH_NO_ERROR)
       
       
    {
       
       
        self.addressArray = [NSMutableArray array];
       
       
        [self.addressArray removeAllObjects];
       
       
        for (BMKPoiInfo *info in poiResult.poiInfoList) {
           
           
            _model = [[SearchModel alloc] init];
           
           
            _model.name = info.name;
           
           
            _model.address = info.address;
           
           
            _model.pt = info.pt;
           
           
            [self.addressArray addObject:_model];
           
           
        }
       
       
        [self.displayTableView reloadData];
       
       
    }
   
   
}

#pragma mark 滑动的时候收起键盘


- (void)scrollViewDidScroll:(UIScrollView *)scrollView


{
   
   
    [self.view endEditing:YES];
   
   
    [self.navigationItem.titleView endEditing:YES];
   
   
}

-(void)dealloc


{
   
   
    if (_poisearch != nil) {
       
       
        _poisearch = nil;
       
       
    }
   
   
    if (_geocodesearch != nil) {
       
       
        _geocodesearch = nil;
       
       
    }
   
   
}
@end
 
///描点划线
 
//描点画线
    if (self.lastCoor.latitude) {
    CLLocationCoordinate2D coors[2] = {0};
    coors[0].latitude = self.lastCoor.latitude;
    coors[0].longitude = self.lastCoor.longitude;
    coors[1].latitude = touchMapCoordinate.latitude;
    coors[1].longitude = touchMapCoordinate.longitude;
    BMKPolyline *polyline = [BMKPolyline polylineWithCoordinates:coors count:2];
    [self.handleView.mapView addOverlay:polyline];
   
    }
 
 

总结一下百度地图 定位 大头针 和划线和城市检索的功能