从自定义标注跳转到 MKMap 中的另一个标注

发布于 2024-10-11 06:38:42 字数 3559 浏览 4 评论 0原文

我一直在关注这个教程 显示自定义注释标注气泡。 如果您只有一个带有自定义注释视图的注释,它会完美地工作。

但是,我需要在地图上显示更多内容,并且从自定义注释视图切换到另一个注释视图时遇到麻烦。如果我在选择了一个图钉后单击另一个图钉并希望显示新的自定义注释视图,则它不起作用。我首先要在地图视图上随机单击其他地方。我想我在 DidDeselect 方法中有一些工作要做,但我不确定......

你会如何解决这样的问题?

- (void)mapView:(MKMapView *)mapView didDeselectAnnotationView:(MKAnnotationView *)view {
    if (self.calloutAnnotation && [view.annotation isKindOfClass:[MyHomeAnnotation class]]) {
        [self.mapView removeAnnotation: self.calloutAnnotation];
    }
}
- (void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view {
     if ([view.annotation isKindOfClass:[MyHomeAnnotation class]]) { 
        if (self.calloutAnnotation == nil) {
            self.calloutAnnotation = [[CalloutMapAnnotation alloc] initWithLatitude:view.annotation.coordinate.latitude
                                                                       andLongitude:view.annotation.coordinate.longitude];
        } else {
            self.calloutAnnotation.latitude = view.annotation.coordinate.latitude;
            self.calloutAnnotation.longitude = view.annotation.coordinate.longitude;
        }
        [self.mapView addAnnotation:self.calloutAnnotation];
        self.selectedAnnotationView = view;
    }
}



- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation {
    if (annotation == self.calloutAnnotation) {
        CalloutMapAnnotationView *calloutMapAnnotationView = (CalloutMapAnnotationView *)[self.mapView dequeueReusableAnnotationViewWithIdentifier:@"CalloutAnnotation"];
        if (!calloutMapAnnotationView) {
            calloutMapAnnotationView = [[[CalloutMapAnnotationView alloc] initWithAnnotation:annotation 
                                                                             reuseIdentifier:@"CalloutAnnotation"] autorelease];
            calloutMapAnnotationView.contentHeight = 78.0f;
            UIImage *asynchronyLogo = [UIImage imageNamed:@"cykelrød1.png"];
            UIImageView *asynchronyLogoView = [[[UIImageView alloc] initWithImage:asynchronyLogo] autorelease];
            asynchronyLogoView.frame = CGRectMake(5, 2, asynchronyLogoView.frame.size.width, asynchronyLogoView.frame.size.height);
            [calloutMapAnnotationView.contentView addSubview:asynchronyLogoView];
        }
        calloutMapAnnotationView.parentAnnotationView = self.selectedAnnotationView;
        calloutMapAnnotationView.mapView = self.mapView;
        return calloutMapAnnotationView;
    } else if ([annotation isKindOfClass:[MyHomeAnnotation class]]) {
        MKPinAnnotationView *annotationView = [[[MKPinAnnotationView alloc] initWithAnnotation:annotation 
                                                                               reuseIdentifier:@"CustomAnnotation"] autorelease];
        annotationView.canShowCallout = NO;
        annotationView.pinColor = MKPinAnnotationColorGreen;
        return annotationView;
    }else if ([annotation isKindOfClass:[MyMapAnnotation class]]) {
        MKPinAnnotationView *annotationView = [[[MKPinAnnotationView alloc] initWithAnnotation:annotation 
                                                                               reuseIdentifier:@"NormalAnnotation"] autorelease];
        annotationView.canShowCallout = YES;
        annotationView.pinColor = MKPinAnnotationColorPurple;
        return annotationView;
    }


    return nil;
}

I've been following this tutorial to show custom annotation callout bubbles.
It works perfectly if you have only one annotation with a custom annotationview.

However, I need to have more on my map, and I have troubles switching from a custom annotationview to another one. If I click on another pin when having selected already one and would like to make appear the new custom annotationview, it doesn't work. I have first to click somewhere else random on the mapview. I guess I have something to work on in DidDeselect Method, but I'm not sure...

How would you solve such a problem?

- (void)mapView:(MKMapView *)mapView didDeselectAnnotationView:(MKAnnotationView *)view {
    if (self.calloutAnnotation && [view.annotation isKindOfClass:[MyHomeAnnotation class]]) {
        [self.mapView removeAnnotation: self.calloutAnnotation];
    }
}
- (void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view {
     if ([view.annotation isKindOfClass:[MyHomeAnnotation class]]) { 
        if (self.calloutAnnotation == nil) {
            self.calloutAnnotation = [[CalloutMapAnnotation alloc] initWithLatitude:view.annotation.coordinate.latitude
                                                                       andLongitude:view.annotation.coordinate.longitude];
        } else {
            self.calloutAnnotation.latitude = view.annotation.coordinate.latitude;
            self.calloutAnnotation.longitude = view.annotation.coordinate.longitude;
        }
        [self.mapView addAnnotation:self.calloutAnnotation];
        self.selectedAnnotationView = view;
    }
}



- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation {
    if (annotation == self.calloutAnnotation) {
        CalloutMapAnnotationView *calloutMapAnnotationView = (CalloutMapAnnotationView *)[self.mapView dequeueReusableAnnotationViewWithIdentifier:@"CalloutAnnotation"];
        if (!calloutMapAnnotationView) {
            calloutMapAnnotationView = [[[CalloutMapAnnotationView alloc] initWithAnnotation:annotation 
                                                                             reuseIdentifier:@"CalloutAnnotation"] autorelease];
            calloutMapAnnotationView.contentHeight = 78.0f;
            UIImage *asynchronyLogo = [UIImage imageNamed:@"cykelrød1.png"];
            UIImageView *asynchronyLogoView = [[[UIImageView alloc] initWithImage:asynchronyLogo] autorelease];
            asynchronyLogoView.frame = CGRectMake(5, 2, asynchronyLogoView.frame.size.width, asynchronyLogoView.frame.size.height);
            [calloutMapAnnotationView.contentView addSubview:asynchronyLogoView];
        }
        calloutMapAnnotationView.parentAnnotationView = self.selectedAnnotationView;
        calloutMapAnnotationView.mapView = self.mapView;
        return calloutMapAnnotationView;
    } else if ([annotation isKindOfClass:[MyHomeAnnotation class]]) {
        MKPinAnnotationView *annotationView = [[[MKPinAnnotationView alloc] initWithAnnotation:annotation 
                                                                               reuseIdentifier:@"CustomAnnotation"] autorelease];
        annotationView.canShowCallout = NO;
        annotationView.pinColor = MKPinAnnotationColorGreen;
        return annotationView;
    }else if ([annotation isKindOfClass:[MyMapAnnotation class]]) {
        MKPinAnnotationView *annotationView = [[[MKPinAnnotationView alloc] initWithAnnotation:annotation 
                                                                               reuseIdentifier:@"NormalAnnotation"] autorelease];
        annotationView.canShowCallout = YES;
        annotationView.pinColor = MKPinAnnotationColorPurple;
        return annotationView;
    }


    return nil;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

别想她 2024-10-18 06:38:42

这是我的解决方案,我使用纬度和经度来区分应从地图视图中删除哪个标注。希望这会有所帮助。

- (void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view {
   NSLog(@"didSelectAnnotationView:%f", view.annotation.coordinate.latitude);

   if (self.calloutAnnotation == nil) {

      CalloutMapAnnotation *tempCallout = [[CalloutMapAnnotation alloc] 
                                         initWithLatitude:view.annotation.coordinate.latitude              
                                         andLongitude:view.annotation.coordinate.longitude];

      self.calloutAnnotation = tempCallout;
      [tempCallout release];

  } else {

      //remove callout when callout already exist
      [self.myMapView removeAnnotation: self.calloutAnnotation];
      self.selectedAnnotationView = nil;

      //reposition
      self.calloutAnnotation.latitude = view.annotation.coordinate.latitude;
      self.calloutAnnotation.longitude = view.annotation.coordinate.longitude;

  }

  [self.myMapView addAnnotation:self.calloutAnnotation];
  self.selectedAnnotationView = view;
}

- (void)mapView:(MKMapView *)mapView didDeselectAnnotationView:(MKAnnotationView *)view   {

  NSLog(@"didDeselectAnnotationView:%f", view.annotation.coordinate.latitude);

  //use the latitude and longitude to avoid remove twice

  if (self.calloutAnnotation &&
    self.selectedAnnotationView.annotation.coordinate.latitude == view.annotation.coordinate.latitude &&
    self.selectedAnnotationView.annotation.coordinate.longitude == view.annotation.coordinate.longitude
    ) {

      [self.myMapView removeAnnotation: self.calloutAnnotation];
      self.selectedAnnotationView = nil;
      self.calloutAnnotation.isAddtoMap = NO;
  }

}

Here are my solution, I use latitude and longitude to distinguish which callout should be remove from mapview. Hope this will be help.

- (void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view {
   NSLog(@"didSelectAnnotationView:%f", view.annotation.coordinate.latitude);

   if (self.calloutAnnotation == nil) {

      CalloutMapAnnotation *tempCallout = [[CalloutMapAnnotation alloc] 
                                         initWithLatitude:view.annotation.coordinate.latitude              
                                         andLongitude:view.annotation.coordinate.longitude];

      self.calloutAnnotation = tempCallout;
      [tempCallout release];

  } else {

      //remove callout when callout already exist
      [self.myMapView removeAnnotation: self.calloutAnnotation];
      self.selectedAnnotationView = nil;

      //reposition
      self.calloutAnnotation.latitude = view.annotation.coordinate.latitude;
      self.calloutAnnotation.longitude = view.annotation.coordinate.longitude;

  }

  [self.myMapView addAnnotation:self.calloutAnnotation];
  self.selectedAnnotationView = view;
}

- (void)mapView:(MKMapView *)mapView didDeselectAnnotationView:(MKAnnotationView *)view   {

  NSLog(@"didDeselectAnnotationView:%f", view.annotation.coordinate.latitude);

  //use the latitude and longitude to avoid remove twice

  if (self.calloutAnnotation &&
    self.selectedAnnotationView.annotation.coordinate.latitude == view.annotation.coordinate.latitude &&
    self.selectedAnnotationView.annotation.coordinate.longitude == view.annotation.coordinate.longitude
    ) {

      [self.myMapView removeAnnotation: self.calloutAnnotation];
      self.selectedAnnotationView = nil;
      self.calloutAnnotation.isAddtoMap = NO;
  }

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文