objective-c - 如何将 map 注释 View 按钮与数据库连接以转到另一个 View ?

标签 objective-c ios database mkmapview

- (MKAnnotationView *)mapView:(MKMapView *)theMapView viewForAnnotation:(id <MKAnnotation>)annotation
{    
    //if it's user location, return nil
    if ([annotation isKindOfClass:[MKUserLocation class]])
        return nil;

    //try to dequeue an existing pin view first
    static NSString* AnnotationIdentifier = @"AnnotationIdentifier";
    MKPinAnnotationView* pinView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:AnnotationIdentifier];
    pinView.animatesDrop = YES;
    pinView.canShowCallout = YES;
    pinView.pinColor = MKPinAnnotationColorRed;

    //button on the right for popup for pins
    UIButton* rightButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
    [rightButton setTitle:annotation.title forState:UIControlStateNormal];
    [rightButton addTarget:self 
                    action:@selector(showDetails:) forControlEvents:UIControlEventTouchUpInside];
    pinView.rightCalloutAccessoryView = rightButton;

    //zoom button on the left of popup for pins
    UIButton* leftButton = [UIButton buttonWithType:UIButtonTypeContactAdd];
    [leftButton setTitle:annotation.title forState:UIControlStateNormal];
    [leftButton addTarget:self 
                    action:@selector(zoomToLocation:) forControlEvents:UIControlEventTouchUpInside];
    pinView.leftCalloutAccessoryView = leftButton;

    return pinView;
}

//for map view annotation right button
-(void)showDetails:(id)sender{
    NSLog(@"Annotation Click");
    //fypAppDelegate *appDelegate = (fypAppDelegate *)[[UIApplication sharedApplication] delegate];
    //Attraction *attraction = (Attraction *)[appDelegate.attractions objectAtIndex:sender];

    infoViewController *viewController = [self.storyboard instantiateViewControllerWithIdentifier:@"info"];
    self.infoView = viewController;
    [self.navigationController pushViewController:infoView animated:true];
}

//for map view annotation left button
-(void)zoomToLocation:(id)sender{
    NSLog(@"Annotation Click");
}

上面是 map 注释的委托(delegate)。我能够显示图钉并显示 map 注释 View ,但我不知道如何将按钮事件链接到下一个 View (infoViewController)。 正如你们所见,我希望使用右侧按钮来让用户查看有关该地点的更多信息,而使用左侧按钮,我希望允许用户放大该图钉的坐标。

数据来 self 创建的数据库。下面是我的做法,仅供引用(以防大家需要)

-(void)putPins
{
    fypAppDelegate *appDelegate = (fypAppDelegate *)[[UIApplication sharedApplication] delegate];   //get data
    [appDelegate readTopAttractions];   
    int i = 0;
    int count = appDelegate.attractions.count;
    self.mapAnnotations = [[NSMutableArray alloc] initWithCapacity:appDelegate.attractions.count];
    while (i < count) {
        Attraction *attraction = (Attraction *)[appDelegate.attractions objectAtIndex:i];
        i++;

        //Set coordinates for pin
        CLLocationCoordinate2D location;
        location.latitude = (double)[[attraction xCoor] doubleValue];
        location.longitude = (double)[[attraction yCoor] doubleValue];
        MapPin *mapPin = [[MapPin alloc] init];
        [mapPin setCoordinate:location];
        [mapPin setName: [attraction name]];
        NSString *desc = [attraction description];
        int i = 0, position;
        while(i < 50){
            if ([desc characterAtIndex:i] == ' '){
                position = i;
                i++;
            }
            else
                i++;
        }
        desc = [@"" stringByAppendingFormat:@"%@%@", [desc substringToIndex:position], @"..."];
        [mapPin setDescription: desc];
        [self.mapAnnotations addObject:mapPin];
    }

    [self.mapView addAnnotations:self.mapAnnotations];
}

如果你们需要更多细节,请告诉我。 谢谢你! =)

最佳答案

在您的 showDetails:zoomToLocation: 方法中,您可以通过执行以下操作获取对其标注按钮被点击的注释的引用:

MapPin *ann = (MapPin *)[mapView.selectedAnnotations objectAtIndex:0];


zoomToLocation: 中,您可以使用以下方法放大该注释:

[mapView setRegion:
    MKCoordinateRegionMakeWithDistance(ann.coordinate, 500, 500) 
        //500 meters vertical span, 500 meters horizontal span
    animated:YES];

showDetails:中,您可以将ann或其属性传递给详细信息 View 。


顺便说一句,您可以使用 map View 的 calloutAccessoryControlTapped 委托(delegate)方法,而不是在 viewForAnnotation 中使用 addTarget 调用自定义方法,这样可以更直接地访问被点击的注解。例如:

-(void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view 
    calloutAccessoryControlTapped:(UIControl *)control
{
    MapPin *ann = (MapPin *)view.annotation;

    if (control == view.rightCalloutAccessoryView)
    {
        NSLog(@"calloutAccessoryControlTapped: control=RIGHT");
        //show detail view (or you can call your custom method here)...
    }
    else
        if (control == view.leftCalloutAccessoryView)
        {
            NSLog(@"calloutAccessoryControlTapped: control=LEFT");
            //zoom in (or you can call your custom method here)...
        }
        else
        {
            NSLog(@"calloutAccessoryControlTapped: unknown control");
        }
}

如果您决定使用 calloutAccessoryControlTapped 委托(delegate)方法,请确保从 viewForAnnotation 中删除 addTarget 调用。

关于objective-c - 如何将 map 注释 View 按钮与数据库连接以转到另一个 View ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9097469/

相关文章:

ios - 无法滚动到 UITableView 的底部

ios - 在 NSUserDefaults 中保存继承的对象

ios - 当推送到详细 View Controller 时,对 UINavigationBar 内的 subview 进行动画处理

javascript - 使用 <img src =""onError =""> 在 MySQL 与 HTTP GET 中存储图像路径

c# - 使用 Visual FoxPro 编程/建立数据库

iphone - 将两个“for”语句合并为一个

objective-c - MKMapView 中的奇怪行为 - 引脚消失

iphone - iOS 如何加密和解密二进制文件?

ios - 运行代码时出错?

java - Hibernate createNativeQuery - 获取多个实体