需求:显示目标点以及自己所在位置,且有方向跟随。
先来张效果图:
CB1C3DEA-5126-47E4-BBE6-BD563C17DEA8.jpeg
1、初始化地图
-(MAMapView*)mapView{
if(!_mapView) {
self.mapView = [ZWMemoryCache.cache MAMapView];
_mapView.frame=CGRectMake(0,kSafeTop,KW,KH-kSafeTop);
_mapView.delegate=self;
//1.1、显示用户位置(重点)
_mapView.showsUserLocation = YES;
//地图跟着位置移动
_mapView.userTrackingMode = MAUserTrackingModeFollow;
//设置定位精度
_mapView.desiredAccuracy = kCLLocationAccuracyBest;
//设置定位距离
_mapView.distanceFilter = 5.0f;
_mapView.mapType = MKMapTypeStandard;
//1.2、新增点A
CLLocationCoordinate2D location = CLLocationCoordinate2DMake([_pointMod.position.coordinates[1] doubleValue], [_pointMod.position.coordinates[0] doubleValue]);//纬度,经度
MAPointAnnotation *pointAnnotation = [[MAPointAnnotation alloc] init];
pointAnnotation.coordinate= location;
pointAnnotation.title=_pointMod.name;
pointAnnotation.subtitle= [NSStringstringWithFormat:@"%@",_pointMod.position.coordinates];
[_mapViewaddAnnotation:pointAnnotation];
//将目标经纬度设置为地图中心店
self.mapView.centerCoordinate= location;
}
return _mapView;
}
2、在代理方法中修改定位点的图标,以及去除默认的淡蓝色圈
- (MAAnnotationView*)mapView:(MAMapView*)mapView viewForAnnotation:(id)annotation
{
//系统默认的当前位置点
if ([annotation isKindOfClass:[MAUserLocation class]]) {
staticNSString*userLocationStyleReuseIndetifier =@"userLocationStyleReuseIndetifier";
MAAnnotationView*annotationView = [mapViewdequeueReusableAnnotationViewWithIdentifier:userLocationStyleReuseIndetifier];
if(annotationView ==nil) {
annotationView = [[MAPinAnnotationViewalloc]initWithAnnotation:annotationreuseIdentifier:userLocationStyleReuseIndetifier];
}
//2.1、设置为带箭头的图标
annotationView.image= [UIImageimageNamed:@"userPosition"];
self.userLocationAnnotationView= annotationView;
//2.2、去掉高德地图淡蓝色精度圈
MAUserLocationRepresentation *r = [[MAUserLocationRepresentation alloc] init];
r.showsAccuracyRing = false;///精度圈是否显示,默认YES
[mapViewupdateUserLocationRepresentation:r];
returnannotationView;
}
//新增的点A
else if ([annotation isKindOfClass:[MAPointAnnotation class]]) {
staticNSString*pointReuseIndentifier =@"annotationReuseIndetifier";
MAPinAnnotationView*annotationView = (MAPinAnnotationView*)[mapView dequeueReusableAnnotationViewWithIdentifier:pointReuseIndentifier];
if(annotationView ==nil)
{
annotationView = [[MAPinAnnotationViewalloc]initWithAnnotation:annotationreuseIdentifier:pointReuseIndentifier];
}
annotationView.canShowCallout=YES; //设置气泡可以弹出,默认为NO
annotationView.animatesDrop=YES; //设置标注动画显示,默认为NO
// annotationView.draggable = YES; //设置标注可以拖动,默认为NO
// annotationView.pinColor = MAPinAnnotationColorPurple;
//2.3、设置自定义点样式
annotationView.image= [UIImageimageNamed:@"searchPosition"];
returnannotationView;
}
return nil;
}
3、设置箭头指示方位
- (void)mapView:(MAMapView*)mapView didUpdateUserLocation:(MAUserLocation*)userLocation updatingLocation:(BOOL)updatingLocation
{
//设置箭头指示方位
if(!updatingLocation &&self.userLocationAnnotationView!=nil) {
[UIView animateWithDuration:0.1 animations:^{
doubledegree = userLocation.heading.trueHeading-self.mapView.rotationDegree;
self.userLocationAnnotationView.transform = CGAffineTransformMakeRotation(degree * M_PI / 180.f );
}];
}
}
网友评论