在ios中的MGLMapView(MapBox)上的自定义注释

时间:2016-10-24 06:34:06

标签: ios objective-c annotations mkmapview mapbox

我使用MKMapView来绘制polyline并显示已保存的polyline以及自定义注释。

但现在尝试将MKMapView替换为MGLMapview

绘制ployline并成功显示默认注释,如上一个MKMapView

所示

但我坚持如何显示如下的自定义注释。

在MKMapView中使用 JPSThumbnailAnnotation

[1]

我的问题是,

如何显示上图中的自定义注释?

1 个答案:

答案 0 :(得分:0)

在.h文件中创建自定义Annotaion类

#import <Foundation/Foundation.h>
#import <MapKit/MapKit.h>

@interface LocationAnnotaion : NSObject<MKAnnotation>
@property NSString * titile;
@property (nonatomic, readonly)CLLocationCoordinate2D  coordinate;
-(id)initWithTitile:(NSString*) titile AndCoOrdinate:(CLLocationCoordinate2D) locationCoordinate;
-(MKAnnotationView*)annotationView;
@end

im .m文件写:

@implementation LocationAnnotaion
-(id)initWithTitile:(NSString *)titile AndCoOrdinate:(CLLocationCoordinate2D)locationCoordinate
{
    if (self = [super init])
    {
        _titile = titile;
        _coordinate= locationCoordinate;
    }
    return self;
}
-(MKAnnotationView*)annotationView
{
    MKAnnotationView * annotationView = [[MKAnnotationView alloc]initWithAnnotation:self reuseIdentifier:@"LocationAnnotation"];
    annotationView.enabled = YES;
   // annotationView.canShowCallout = YES;
    annotationView.image = [UIImage imageNamed:@"location_sign_map"];
    return annotationView;
}
@end

向地图视图添加注释

LocationAnnotaion *annotation = [[LocationAnnotaion alloc]initWithTitile:LOCATION_TEXT AndCoOrdinate:your_coordinate];
  [mapLocationView addAnnotation:annotation];

并在mapview委托中添加

-(MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation
{
    if ([annotation isKindOfClass:[CCLocationAnnotaion class]])
    {
        CCLocationAnnotaion * myAnnotation = (CCLocationAnnotaion*)annotation;
        MKAnnotationView * view = myAnnotation.annotationView;
        view.centerOffset = CGPointMake(0, -view.image.size.height/2);
        return view;
    }
    return nil;
}

希望它会有所帮助。