Mapkit 具有多注释(callout),映射下一个视图

发布于 2024-09-14 06:33:21 字数 321 浏览 5 评论 0原文

需要一些帮助来解决我面临的 Mapkit 问题。应该是一个愚蠢的问题,或者我在浏览 Mapkit 框架时错过了一些东西。

这是参议员。 当用户执行某些搜索(如披萨)时,我会在地图上放置多个注释。 添加了右侧注释视图的按钮,单击后将打开下一个详细视图。问题是如何将一些信息发送到下一个视图,例如我在创建注释时向注释添加索引,现在我想从注释访问此信息,并通过按钮上设置的选择器将其传递到下一个视图。

我已经检查了所有精细的地图套件,但没有找到可以将这些信息与下一个视图和注释进行映射的地图套件。

希望我的问题没有让你们感到困惑。请让我知道我会重新设计它。

提前打招呼。

Wanted some help with a problem with mapkit I am facing. Should be a silly problem or I have missed out something while going through the mapkit framework.

Here is the senario.
I am placing multiple annotation on the map when the user performs some search like pizza.
Added button for the right annotation view, on click which opens a next detail view. The problem is how to send some information to the next view, for example I add index to annotations while creating them, now I want to access this information from annotation, and pass it to the next view via the selector set on the button.

I have checked all the mapkit delicate, but don't find a one where I can map this information with the next view and annotation.

Hope I have not confused you guys in my question. Please let me know I will reframe it.

Thaking in advance.

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

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

发布评论

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

评论(3

執念 2024-09-21 06:33:21

当您为注释创建 UIButton 时,请设置 tag 属性(标记是 UIViewNSInteger 属性)到标识相关对象的 id 或数组索引。然后,您可以将该标记值从 sender 参数检索到选择器。


编辑:这是一些示例代码。

您创建注释视图并将按钮关联到委托的 -mapView:viewForAnnotation: method:

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation {
    // Boilerplate pin annotation code
    MKPinAnnotationView *pin = (MKPinAnnotationView *) [self.map dequeueReusableAnnotationViewWithIdentifier: @"restMap"];
    if (pin == nil) {
        pin = [[[MKPinAnnotationView alloc] initWithAnnotation: annotation reuseIdentifier: @"restMap"] autorelease];
    } else {
        pin.annotation = annotation;
    }
    pin.pinColor = MKPinAnnotationColorRed
    pin.canShowCallout = YES;
    pin.animatesDrop = NO;

    // now we'll add the right callout button
    UIButton *detailButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];

    // customize this line to fit the structure of your code.  basically
    // you just need to find an integer value that matches your object in some way:
    // its index in your array of MKAnnotation items, or an id of some sort, etc
    // 
    // here I'll assume you have an annotation array that is a property of the current
    // class and we just want to store the index of this annotation.
    NSInteger annotationValue = [self.annotations indexOfObject:annotation];

    // set the tag property of the button to the index
    detailButton.tag = annotationValue;

    // tell the button what to do when it gets touched
    [detailButton addTarget:self action:@selector(showDetailView:) forControlEvents:UIControlEventTouchUpInside];

    pin.rightCalloutAccessoryView = detailButton;
    return pin;

}

然后在操作方法中,您将从 tag 中解压值并使用它来显示正确的详细信息:

-(IBAction)showDetailView:(UIView*)sender {
    // get the tag value from the sender
    NSInteger selectedIndex = sender.tag;
    MyAnnotationObject *selectedObject = [self.annotations objectAtIndex:selectedIndex];

    // now you know which detail view you want to show; the code that follows
    // depends on the structure of your app, but probably looks like:
    MyDetailViewController *detailView = [[MyDetailViewController alloc] initWithNibName...];
    detailView.detailObject = selectedObject;

    [[self navigationController] pushViewController:detailView animated:YES];
    [detailView release];
}

When you create the UIButton for the annotation, set the tag property (tag is an NSInteger property of UIView) to an id or array index that identifies the relevant object. You can then retrieve that tag value from the sender parameter to your selector.


Edit: here's some sample code.

