显示定位蓝点 最后更新时间: 2021年03月10日
定位蓝点指的是进入地图后显示用户当前位置点的功能,效果如下:
第一步,配置头文件
在您的 ViewController.h 中添加以下两个头文件:
#import <MAMapKit/MAMapKit.h>
#import <AMapFoundationKit/AMapFoundationKit.h>
第二步,显示定位蓝点
创建地图对象
在 UserLocationViewController.m 文件中添加实例化 MAMapView 对象,示例如下:
- (void)viewDidLoad
{
[super viewDidLoad];
///初始化MAMapWebViewProcotol协议实现类
self.webViewContainer = [[MAWKWebView alloc] initWithFrame:self.view.bounds];
///初始化地图
self.map = [[MAMap alloc] initWithWebView:self.webViewContainer];
self.map.delegate = self;
///把WKWebView添加至view
[self.view addSubview:self.webViewContainer.webView];
}
override func viewDidLoad() {
super.viewDidLoad()
///初始化MAMapWebViewProcotol协议实现类
webViewContainer = MAWKWebView.init(frame: self.view.bounds)
///初始化地图
map = MAMap.init(webView: webViewContainer)
map.delegate = self;
///把WKWebView添加至view
self.view.addSubview(webViewContainer.webView)
}
开启定位
高德地图 3D-Lite SDK内部不在涉及定位相关功能,所以需要外部发起定位,将定位结果传入地图SDK。此处以高德定位SDK为例:
- (void)viewDidLoad {
[super viewDidLoad];
///初始化定位SDK实例
self.locationManager = [[AMapLocationManager alloc] init];
self.locationManager.delegate = self;
///开启连续定位
[self.locationManager startUpdatingLocation];
///开启方向定位
[self.locationManager startUpdatingHeading];
}
//AMapLocationManagerDelegate-定位数据回调
- (void)amapLocationManager:(AMapLocationManager *)manager didUpdateLocation:(CLLocation *)location reGeocode:(AMapLocationReGeocode *)reGeocode
{
//定位数据回传地图SDK
[self.map setUserLocation:location coordinateType:AMapCoordinateTypeAMap];
}
//AMapLocationManagerDelegate-位置方向回调
- (void)amapLocationManager:(AMapLocationManager *)manager didUpdateHeading:(CLHeading *)newHeading{
//位置方向回传地图SDK
[self.map setUserHeading:newHeading];
}
override func viewDidLoad() {
super.viewDidLoad()
///初始化定位SDK实例
locationManager = AMapLocationManager.init()
locationManager.delegate = self;
///开启连续定位
locationManager.startUpdatingLocation()
///开启方向定位
locationManager.startUpdatingHeading()
}
//AMapLocationManagerDelegate-定位数据回调
func amapLocationManager(_ manager: AMapLocationManager!, didUpdate location: CLLocation!, reGeocode: AMapLocationReGeocode!) {
//定位数据回传地图SDK
map.setUserLocation(location, coordinateType: AMapCoordinateType.aMap)
}
//AMapLocationManagerDelegate-位置方向回调
func amapLocationManager(_ manager: AMapLocationManager!, didUpdate newHeading: CLHeading!) {
//位置方向回传地图SDK
map.setUserHeading(newHeading)
}
注意:地图SDK接收定位数据类型为AMapCoordinateTypeAMap,如果使用系统定位则需要通过基础SDK AMapFoundationKit API接口 AMapCoordinateConvert(CLLocationCoordinate2D coordinate, AMapCoordinateType type) 转换后传给地图SDK。
自定义定位小蓝点
初始化 MAUserLocationRepresentation 对象:
MAUserLocationRepresentation *represent = [[MAUserLocationRepresentation alloc] init];
let represent:MAUserLocationRepresentation = MAUserLocationRepresentation.init()
精度圈是否显示:
represent.showsAccuracyRing = NO;///精度圈是否显示,默认YES
represent.showsAccuracyRing = true
调整精度圈填充颜色:
represent.fillColor = [UIColor redColor];///精度圈 填充颜色, 默认 kAccuracyCircleDefaultColor:
represent.fillColor = UIColor.red
调整精度圈边线颜色:
represent.strokeColor = [UIColor blueColor];///精度圈 边线颜色, 默认 kAccuracyCircleDefaultColor
represent.strokeColor = UIColor.blue
调整精度圈边线宽度:
represent.lineWidth = 2;///精度圈 边线宽度,默认0
represent.lineWidth = 2.0
调整定位蓝点的图标:
represent.image = [UIImage imageNamed:@"你的图片"]; ///定位图标
represent.image = UIImage.init(named: "你的图片")
执行:
[self.map updateUserLocationRepresentation:represent];
map.update(represent)