博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
ios 微信发送位置
阅读量:5099 次
发布时间:2019-06-13

本文共 8272 字,大约阅读时间需要 27 分钟。

 

@interface GroupReportViewController () <BMKMapViewDelegate,BMKLocationServiceDelegate,BMKGeoCodeSearchDelegate,UITableViewDataSource,UITableViewDelegate>

{

    BOOL isFirstLocation;

}

 

@property(nonatomic,strong)BMKMapView* mapView;

@property(nonatomic,strong)BMKLocationService* locService;

@property(nonatomic,strong)BMKGeoCodeSearch* geocodesearch;

 

@property(nonatomic,strong)UITableView *tableView;

@property(nonatomic,strong)NSMutableArray *dataSource;

 

@property(nonatomic,assign)CLLocationCoordinate2D currentCoordinate;

@property(nonatomic,assign)NSInteger currentSelectLocationIndex;

@property(nonatomic,strong)UIImageView *centerCallOutImageView;

@property(nonatomic,strong)UIButton *currentLocationBtn;

 

@end

 

@implementation GroupReportViewController

 

- (void)viewDidLoad

{

    [super viewDidLoad];

    [self.view setBackgroundColor:[UIColor whiteColor]];

    [self setTitle:@"定位demo"];

    

    

    [self configUI];

    

    [self startLocation];

    

}

 

-(void)configUI

{

    self.mapView = [[BMKMapView alloc]initWithFrame:CGRectMake(0, 0, kSCREENWIDTH, 300)];

    self.mapView.delegate = self;

    [self.view addSubview:self.mapView];

    

    

    self.centerCallOutImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 30, 30)];

    self.centerCallOutImageView.center = self.mapView.center;

    self.centerCallOutImageView.backgroundColor = [UIColor redColor];

    

    [self.view addSubview:self.centerCallOutImageView];

    [self.view bringSubviewToFront:self.centerCallOutImageView];

   

    

    [self.mapView layoutIfNeeded];

    

    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 300, kSCREENWIDTH, 200)];

    self.tableView.delegate = self;

    self.tableView.dataSource = self;

    [self.view addSubview:self.tableView];

    [self.tableView registerClass:[LocationCell class] forCellReuseIdentifier:@"LocationCell"];

    

    self.currentLocationBtn =[UIButton buttonWithType:UIButtonTypeCustom];

    

    [self.currentLocationBtn setImage:[UIImage imageNamed:@"location_back_icon"] forState:UIControlStateNormal];

    [self.currentLocationBtn setImage:[UIImage imageNamed:@"location_blue_icon"] forState:UIControlStateSelected];

    [self.currentLocationBtn addTarget:self action:@selector(startLocation) forControlEvents:UIControlEventTouchUpInside];

    

    [self.view addSubview:self.currentLocationBtn];

    [self.view bringSubviewToFront:self.currentLocationBtn];

    

    

}

 

-(void)startLocation

{

    isFirstLocation=YES;//首次定位

    self.currentSelectLocationIndex=0;

    self.currentLocationBtn.selected=YES;

    [self.locService startUserLocationService];

    self.mapView.showsUserLocation = NO;//先关闭显示的定位图层

    self.mapView.userTrackingMode = BMKUserTrackingModeFollow;//设置定位的状态

    self.mapView.showsUserLocation = YES;//显示定位图层

}

 

-(void)startGeocodesearchWithCoordinate:(CLLocationCoordinate2D)coordinate

{

    BMKReverseGeoCodeOption *reverseGeocodeSearchOption = [[BMKReverseGeoCodeOption alloc]init];

    reverseGeocodeSearchOption.reverseGeoPoint = coordinate;

    BOOL flag = [_geocodesearch reverseGeoCode:reverseGeocodeSearchOption];

    if(flag)

    {

        NSLog(@"geo检索发送成功");

    }

    else

    {

        NSLog(@"geo检索发送失败");

    }

}

 

-(void)setCurrentCoordinate:(CLLocationCoordinate2D)currentCoordinate

{

    _currentCoordinate=currentCoordinate;

    [self startGeocodesearchWithCoordinate:currentCoordinate];

}

 

