我使用MKMapView
来绘制polyline
并显示已保存的polyline
以及自定义注释。
但现在尝试将MKMapView
替换为MGLMapview
。
绘制ployline
并成功显示默认注释,如上一个MKMapView
但我坚持如何显示如下的自定义注释。
在MKMapView中使用 JPSThumbnailAnnotation
我的问题是,
如何显示上图中的自定义注释?
答案 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;
}
希望它会有所帮助。