zoukankan      html  css  js  c++  java
  • 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
  • 相关阅读:
    mysql在第一次查询的时候很慢,第二次查询就比较快的原因?
    mysql的递归(使用函数)
    什么样的男人才是女人眼中最帅的男人
    面试题总结
    java的重载总结
    arduino读取GPIO数据
    electron+react项目改为typescript
    百度AI训练营笔记
    python读取文件出现ufeff问题
    大端小端
  • 原文地址:https://www.cnblogs.com/lihaibo-Leao/p/4308865.html
Copyright © 2011-2022 走看看