需求:顯示目標點以及自己所在位置,且有方向跟隨。
先來張效果圖:

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]);//緯度,經(jīng)度
MAPointAnnotation *pointAnnotation = [[MAPointAnnotation alloc] init];
pointAnnotation.coordinate= location;
pointAnnotation.title=_pointMod.name;
pointAnnotation.subtitle= [NSStringstringWithFormat:@"%@",_pointMod.position.coordinates];
[_mapViewaddAnnotation:pointAnnotation];
//將目標經(jīng)緯度設置為地圖中心店
self.mapView.centerCoordinate= location;
}
return _mapView;
}
2、在代理方法中修改定位點的圖標,以及去除默認的淡藍色圈
- (MAAnnotationView*)mapView:(MAMapView*)mapView viewForAnnotation:(id)annotation
{
//系統(tǒng)默認的當前位置點
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 );
}];
}
}