-(void)viewWillAppear:(BOOL)animated

{

    [self.mapView viewWillAppear];

    self.mapView.delegate = self; // 此处记得不用的时候需要置nil,否则影响内存的释放

    self.locService.delegate = self;

    self.geocodesearch.delegate = self;

}

 

-(void)viewWillDisappear:(BOOL)animated

{

    [self.mapView viewWillDisappear];

    self.mapView.delegate = nil; // 不用时,置nil

    self.locService.delegate = nil;

    self.geocodesearch.delegate = nil;

}

 

- (void)didReceiveMemoryWarning

{

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}

 

- (void)dealloc

{

    if (_mapView)

    {

        _mapView = nil;

    }

    if (_geocodesearch)

    {

        _geocodesearch = nil;

    }

    if (_locService)

    {

        _locService=nil;

    }

}

 

#pragma mark - BMKMapViewDelegate

 

/**

 *在地图View将要启动定位时,会调用此函数

 *@param mapView 地图View

 */

- (void)willStartLocatingUser

{

    NSLog(@"start locate");

}

 

/**

 *用户方向更新后,会调用此函数

 *@param userLocation 新的用户位置

 */

- (void)didUpdateUserHeading:(BMKUserLocation *)userLocation

{

    [self.mapView updateLocationData:userLocation];

    //    NSLog(@"heading is %@",userLocation.heading);

}

 

/**

 *用户位置更新后,会调用此函数

 *@param userLocation 新的用户位置

 */

- (void)didUpdateBMKUserLocation:(BMKUserLocation *)userLocation

{

    isFirstLocation=NO;

    self.currentLocationBtn.selected=NO;

    [self.mapView  updateLocationData:userLocation];

    self.currentCoordinate=userLocation.location.coordinate;

    

    if (self.currentCoordinate.latitude!=0)

    {

        [self.locService stopUserLocationService];

    }

}

 

/**

 *在地图View停止定位后,会调用此函数

 *@param mapView 地图View

 */

- (void)didStopLocatingUser

{

    NSLog(@"stop locate");

}

 

/**

 *定位失败后,会调用此函数

 *@param mapView 地图View

 *@param error 错误号,参考CLError.h中定义的错误号

 */

- (void)didFailToLocateUserWithError:(NSError *)error

{

    NSLog(@"location error");

    

}

 

- (void)mapView:(BMKMapView *)mapView onClickedMapBlank:(CLLocationCoordinate2D)coordinate

{

    NSLog(@"map view: click blank");

}

- (void)mapView:(BMKMapView *)mapView regionDidChangeAnimated:(BOOL)animated

{

    if (!isFirstLocation)

    {

        CLLocationCoordinate2D tt =[mapView convertPoint:self.centerCallOutImageView.center toCoordinateFromView:self.centerCallOutImageView];

        self.currentCoordinate=tt;

    }

    

}

#pragma mark - BMKGeoCodeSearchDelegate

 

/**

 *返回地址信息搜索结果

 *@param searcher 搜索对象

 *@param result 搜索结BMKGeoCodeSearch

 *@param error 错误号,@see BMKSearchErrorCode

 */

- (void)onGetGeoCodeResult:(BMKGeoCodeSearch *)searcher result:(BMKGeoCodeResult *)result errorCode:(BMKSearchErrorCode)error

{

    NSLog(@"返回地址信息搜索结果,失败-------------");

}

 

/**

 *返回反地理编码搜索结果

 *@param searcher 搜索对象

 *@param result 搜索结果

 *@param error 错误号,@see BMKSearchErrorCode

 */

 

- (void)onGetReverseGeoCodeResult:(BMKGeoCodeSearch *)searcher result:(BMKReverseGeoCodeResult *)result errorCode:(BMKSearchErrorCode)error