You create your annotation view and associate the button in your delegate's -mapView:viewForAnnotation: method:

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation {
    // Boilerplate pin annotation code
    MKPinAnnotationView *pin = (MKPinAnnotationView *) [self.map dequeueReusableAnnotationViewWithIdentifier: @"restMap"];
    if (pin == nil) {
        pin = [[[MKPinAnnotationView alloc] initWithAnnotation: annotation reuseIdentifier: @"restMap"] autorelease];
    } else {
        pin.annotation = annotation;
    }
    pin.pinColor = MKPinAnnotationColorRed
    pin.canShowCallout = YES;
    pin.animatesDrop = NO;

    // now we'll add the right callout button
    UIButton *detailButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];

    // customize this line to fit the structure of your code.  basically
    // you just need to find an integer value that matches your object in some way:
    // its index in your array of MKAnnotation items, or an id of some sort, etc
    // 
    // here I'll assume you have an annotation array that is a property of the current
    // class and we just want to store the index of this annotation.
    NSInteger annotationValue = [self.annotations indexOfObject:annotation];

    // set the tag property of the button to the index
    detailButton.tag = annotationValue;

    // tell the button what to do when it gets touched
    [detailButton addTarget:self action:@selector(showDetailView:) forControlEvents:UIControlEventTouchUpInside];

    pin.rightCalloutAccessoryView = detailButton;
    return pin;

}

Then in your action method, you'll unpack the value from tag and use it to display the right detail:

-(IBAction)showDetailView:(UIView*)sender {
    // get the tag value from the sender
    NSInteger selectedIndex = sender.tag;
    MyAnnotationObject *selectedObject = [self.annotations objectAtIndex:selectedIndex];

    // now you know which detail view you want to show; the code that follows
    // depends on the structure of your app, but probably looks like:
    MyDetailViewController *detailView = [[MyDetailViewController alloc] initWithNibName...];
    detailView.detailObject = selectedObject;

    [[self navigationController] pushViewController:detailView animated:YES];
    [detailView release];
}
生来就爱笑 2024-09-21 06:33:21

在注释视图中,是否可以抓取标题或副标题或您在创建图钉时使用的任何其他信息?我想要做的是根据这些变量之一在注释中弹出某个图像。

#import "MapPin.h"

@implementation MapPin


@synthesize coordinate;
@synthesize title;
@synthesize subtitle;
@synthesize indexnumber;
@synthesize imageFile;

-(id)initWithCoordinates:(CLLocationCoordinate2D)location
               placeName: placeName
             description:description
                indexnum:indexnum
            imageFileLoc:imageFileLoc{

    self = [super init];
    if (self != nil) {
        imageFile=imageFileLoc;
        [imageFile retain];
        indexnumber=indexnum;
        [indexnumber retain];
        coordinate = location;
        title = placeName;
        [title retain];
        subtitle = description;
        [subtitle retain];
    }
    return self;

}



-(void)addAnnotations {

    // Normally read the data for these from the file system or a Web service
    CLLocationCoordinate2D coordinate = {35.9077803, -79.0454936};
    MapPin *pin = [[MapPin alloc]initWithCoordinates:coordinate
                                          placeName:@"Keenan Stadium"
                                        description:@"Tar Heel Football"
                                            indexnum:@"1"
                                        imageFileLoc:@"owl.jpg"];
    [self.map addAnnotation:pin];

In the Annotation view, is it possible to grab,say, the Title or Subtitle or any other information you used while creating pins? What i am looking to do is have a certain image popup in the annotation based on one of those variables.

#import "MapPin.h"

@implementation MapPin


@synthesize coordinate;
@synthesize title;
@synthesize subtitle;
@synthesize indexnumber;
@synthesize imageFile;

-(id)initWithCoordinates:(CLLocationCoordinate2D)location
               placeName: placeName
             description:description
                indexnum:indexnum
            imageFileLoc:imageFileLoc{

    self = [super init];
    if (self != nil) {
        imageFile=imageFileLoc;
        [imageFile retain];
        indexnumber=indexnum;
        [indexnumber retain];
        coordinate = location;
        title = placeName;
        [title retain];
        subtitle = description;
        [subtitle retain];
    }
    return self;

}



-(void)addAnnotations {

    // Normally read the data for these from the file system or a Web service
    CLLocationCoordinate2D coordinate = {35.9077803, -79.0454936};
    MapPin *pin = [[MapPin alloc]initWithCoordinates:coordinate
                                          placeName:@"Keenan Stadium"
                                        description:@"Tar Heel Football"
                                            indexnum:@"1"
                                        imageFileLoc:@"owl.jpg"];
    [self.map addAnnotation:pin];
风柔一江水 2024-09-21 06:33:21

另一种选择:

您可以实现这些方法:

- (void)mapView:(MKMapView *)mapView didSelectAnnotation:(MKAnnotationView *)view;
- (void)mapView:(MKMapView *)mapView didDeselectAnnotation:(MKAnnotationView *)view;

Another option:

You can implement these methods:

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