iOS MapKit custom pins

前端 未结 3 663
别跟我提以往
别跟我提以往 2020-12-13 08:01

How do I show images instead of pins in the map. So far I can only add pins on tap. A sample code of the .m would extremely help since i\'m still new to iOS programming.

3条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-13 08:27

    #pragma mark -
    #pragma mark MKMapView delegate
    - (MKAnnotationView *)mapView:(MKMapView *)mapview viewForAnnotation:(id )annotation
    {
        if ([annotation isKindOfClass:[MKUserLocation class]])
            return nil;
        static NSString* AnnotationIdentifier = @"AnnotationIdentifier";
        MKAnnotationView *annotationView = [mapView dequeueReusableAnnotationViewWithIdentifier:AnnotationIdentifier];
        if(annotationView)
            return annotationView;
        else
        {
            MKAnnotationView *annotationView = [[[MKAnnotationView alloc] initWithAnnotation:annotation
                                                                             reuseIdentifier:AnnotationIdentifier] autorelease];
            annotationView.canShowCallout = YES;
            annotationView.image = [UIImage imageNamed:@"someImage.png"];
            UIButton* rightButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
            [rightButton addTarget:self action:@selector(writeSomething:) forControlEvents:UIControlEventTouchUpInside];
            [rightButton setTitle:annotation.title forState:UIControlStateNormal];
            annotationView.rightCalloutAccessoryView = rightButton;
            annotationView.canShowCallout = YES;
            annotationView.draggable = YES;
            return annotationView;
        }
        return nil;
     }
    

    EDIT:

    I could explain to you all about the MKAnnotationView, but I think you will find the documentation provided by Apple to be a far better explanation than from any other source. Check the overview section in the link.

    https://developer.apple.com/documentation/mapkit/mkannotationview

提交回复
热议问题