• iOS 百度地图获取当前地理位置


    //
    //  ViewController.m
    //  BaiDuDemo
    //
    //  Created by Chocolate. on 15-3-2.
    //  Copyright (c) 2015年 redasen. All rights reserved.
    //
    
    #import "ViewController.h"
    #import "BMapKit.h"
    
    @interface ViewController () <BMKGeoCodeSearchDelegate,BMKMapViewDelegate, BMKLocationServiceDelegate>
    @property (strong, nonatomic) BMKMapView *mapView;
    @property (strong, nonatomic) BMKGeoCodeSearch *search;
    @property (strong, nonatomic) BMKLocationService *locService;
    @end
    
    @implementation ViewController
    {
        BMKUserLocation *myLocation;
    }
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        // Do any additional setup after loading the view, typically from a nib.
        _mapView = [[BMKMapView alloc] initWithFrame:self.view.bounds];
        [self.view addSubview:_mapView];
        
        _search = [[BMKGeoCodeSearch alloc]init];
        
        //初始化BMKLocationService
        _locService = [[BMKLocationService alloc]init];
        _locService.delegate = self;
        //启动LocationService
        [_locService startUserLocationService];
    }
    
    -(void)viewWillAppear:(BOOL)animated
    {
        [_mapView viewWillAppear];
        _mapView.delegate = self;
         _search.delegate = self;
    }
    
    -(void)viewDidAppear:(BOOL)animated
    {
        [_mapView setShowsUserLocation:YES];
    }
    
    -(void)viewWillDisappear:(BOOL)animated
    {
        [_mapView setShowsUserLocation:NO];
        _mapView.delegate = nil;
        _search.delegate = nil;
    }
    
    - (void)didReceiveMemoryWarning {
        [super didReceiveMemoryWarning];
        // Dispose of any resources that can be recreated.
    }
    
    #pragma mark - BMKLocationServiceDelegate
    /**
     *在将要启动定位时,会调用此函数
     */
    - (void)willStartLocatingUser
    {
        
    }
    
    /**
     *在停止定位后,会调用此函数
     */
    - (void)didStopLocatingUser
    {
        CLLocationCoordinate2D pt = (CLLocationCoordinate2D){0, 0};
        
        pt = (CLLocationCoordinate2D){myLocation.location.coordinate.latitude, myLocation.location.coordinate.longitude};
        
        BMKReverseGeoCodeOption *option = [[BMKReverseGeoCodeOption alloc]init];
        option.reverseGeoPoint = pt;
        BOOL result = [_search reverseGeoCode:option];
        
        if(result)
        {
            NSLog(@"反geo检索发送成功");
        }
        else
        {
            NSLog(@"反geo检索发送失败");
        }
    }
    
    /**
     *用户方向更新后,会调用此函数
     *@param userLocation 新的用户位置
     */
    - (void)didUpdateUserHeading:(BMKUserLocation *)userLocation
    {
        NSLog(@"heading is %@",userLocation.heading);
    }
    
    /**
     *用户位置更新后,会调用此函数
     *@param userLocation 新的用户位置
     */
    - (void)didUpdateBMKUserLocation:(BMKUserLocation *)userLocation
    {
        if (userLocation != nil) {
            
            NSLog(@"get location success");
            myLocation = userLocation;
            _mapView.showsUserLocation = NO;
            [_locService stopUserLocationService];
        }
        
        NSLog(@"didUpdateUserLocation lat %f,long %f",userLocation.location.coordinate.latitude,userLocation.location.coordinate.longitude);
    }
    
    /**
     *定位失败后,会调用此函数
     *@param error 错误号
     */
    - (void)didFailToLocateUserWithError:(NSError *)error
    {
        
    }
    
    #pragma mark - BMKGeoCodeSearchDelegate
    
    /**
     *返回地址信息搜索结果
     *@param searcher 搜索对象
     *@param result 搜索结BMKGeoCodeSearch果
     *@param error 错误号,@see BMKSearchErrorCode
     */
    - (void)onGetGeoCodeResult:(BMKGeoCodeSearch *)searcher result:(BMKGeoCodeResult *)result errorCode:(BMKSearchErrorCode)error
    {
        
    }
    
    /**
     *返回反地理编码搜索结果
     *@param searcher 搜索对象
     *@param result 搜索结果
     *@param error 错误号,@see BMKSearchErrorCode
     */
    - (void)onGetReverseGeoCodeResult:(BMKGeoCodeSearch *)searcher result:(BMKReverseGeoCodeResult *)result errorCode:(BMKSearchErrorCode)error
    {
        UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"" message:result.address delegate:self cancelButtonTitle:@"关闭" otherButtonTitles:nil, nil];
        [alert show];
    }
    
    @end
  • 相关阅读:
    linux 清空文件内容命令
    优秀的java 社区
    vue强制刷新组件 ----组件重置到初始状态
    function的json对象转换字符串与字符串转换为对象的方法
    js实现深度优先遍历和广度优先遍历
    Egg.js中使用sequelize事务
    JavaScript ES6 数组新方法 学习随笔
    eggjs的参数校验模块egg-validate的使用和进一步定制化升级
    Node.js 服务端图片处理利器
    webp图片实践之路
  • 原文地址:https://www.cnblogs.com/lihaibo-Leao/p/4308865.html
Copyright © 2020-2023  润新知