custom annotation apple mapkit

前端 未结 2 1662
醉话见心
醉话见心 2020-12-22 01:03

I am trying to assign two custom annotations, one called \"arsenal.png\" and one called \"chelsea.png\"

Using apple mapkit framework, xcode 7.

Need assistanc

2条回答
  •  萌比男神i
    2020-12-22 01:49

    You can use viewForAnnotation delegate method for creating custom annotation.

    - (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id)annotation
    

    See the below tutorials

    http://www.codigator.com/tutorials/advanced-mapkit-tutorial-for-ios-custom-callout/

    http://ios-funda.blogspot.in/2012/08/custom-annotations-example.html

    EDIT

    You need to set the delegate in viewDidLoad

    self.myMapView.delegate = self
    

    Now in .h

    @interface TrucksViewController : UIViewController
    

    Then implement the delegate method

    - (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id )annotation  {
    
        if ([annotation isKindOfClass:[MKUserLocation class]]) {
            return nil;
        }
    
        static NSString *defaultPinId = @"Pin";
        CustomAnnotation *pinView = (CustomAnnotation *)[mapView dequeueReusableAnnotationViewWithIdentifier:defaultPinId];
    
    
        if (pinView == nil) {
    
            pinView = [[CustomAnnotation alloc]initWithAnnotation:annotation reuseIdentifier:defaultPinId];
            //pinView.pinColor = MKPinAnnotationColorRed;
            //pinView.animatesDrop = YES;
            pinView.canShowCallout = YES;
            //NSLog(@"%f",[annotation coordinate].latitude);
    
        }
        else    {
            pinView.annotation = annotation;
        }
    
    
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
        btn.frame = CGRectMake(275, 27, 30, 30);
    
        //Adding a navigation inside a callout view
        btn.tag = [sid intValue];
        //NSLog(@"%i",btn.tag);
        [btn addTarget:self action:@selector(YOUR_SELECTOR) forControlEvents:UIControlEventTouchUpInside];
        pinView.rightCalloutAccessoryView = btn;
    
        return pinView;
    
    }
    

    in CustomAnnotation.m add the below methods

    -(id)initWithAnnotation:(id)annotation reuseIdentifier:(NSString *)reuseIdentifier    {
    
        self = [super initWithAnnotation:annotation reuseIdentifier:reuseIdentifier];
        if (self != nil) {
            CGRect frame = self.frame;
            frame.size = CGSizeMake(46.0, 49.0);
            self.frame = frame;
            self.backgroundColor = [UIColor clearColor];
            self.centerOffset = CGPointMake(-5, -5);
        }
        return self;
    }
    
    
    // Only override drawRect: if you perform custom drawing.
    // An empty implementation adversely affects performance during animation.
    - (void)drawRect:(CGRect)rect
    {
        // Drawing code
        [[UIImage imageNamed:@"locatorico"] drawInRect:rect];
    
    }
    

提交回复
热议问题