根据 MapKit 注释标注单击在视图中加载数据

发布于 2024-09-16 01:02:40 字数 155 浏览 6 评论 0原文

我有一张地图,其注释标注上有一个公开按钮,单击该按钮会显示详细视图。地图上的每个点都需要在详细视图中加载不同的数据 - 出于显而易见的原因,我不想为每个注释构建单独的视图,但我不确定执行此操作的最佳方法。

如何标记已单击哪个注释,以便我可以通过详细信息的视图控制器加载正确的数据?

I have a map with a disclosure button on the callout of it's annotations, which when clicked displays a detail view. Each point on the map needs to load up different data in the detail view - I dont want to build separate views for each annotation, for obvious reasons, but I am unsure of the best way to do this.

How do I flag which annotation has been clicked, so I can load the correct data through the detail's view controller?

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

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

发布评论

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

评论(1

暗恋未遂 2024-09-23 01:02:40

您可以在注释类中实现数据加载功能。为此,您需要在注释类中引用该数据。

您的注释类可以类似于

@interface MyAnnotation : NSObject<MKAnnotation> {
     CLLocationCoordinate2D coordinate;

     MyDataObject *mydataObject;
}

- (id) initWithCoordinate:(CLLocoationCoordinate2D)coord withMyData:(MyDataObject*)aDataObject;
- (void) loadDetailView;   // this function will load the data in myDataObject to the DetailView

要获得标注触摸,您可以在实现 MKMapViewDelegate 的控制器中实现 calloutAccessaryControlTapped 函数。

该函数将类似于

- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control{
      MyAnnotation *myAnnotation = (MyAnnotation*)view.annotation;
      [myAnnotation loadDetailView];
}

Edit

initWithCooperative 的实现将类似于:

@implementation MyAnnotation

....

- (id) initWithCoordinate:(CLLocoationCoordinate2D)coord withMyData:(MyDataObject*)aDataObject{
    coordinate = coord;    // cannot use "self.coordinate" here as CLLocationCoordinate2D is double instead of object (?)

    self.title = <# some name #> // assign some string to the title property

    self.mydataObject = aDataObject; // this is your data object

    return self;
}

....

You can implement the data loading function in your annotation class. To do that you will need to have a reference to that data in your annotation class.

Your annotation class can be something like

@interface MyAnnotation : NSObject<MKAnnotation> {
     CLLocationCoordinate2D coordinate;

     MyDataObject *mydataObject;
}

- (id) initWithCoordinate:(CLLocoationCoordinate2D)coord withMyData:(MyDataObject*)aDataObject;
- (void) loadDetailView;   // this function will load the data in myDataObject to the DetailView

To get the callout touch, you can implement calloutAccessaryControlTapped function in the controller where you implement MKMapViewDelegate.

The function will be something like

- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control{
      MyAnnotation *myAnnotation = (MyAnnotation*)view.annotation;
      [myAnnotation loadDetailView];
}

Edit

The implementation of initWithCoordinate will be something like:

@implementation MyAnnotation

....

- (id) initWithCoordinate:(CLLocoationCoordinate2D)coord withMyData:(MyDataObject*)aDataObject{
    coordinate = coord;    // cannot use "self.coordinate" here as CLLocationCoordinate2D is double instead of object (?)

    self.title = <# some name #> // assign some string to the title property

    self.mydataObject = aDataObject; // this is your data object

    return self;
}

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