How to add annotation in Mapview with image and label in IPhone?

后端 未结 2 1202
谎友^
谎友^ 2021-02-02 04:21

\"image1\" \"image

I am using MKMapview in my i

2条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2021-02-02 05:01

    1. Create a custom Annotation class (MKAnnotation)
    2. Create a custom AnnotationView class (MKAnnotationView)

    Make sure you define the title, background image, and other elements of your custom annotation in the AnnotationView class as properties.

    1. Override the following method, and set the values of the properties you defined in AnnotationView class.

      - (MKAnnotationView*) mapView:(MKMapView *)mapView viewForAnnotation:(id)annotation{
      
      if ([ isKindOfClass:[MKUserLocation class]])
          return nil;
      
      CustomAnnotation* custom_anno = (CustomAnnotation*)[mapView dequeueReusableAnnotationViewWithIdentifier:@"custom_annotation"];
      
      if (!custom_anno){
      custom_anno = [[CustomAnnotation alloc] initWithAnnotation:annotation reuseIdentifier:@"custom_annotation"];
      custom_anno.frame = CGRectMake(0, 0, 250, 57.5);
      
      custom_anno.canShowCallout = NO;//CHange if you want to change callout behaviour (thats is it's abilty to apper). I set it top no because i did not want a callout.
      
      UIImageView* icon = [[UIImageView alloc] initWithFrame:CGRectMake(5, 5, 40, 40)];
      [icon setImageWithURL: placeholderImage:];//placeholder image = nil if you do not want one.
      icon.backgroundColor = [UIColor lightGrayColor];
      
      UILabel* name = [[UILabel alloc] initWithFrame:CGRectMake(50, 5, 200, 20)];
      name.text = nameValue;
      
      UILabel* category = [[UILabel alloc] initWithFrame:CGRectMake(50, 25, 200, 20)];
      category.text = otherValue;
      
      [custom_anno addSubview:icon];
      [custom_anno addSubview:name];
      [custom_anno addSubview:category];
      }
      return custom_anno;
      }
      
      
      

      My custom annotation class was 'Annotation' and custom annotation view class was 'CustomAnnotation'. Change according to your needs.

      After this, just create an object of the Annotationclass, and use it.

      EDIT :

      Make sure you override the follwing method int he custom annotation view class:

      in .h:

      - (id)initWithAnnotation:(id)annotation reuseIdentifier:(NSString *)reuseIdentifier;
      

      in .m:

      - (id)initWithAnnotation:(id)annotation reuseIdentifier:(NSString *)reuseIdentifier{
      self = [super initWithAnnotation:annotation reuseIdentifier:reuseIdentifier];
      if (self) {
      }
      return self;
      

      }

      EDIT 2 :

      You may use it in your view controller like this :

      MKMapView* cellMap = [[MKMapView alloc] initWithFrame:CGRectMake(0, 0, 290, 100)];
      cellMap.delegate = self;
      cellMap.userInteractionEnabled = NO;
      
      CLLocationCoordinate2D center = CLLocationCoordinate2DMake();
      MKCoordinateSpan span = MKCoordinateSpanMake(0.04, 0.04);
      MKCoordinateRegion region = MKCoordinateRegionMake(center, span);
      cellMap.showsUserLocation = NO;
      [cellMap setRegion:region animated:YES];
      
      Annotation* anon = [[Annotation alloc] init];
      anon.coordinate = center;
      
      [cellMap addAnnotation:anon];
      [checkIn addSubview:cellMap];
      

      提交回复
      热议问题