{

    if (error == BMK_SEARCH_NO_ERROR)

    {

        [self.dataSource removeAllObjects];

        [self.dataSource addObjectsFromArray:result.poiList];

        

        if (isFirstLocation)

        {

            //把当前定位信息自定义组装 放进数组首位

            BMKPoiInfo *first =[[BMKPoiInfo alloc]init];

            first.address=result.address;

            first.name=@"[当前位置]";

            first.pt=result.location;

            first.city=result.addressDetail.city;

            [self.dataSource insertObject:first atIndex:0];

        }

        

        [self.tableView reloadData];

        

    }

}

 

#pragma mark - TableViewDelegate

 

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

{

    return self.dataSource.count;

}

 

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

{

    LocationCell*cell =[tableView dequeueReusableCellWithIdentifier:@"LocationCell" ];

    

    BMKPoiInfo *model=[self.dataSource objectAtIndex:indexPath.row];

    cell.textLabel.text=model.name;

    cell.detailTextLabel.text=model.address;

    cell.detailTextLabel.textColor=[UIColor grayColor];

    

    if (self.currentSelectLocationIndex==indexPath.row)

        cell.accessoryType=UITableViewCellAccessoryCheckmark;

        else

            cell.accessoryType=UITableViewCellAccessoryNone;

            

            

            return cell;

    

}

 

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

{

    BMKPoiInfo *model=[self.dataSource objectAtIndex:indexPath.row];

    BMKMapStatus *mapStatus =[self.mapView getMapStatus];

    mapStatus.targetGeoPt=model.pt;

    [self.mapView setMapStatus:mapStatus withAnimation:YES];

    self.currentSelectLocationIndex=indexPath.row;

    [self.tableView reloadData];

}

 

#pragma mark - InitMethod

 

-(BMKMapView*)mapView

{

    if (_mapView==nil)

    {

        _mapView =[BMKMapView new];

        _mapView.zoomEnabled=NO;

        _mapView.zoomEnabledWithTap=NO;

        _mapView.zoomLevel=17;

    }

    return _mapView;

}

 

-(BMKLocationService*)locService

{

    if (_locService==nil)

    {

        _locService = [[BMKLocationService alloc]init];

    }

    return _locService;

}

-(BMKGeoCodeSearch*)geocodesearch

{

    if (_geocodesearch==nil)

    {

        _geocodesearch=[[BMKGeoCodeSearch alloc]init];

    }

    return _geocodesearch;

}

 

-(UITableView*)tableView

{

    if (_tableView==nil)

    {

        _tableView=[UITableView new];

        _tableView.delegate=self;

        _tableView.dataSource=self;

        

    }

    return _tableView;

}

 

-(UIImageView*)centerCallOutImageView

{

    if (_centerCallOutImageView==nil)

    {

        _centerCallOutImageView=[UIImageView new];

        [_centerCallOutImageView setImage:[UIImage imageNamed:@"location_green_icon"]];

    }

    return _centerCallOutImageView;

}

 

-(NSMutableArray*)dataSource

{

    if (_dataSource==nil) {

        _dataSource=[[NSMutableArray alloc]init];

    }

    

    return _dataSource;

}

 

@end

转载于:https://www.cnblogs.com/sgdkg/p/4798767.html

你可能感兴趣的文章
C# 类(10) 抽象类.
查看>>
1.linux ping:unknown host www.***.***
查看>>
无向图求桥 UVA 796
查看>>
Nginx+Keepalived 实现双击热备及负载均衡
查看>>
五分钟搭建WordPress博客(二)
查看>>
Vue_(组件通讯)子组件向父组件传值
查看>>
jvm参数
查看>>
Something-Summary
查看>>
Spring学习笔记
查看>>
6个有用的MySQL语句
查看>>
linux c/c++ IP字符串转换成可比较大小的数字
查看>>
我对前端MVC的理解
查看>>
sql: table,view,function, procedure created MS_Description in sql server
查看>>
[网络流24题] 最长k可重区间集问题 (费用流)
查看>>
路径依赖理论
查看>>
ActiveX多线程回调JavaScript
查看>>
剑指offer系列32-----对称二叉树的判断
查看>>
Silverlight实用窍门系列:19.Silverlight调用webservice上传多个文件【附带源码实例】...
查看>>
2016.3.31考试心得
查看>>
Python 编程快速上手 第 7章 模式匹配与正则表达式
查看>>