zoukankan      html  css  js  c++  java
  • ios 百度地图 定位

    首先去下载百度地图的SDK

    这是我的百度地图版本和需要导入的库文件

    接下来是一些设置:

    app.h

    #import <UIKit/UIKit.h>
    #import <CoreLocation/CoreLocation.h>
    #import "BMapKit.h"
    
    @interface AppDelegate : UIResponder <UIApplicationDelegate>
    {
        UINavigationController *navigationController;
        BMKMapManager *_mapManager;
    }
    @property (strong, nonatomic) UIWindow *window;
    
    
    @end
    

      

    app.m

    #import "AppDelegate.h"
    
    @interface AppDelegate ()
    
    @end
    
    @implementation AppDelegate
    
    
    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
        
        _mapManager = [[BMKMapManager alloc]init];
        
        // 如果要关注网络及授权验证事件,请设定     generalDelegate参数
        
        BOOL ret = [_mapManager start:@"jmvLCjElajXyuNmKShKzZUva"  generalDelegate:nil];
        if (!ret) {
            NSLog(@"manager start failed!");
        }
        [self.window addSubview:navigationController.view];
        [self.window makeKeyAndVisible];
        // Override point for customization after application launch.
        return YES;
    }
    
    - (void)applicationWillResignActive:(UIApplication *)application {
        // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
        // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
        [BMKMapView willBackGround];
    }
    
    - (void)applicationDidEnterBackground:(UIApplication *)application {
        // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
        // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
    }
    
    - (void)applicationWillEnterForeground:(UIApplication *)application {
        // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
        
    }
    
    - (void)applicationDidBecomeActive:(UIApplication *)application {
        // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
        [BMKMapView didForeGround];
    }
    
    - (void)applicationWillTerminate:(UIApplication *)application {
        // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
    }
    
    
    @end
    

      viewController.h

    #import <UIKit/UIKit.h>
    #import "BMapKit.h"
    
    @interface ViewController : UIViewController<BMKMapViewDelegate,BMKLocationServiceDelegate>
    {
        BMKMapView* _mapView;
        UIButton* startBtn;
        UIButton* stopBtn;
        UIButton* followingBtn;
        UIButton* followHeadBtn;
        BMKLocationService* _locService;
    }
    
    
    @end
    

      

    viewController.m

    #import "ViewController.h"
    #import "BMapKit.h"
    #import <CoreLocation/CoreLocation.h>
    
    @interface ViewController ()
    
    @end
    
    @implementation ViewController
    - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
    {
        self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
        if (self) {
            // Custom initialization
        }
        return self;
    }
    
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        _mapView = [[BMKMapView alloc]initWithFrame:CGRectMake(0, 0, 320, 480)];
        self.view = _mapView;
        
        if( ([[[UIDevice currentDevice] systemVersion] doubleValue]>=7.0))
        {
            self.navigationController.navigationBar.translucent = NO;
        }
        _locService = [[BMKLocationService alloc]init];
        [followHeadBtn setEnabled:NO];
        [followingBtn setAlpha:0.6];
        [followingBtn setEnabled:NO];
        [followHeadBtn setAlpha:0.6];
        [stopBtn setEnabled:NO];
        [stopBtn setAlpha:0.6];
        
        [self startLocation];
        // Do any additional setup after loading the view, typically from a nib.
    }
    -(void)viewWillAppear:(BOOL)animated {
        [_mapView viewWillAppear];
        _mapView.delegate = self; // 此处记得不用的时候需要置nil,否则影响内存的释放
        _locService.delegate = self;
    }
    
    -(void)viewWillDisappear:(BOOL)animated {
        [_mapView viewWillDisappear];
        _mapView.delegate = nil; // 不用时,置nil
        _locService.delegate = nil;
    }
    -(void)startLocation
    {
        NSLog(@"进入普通定位态");
        [_locService startUserLocationService];
        _mapView.showsUserLocation = NO;//先关闭显示的定位图层
        _mapView.userTrackingMode = BMKUserTrackingModeNone;//设置定位的状态
        _mapView.showsUserLocation = YES;//显示定位图层
        [startBtn setEnabled:NO];
        [startBtn setAlpha:0.6];
        [stopBtn setEnabled:YES];
        [stopBtn setAlpha:1.0];
        [followHeadBtn setEnabled:YES];
        [followHeadBtn setAlpha:1.0];
        [followingBtn setEnabled:YES];
        [followingBtn setAlpha:1.0];
        [self startFollowing];
    }
    -(void)startFollowing
    {
        NSLog(@"进入跟随态");
        
        _mapView.showsUserLocation = NO;
        _mapView.userTrackingMode = BMKUserTrackingModeFollow;
        _mapView.showsUserLocation = YES;
        self.view = _mapView;
        
    }
    - (void)didUpdateUserHeading:(BMKUserLocation *)userLocation
    {
        [_mapView updateLocationData:userLocation];
        NSLog(@"heading is %@",userLocation.heading);
    }
    - (void)didUpdateBMKUserLocation:(BMKUserLocation *)userLocation
    {
        //    NSLog(@"didUpdateUserLocation lat %f,long %f",userLocation.location.coordinate.latitude,userLocation.location.coordinate.longitude);
        [_mapView updateLocationData:userLocation];
    }
    - (void)didFailToLocateUserWithError:(NSError *)error
    {
        NSLog(@"location error");
    }
    
    
    - (void)dealloc {
        if (_mapView) {
            _mapView = nil;
        }
        [super dealloc];
    }
    
    - (void)didReceiveMemoryWarning {
        [super didReceiveMemoryWarning];
        // Dispose of any resources that can be recreated.
    }
    

      

  • 相关阅读:
    循环语句
    JAVA-数据类型、变量、常量
    JAVA——程序流程控制——循环语句——for循环(打印三角形)
    循环语句
    JAVA基础学习1
    新手上路
    mysql内置函数之事务
    mysql内置功能
    mysql内置函数之视图
    pymysql模块
  • 原文地址:https://www.cnblogs.com/JJFly/p/4297795.html
Copyright © 2011-2022 走看看