TA的每日心情 | 汗 2024-10-15 10:05 |
---|
签到天数: 372 天 [LV.9]以坛为家II
|
1、加载百度地图
2、开启定位服务
3、在定位服务的代理方法(获取用户地理位置后)中,设置地图中心点为用户当前位置
4、相关说明及代码实现:- //
- // 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
复制代码 效果展示:
|
|