一.自定义当前位置图标,并且随着手机方向的变化箭头指向做出相对应的变动
样式如下图:
遵守 MAMapViewDelegate 代理 ,主要代码如下
#pragma mark 箭头方向
- (void)mapView:(MAMapView *)mapView didAddAnnotationViews:(NSArray *)views{
MAAnnotationView *view = views[0];
// 放到该方法中用以保证userlocation的annotationView已经添加到地图上了。
if ([view.annotation isKindOfClass:[MAUserLocation class]])
{
MAUserLocationRepresentation *pre = [[MAUserLocationRepresentation alloc] init];
// pre.fillColor = [UIColor colorWithRed:0.0 green:0.0 blue:255 alpha:0.1];
// pre.strokeColor = [UIColor colorWithRed:0.1 green:0.1 blue:0.9 alpha:1.0];
pre.image = [UIImage imageNamed:@"NowLocal"];//自定义当前位置的图片
// pre.lineWidth = 2;
// pre.lineDashPattern = @[@6, @3];
[self.mapView updateUserLocationRepresentation:pre];
view.calloutOffset = CGPointMake(0, 0);
view.canShowCallout = YES;
self.userLocationAnnotationView = view;
}
}
/**
* @brief 位置或者设备方向更新后,会调用此函数
* @param mapView 地图View
* @param userLocation 用户定位信息(包括位置与设备方向等数据)
* @param updatingLocation 标示是否是location数据更新, YES:location数据更新 NO:heading数据更新
*/
- (void)mapView:(MAMapView *)mapView didUpdateUserLocation:(MAUserLocation *)userLocation updatingLocation:(BOOL)updatingLocation{
if (!updatingLocation && self.userLocationAnnotationView != nil)
{
[UIView animateWithDuration:0.1 animations:^{
double degree = userLocation.heading.trueHeading;
self.userLocationAnnotationView.transform = CGAffineTransformMakeRotation(degree * M_PI / 180.f );
}];
}
}
二. 添加自定义样式点标记、添加自定义气泡
1.添加自定义样式点标记 如下图:
2. 添加自定义气泡 如下图:
#pragma mark 添加自定义样式点标记
- (MAAnnotationView *)mapView:(MAMapView *)mapView viewForAnnotation:(id)annotation{
if ([annotation isMemberOfClass:[MAUserLocation class]]) {
return nil;
}
if ([annotation isKindOfClass:[MAPointAnnotation class]]){
static NSString *customReuseIndetifier = @"customReuseIndetifier";
//CustomAnnotationView 自定义标记点的类 github下载地址:https://github.com/c103363/CustomAnnotationView.git
CustomAnnotationView *annotationView = (CustomAnnotationView*)[mapView dequeueReusableAnnotationViewWithIdentifier:customReuseIndetifier];
if (annotationView == nil){
annotationView = [[CustomAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:customReuseIndetifier];
annotationView.canShowCallout = NO;
annotationView.draggable = YES;
annotationView.calloutOffset = CGPointMake(0, -5);
}
annotationView.portrait = [UIImage imageNamed:@"定位"];
annotationView.nameLabel.text = annotation.subtitle;
return annotationView;
}
return nil;
}