需求:顯示目標(biāo)點(diǎn)以及自己所在位置赎瞎,且有方向跟隨。
先來張效果圖:
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顶捷、顯示用戶位置(重點(diǎn))
_mapView.showsUserLocation = YES;
//地圖跟著位置移動
_mapView.userTrackingMode = MAUserTrackingModeFollow;
//設(shè)置定位精度
_mapView.desiredAccuracy = kCLLocationAccuracyBest;
//設(shè)置定位距離
_mapView.distanceFilter = 5.0f;
_mapView.mapType = MKMapTypeStandard;
//1.2、新增點(diǎn)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];
//將目標(biāo)經(jīng)緯度設(shè)置為地圖中心店
self.mapView.centerCoordinate= location;
}
return _mapView;
}
2项乒、在代理方法中修改定位點(diǎn)的圖標(biāo),以及去除默認(rèn)的淡藍(lán)色圈
- (MAAnnotationView*)mapView:(MAMapView*)mapView viewForAnnotation:(id)annotation
{
//系統(tǒng)默認(rèn)的當(dāng)前位置點(diǎn)
if ([annotation isKindOfClass:[MAUserLocation class]]) {
staticNSString*userLocationStyleReuseIndetifier =@"userLocationStyleReuseIndetifier";
MAAnnotationView*annotationView = [mapViewdequeueReusableAnnotationViewWithIdentifier:userLocationStyleReuseIndetifier];
if(annotationView ==nil) {
annotationView = [[MAPinAnnotationViewalloc]initWithAnnotation:annotationreuseIdentifier:userLocationStyleReuseIndetifier];
}
//2.1梁沧、設(shè)置為帶箭頭的圖標(biāo)
annotationView.image= [UIImageimageNamed:@"userPosition"];
self.userLocationAnnotationView= annotationView;
//2.2板丽、去掉高德地圖淡藍(lán)色精度圈
MAUserLocationRepresentation *r = [[MAUserLocationRepresentation alloc] init];
r.showsAccuracyRing = false;///精度圈是否顯示,默認(rèn)YES
[mapViewupdateUserLocationRepresentation:r];
returnannotationView;
}
//新增的點(diǎn)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; //設(shè)置氣泡可以彈出趁尼,默認(rèn)為NO
annotationView.animatesDrop=YES; //設(shè)置標(biāo)注動畫顯示埃碱,默認(rèn)為NO
// annotationView.draggable = YES; //設(shè)置標(biāo)注可以拖動,默認(rèn)為NO
// annotationView.pinColor = MAPinAnnotationColorPurple;
//2.3酥泞、設(shè)置自定義點(diǎn)樣式
annotationView.image= [UIImageimageNamed:@"searchPosition"];
returnannotationView;
}
return nil;
}
3砚殿、設(shè)置箭頭指示方位
- (void)mapView:(MAMapView*)mapView didUpdateUserLocation:(MAUserLocation*)userLocation updatingLocation:(BOOL)updatingLocation
{
//設(shè)置箭頭指示方位
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 );
}];
}
}