简体   繁体   中英

Custom annotation on MGLMapView (MapBox) in ios

I used MKMapView for drawing polyline and showing saved polyline and also custom annotations.

But now trying to replace MKMapView with MGLMapview .

Drawing ployline and showing default annotations successfully as shown in the previous MKMapView

But I stuck at how to show custom annotations like below.

Used JPSThumbnailAnnotation in MKMapView

[1]

My question is,

How can I show custom annotations like above image ?

Create your custom Annotaion class in .h file

#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:

@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

-(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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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