1、加载百度地图
2、开启定位服务
3、在定位服务的代理方法(获取用户地理位置后)中,设置地图中心点为用户当前位置
4、相关说明及代码实现:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 | // // SAViewController.m // MapDemo // // Created by Sian on 14/11/13. // Copyright (c) 2014年 Sian. All rights reserved. // #import "SAViewController.h" #import "BMapKit.h" @interface SAViewController () <BMKMapViewDelegate, BMKLocationServiceDelegate> @property (nonatomic, strong) BMKMapView *mapView; @property (nonatomic, strong) BMKLocationService *location; @end @implementation SAViewController - (void)viewDidLoad { [super viewDidLoad]; // 初始化定位服务 self.mapView = [[BMKMapView alloc] initWithFrame:self.view.frame]; self.mapView.delegate = self; // 显示用户当前位置 self.mapView.showsUserLocation = YES; [self.view addSubview:self.mapView]; // 初始化定位服务 self.location = [[BMKLocationService alloc] init]; self.location.delegate = self; // 开启定位服务 [self.location startUserLocationService]; } - (void)viewWillAppear:(BOOL)animated { [super viewWillAppear:animated]; [self.mapView viewWillAppear]; } - (void)viewWillDisappear:(BOOL)animated { [super viewWillDisappear:animated]; [self.mapView viewWillDisappear]; } #pragma mark 代理方法获取用户位置信息时调用 - (void)didUpdateUserLocation:(BMKUserLocation *)userLocation { NSLog(@"%f", userLocation.location.horizontalAccuracy); // 地图加载用户位置图层 [self.mapView updateLocationData:userLocation]; // 将用户当前位置设置为地图中心点 self.mapView.centerCoordinate = userLocation.location.coordinate; // 设置地图缩放比例 self.mapView.zoomLevel = 17; } @end |
效果展示: