简体   繁体   English

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

[英]Custom annotation on MGLMapView (MapBox) in ios

I used MKMapView for drawing polyline and showing saved polyline and also custom annotations. 我使用MKMapView绘制polyline并显示保存的polyline和自定义注释。

But now trying to replace MKMapView with MGLMapview . 但现在尝试用MGLMapview替换MKMapView

Drawing ployline and showing default annotations successfully as shown in the previous MKMapView 绘制ployline并成功显示默认注释,如前面的MKMapView所示

But I stuck at how to show custom annotations like below. 但我坚持如何显示如下的自定义注释。

Used JPSThumbnailAnnotation in MKMapView 在MKMapView中使用JPSThumbnailAnnotation

[1]

My question is, 我的问题是,

How can I show custom annotations like above image ? 如何显示上图中的自定义注释?

Create your custom Annotaion class in .h file 在.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 file write: 我.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

Add annotation to map view 向地图视图添加注释

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

and in mapview delegate add 并在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;
}

Hope it will help . 希望它会有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM