How to add more details in MKAnnotation in iOS

后端 未结 1 1916
既然无缘
既然无缘 2020-11-27 04:55

I want to add more details in MKAnnotation like location title, description, date, location name. So it will be four lines that are needed. But I found that only 2 parameter

相关标签:
1条回答
  • 2020-11-27 05:39

    Take a look at creating a custom MKAnnotationView object... it is basically a UIView that is tailored for map annotations. In that object you could have your 4 custom labels.

    In your MKMapViewDelegate class, you implement the viewForAnnotation method:

    - (CustomMapAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation
    {
        CustomMapAnnotationView *annotationView = nil;
    
        // determine the type of annotation, and produce the correct type of annotation view for it.
        CustomMapAnnotation* myAnnotation = (CustomMapAnnotation *)annotation;
    
        NSString* identifier = @"CustomMapAnnotation";
        CustomMapAnnotationView *newAnnotationView = (CustomMapAnnotationView *)[self.mapView dequeueReusableAnnotationViewWithIdentifier:identifier];
    
        if(nil == newAnnotationView) {
            newAnnotationView = [[[CustomMapAnnotationView alloc] initWithAnnotation:myAnnotation reuseIdentifier:identifier] autorelease];
        }
    
        annotationView = newAnnotationView;
        [annotationView setEnabled:YES];
        [annotationView setCanShowCallout:YES];
    
        return annotationView;
    }
    

    And that will display your custom view where ever you have an annotation... if you want a step by step tutorial, check out this video.

    hope this helps

    EDIT

    I actually just found a new Maps example on the apple dev site... has all the source code you need to go through. They are also using a custom MKAnnotationView called WeatherAnnotationView

    0 讨论(0)
提交回复
热议问题