如何将地图注释视图按钮与数据库连接到另一个视图? [英] How to connect map annotation view buttons with database to go to another view?

查看:149
本文介绍了如何将地图注释视图按钮与数据库连接到另一个视图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

   - (MKAnnotationView *)mapView:(MKMapView *)theMapView viewForAnnotation:(id< MKAnnotation>)注释
{
//如果它是用户位置, return nil
if([annotation isKindOfClass:[MKUserLocation class]])
return nil;

//尝试先将现有的pin视图出队
static NSString * AnnotationIdentifier = @AnnotationIdentifier;
MKPinAnnotationView * pinView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:AnnotationIdentifier];
pinView.animatesDrop = YES;
pinView.canShowCallout = YES;
pinView.pinColor = MKPinAnnotationColorRed;

//右边的按钮用于弹出菜单
UIButton * rightButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
[rightButton setTitle:annotation.title forState:UIControlStateNormal];
[rightButton addTarget:self
action:@selector(showDetails :) forControlEvents:UIControlEventTouchUpInside];
pinView.rightCalloutAccessoryView = rightButton;

//弹出框左侧的缩放按钮
UIButton * leftButton = [UIButton buttonWithType:UIButtonTypeContactAdd];
[leftButton setTitle:annotation.title forState:UIControlStateNormal];
[leftButton addTarget:self
action:@selector(zoomToLocation :) forControlEvents:UIControlEventTouchUpInside];
pinView.leftCalloutAccessoryView = leftButton;

return pinView;
}

//地图视图注释右键
- (void)showDetails:(id)sender {
NSLog(@Annotation Click);
// fypAppDelegate * appDelegate =(fypAppDelegate *)[[UIApplication sharedApplication] delegate];
//景点* attraction =(景点*)[appDelegate.attractions objectAtIndex:sender];

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

//用于地图视图注释left button
- (void)zoomToLocation:(id)sender {
NSLog(@Annotation Click);
}

上面是地图注释的委托。我能够显示引脚和显示地图注释视图,但我不知道如何链接按钮事件到下一个视图(infoViewController)。
所以,你们可以看到,右边的按钮是我想要使用的一个,使用户可以查看关于那个地方的更多信息,而左边的按钮,我想允许用户放大到该pin的坐标。



数据来自我创建的数据库。下面是我做的只是为了参考(如果你们可能需要它)

   - (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 Attraction * attraction =(Attraction *)[appDelegate.attractions objectAtIndex:i];
i ++;

//为引脚设置坐标
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米垂直跨度,500米水平跨度
动画:YES];

showDetails: code> ann 或其属性到详细视图。





顺便说一下,在 viewForAnnotation 中使用 addTarget 的自定义方法,可以使用地图视图的 calloutAccessoryControlTapped delegate方法,可以更直接地访问被点击的注释。例如:

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

if(control == view.rightCalloutAccessoryView)
{
NSLog(@calloutAccessoryControlTapped:control = RIGHT);
//显示详细视图(或者你可以在这里调用你的自定义方法)...
}
else
if(control == view.leftCalloutAccessoryView)
{
NSLog(@calloutAccessoryControlTapped:control = LEFT);
//放大(或者你可以在这里调用你的自定义方法)...
}
else
{
NSLog(@calloutAccessoryControlTapped:unknown control) ;
}
}

确保您删除如果您决定使用 calloutAccessoryControlTapped 委托方法,则 viewForAnnotation >

- (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");
}

Above is the delegate for the map annotations. I am able to show the pins and show the map annotation view but I don't know how to link the button events to the next view (infoViewController). So as you guys can see, the right button is the one I want to use to enable user to view more information about that place while the left button, I want to allow user to zoom in into the coordinates of that pin.

The data are from the database I've created. Below is how I did it just for reference (in case you guys might need it)

-(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];
}

please do tell me if you guys need more details. Thank you! =)

解决方案

In your showDetails: and zoomToLocation: methods, you can get a reference to the annotation whose callout button was tapped by doing the following:

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


In zoomToLocation: you can then zoom in to that annotation using:

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

In showDetails:, you can pass ann or its properties to the detail view.


By the way, instead of calling custom methods using addTarget in viewForAnnotation, you can use the map view's calloutAccessoryControlTapped delegate method which gives more direct access to the annotation that was tapped. For example:

-(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");
        }
}

Make sure you remove the addTarget calls from viewForAnnotation if you decide to use the calloutAccessoryControlTapped delegate method.

这篇关于如何将地图注释视图按钮与数据库连接到另一个视图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