使用mapPin按钮Xcode打开一个新视图

时间:2016-11-20 12:14:02

标签: ios objective-c iphone mkmapview

我正在开发Xcode中的应用程序(objective-c)。我的应用程序有一个带有餐馆列表的TableView,当您按下一行时,会打开另一个带有餐厅信息的视图。我正在使用的方法是我将行中的标题发送到新视图,并根据标题加载餐馆的信息。 我想使用地图引脚按钮完全相同。我有一个带有别针的地图,我想按右侧的详细信息按钮打开另一个带有餐馆信息的视图,具体取决于mapPin标题。

有人可以帮帮我!我正在寻找教程和其他帖子,但它在我的案例中不起作用。非常感谢你!

这是我的MapViewController.h:

#import <UIKit/UIKit.h>
#import <MapKit/MapKit.h>

@interface MapViewController : UIViewController {

    MKMapView *mapView;
}

@property (weak, nonatomic) IBOutlet UIBarButtonItem *barButton;

@property (nonatomic, retain) IBOutlet MKMapView *mapView;

-(IBAction)setMap:(id)sender;

@end

这是我的MapViewController.m:

#import "MapViewController.h"
#import "SWRevealViewController.h"
#import "RestMapPin.h"
#import "RestViewController.h"

@interface MapViewController ()

@end

@implementation MapViewController

@synthesize mapView;

- (void)viewDidLoad {
    [super viewDidLoad];

    _barButton.target = self.revealViewController;
    _barButton.action = @selector(revealToggle:);

    [self.view addGestureRecognizer:self.revealViewController.panGestureRecognizer];

    [self.navigationItem setTitle:NSLocalizedString (@"Map", nil)]; /*Cambia el titulo del navigation controller*/

    [self.navigationController.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName : [UIColor whiteColor]}]; /*Cambia el color de las letras del navigation controller bar del menu principal*/

    [self.navigationController.navigationBar setBarTintColor:[UIColor colorWithRed:27/255.0f green:101/255.0f blue:163/255.0f alpha:1.0f]];

    self.navigationController.navigationBar.tintColor = [UIColor whiteColor]; /*Cambia el color del boton de la izquierda*/

    mapView.delegate = self;

    //1
    //80 Grados
    MKCoordinateRegion region_80_grados = { {0.0, 0.0}, {0.0, 0.0}};
    region_80_grados.center.latitude = 40.42871179999999;
    region_80_grados.center.longitude =  -3.703639100000032;
    region_80_grados.span.longitudeDelta = 0.1f;
    region_80_grados.span.latitudeDelta = 0.1f;
    [mapView setRegion:region_80_grados animated:YES];

    RestMapPin *ann_80_grados = [[RestMapPin alloc] init];
    ann_80_grados.title = @"80 Grados";
    ann_80_grados.subtitle = @"Malasaña";
    ann_80_grados.coordinate = region_80_grados.center;
    [mapView addAnnotation:ann_80_grados];

    //90 Grados
    MKCoordinateRegion region_90_grados = { {0.0, 0.0}, {0.0, 0.0}};
    region_90_grados.center.latitude = 40.4164161;
    region_90_grados.center.longitude =  -3.6699459999999817;
    region_90_grados.span.longitudeDelta = 0.1f;
    region_90_grados.span.latitudeDelta = 0.1f;
    [mapView setRegion:region_90_grados animated:YES];

    RestMapPin *ann_90_grados = [[RestMapPin alloc] init];
    ann_90_grados.title = @"90 Grados";
    ann_90_grados.subtitle = @"Retiro";
    ann_90_grados.coordinate = region_90_grados.center;
    [mapView addAnnotation:ann_90_grados];

    /*B&B Babel*/
    MKCoordinateRegion region_babel = { {0.0, 0.0}, {0.0, 0.0}};
    region_babel.center.latitude = 40.4214535;
    region_babel.center.longitude =  -3.6974301;
    region_babel.span.longitudeDelta = 0.1f;
    region_babel.span.latitudeDelta = 0.1f;
    [mapView setRegion:region_babel animated:YES];

    RestMapPin *ann_babel = [[RestMapPin alloc] init];
    ann_babel.title = @"B&B Babel";
    ann_babel.subtitle = @"Barrio de Chueca";
    ann_babel.coordinate = region_babel.center;
    [mapView addAnnotation:ann_babel];

    /*Babelia*/
    MKCoordinateRegion region_Babelia = { {0.0, 0.0}, {0.0, 0.0}};
    region_Babelia.center.latitude = 40.4234778;
    region_Babelia.center.longitude =  -3.686283000000003;
    region_Babelia.span.longitudeDelta = 0.1f;
    region_Babelia.span.latitudeDelta = 0.1f;
    [mapView setRegion:region_Babelia animated:YES];

    RestMapPin *ann_Babelia = [[RestMapPin alloc] init];
    ann_Babelia.title = @"Babelia";
    ann_Babelia.subtitle = @"Barrio de Salamanca";
    ann_Babelia.coordinate = region_Babelia.center;
    [mapView addAnnotation:ann_Babelia];

    /*Bacira*/
    MKCoordinateRegion region_Bacira = { {0.0, 0.0}, {0.0, 0.0}};
    region_Bacira.center.latitude = 40.43375390000001;
    region_Bacira.center.longitude =  -3.699036299999989;
    region_Bacira.span.longitudeDelta = 0.1f;
    region_Bacira.span.latitudeDelta = 0.1f;
    [mapView setRegion:region_Bacira animated:YES];

    RestMapPin *ann_Bacira = [[RestMapPin alloc] init];
    ann_Bacira.title = @"Bacira";
    ann_Bacira.subtitle = @"Chamberí";
    ann_Bacira.coordinate = region_Bacira.center;
    [mapView addAnnotation:ann_Bacira];

    /*Bar Galleta*/
    MKCoordinateRegion region_bar_galleta = { {0.0, 0.0}, {0.0, 0.0}};
    region_bar_galleta.center.latitude = 40.4227336;
    region_bar_galleta.center.longitude =  -3.7036699999999882;
    region_bar_galleta.span.longitudeDelta = 0.1f;
    region_bar_galleta.span.latitudeDelta = 0.1f;
    [mapView setRegion:region_bar_galleta animated:YES];

    RestMapPin *ann_bar_galleta = [[RestMapPin alloc] init];
    ann_bar_galleta.title = @"Bar Galleta";
    ann_bar_galleta.subtitle = @"Malasaña";
    ann_bar_galleta.coordinate = region_bar_galleta.center;
    [mapView addAnnotation:ann_bar_galleta];

    /*Bar Tomate*/
    MKCoordinateRegion region_bar_tomate = { {0.0, 0.0}, {0.0, 0.0}};
    region_bar_tomate.center.latitude = 40.428041;
    region_bar_tomate.center.longitude =  -3.69047;
    region_bar_tomate.span.longitudeDelta = 0.1f;
    region_bar_tomate.span.latitudeDelta = 0.1f;
    [mapView setRegion:region_bar_tomate animated:YES];

    RestMapPin *ann_bar_tomate = [[RestMapPin alloc] init];
    ann_bar_tomate.title = @"Bar Tomate";
    ann_bar_tomate.subtitle = @"Chamberí";
    ann_bar_tomate.coordinate = region_bar_tomate.center;
    [mapView addAnnotation:ann_bar_tomate];




    /*Indicador de posicion del mapa (para centrarlo)*/
    MKCoordinateRegion region_posicion = { {0.0, 0.0}, {0.0, 0.0}};
    region_posicion.center.latitude = 40.44934744420573;
    region_posicion.center.longitude =  -3.695504665374756;
    region_posicion.span.longitudeDelta = 0.08f;
    region_posicion.span.latitudeDelta = 0.08f;
    [mapView setRegion:region_posicion animated:YES];
    /*************************************************/


}

-(MKAnnotationView *)mapView:(MKMapView *)mV viewForAnnotation:(id <MKAnnotation>)annotation
{
    MKAnnotationView *pinView = nil;
    if(annotation != mapView.userLocation) {
        static NSString *defaultPinID = @"com.invasivecode.pin";
        pinView = (MKAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:defaultPinID];
        if ( pinView == nil )
            pinView = [[MKAnnotationView alloc]
                       initWithAnnotation:annotation reuseIdentifier:defaultPinID];


        pinView.canShowCallout = YES;
        pinView.image = [UIImage imageNamed:@"pin2@2x.png"];
    }
    else {
        //[mapView.userLocation setTitle:@"I am here"];
    }


    UIButton *pinButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
    if ([[annotation title] isEqualToString:@"Bacira"]) {

    }
    pinView.rightCalloutAccessoryView = pinButton;


    return pinView;
}

-(IBAction)setMap:(id)sender {

    switch (((UISegmentedControl *) sender).selectedSegmentIndex) {
        case 0:
            mapView.mapType = MKMapTypeStandard;
            break;
        case 1:
            mapView.mapType = MKMapTypeSatellite;
            break;
        case 2:
            mapView.mapType = MKMapTypeHybrid;
            break;
        default:
            break;
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

这是我的RestMapPin.h:

#import <Foundation/Foundation.h>
#import <MapKit/MapKit.h>

@interface RestMapPin : NSObject <MKAnnotation> {

    CLLocationCoordinate2D coordinate;
    NSString *title;
    NSString *subtitle;
}

@property (nonatomic, assign) CLLocationCoordinate2D coordinate;
@property (nonatomic, copy) NSString *title;
@property (nonatomic, copy) NSString *subtitle;

@end

这是我的RestMapPin.m:

#import "RestMapPin.h"

@implementation RestMapPin

@synthesize  coordinate, title, subtitle;

@end

最后,这是餐馆信息出现的视图:RestViewController.h:

#import <UIKit/UIKit.h>
#import "Restaurant.h"

@interface RestViewController : UIViewController

@property (strong, nonatomic) IBOutlet UILabel *TitleLabel;
@property (strong, nonatomic) IBOutlet UILabel *DescriptionLabel;
@property (strong, nonatomic) IBOutlet UIImageView *ImageView;

@property (nonatomic, strong) Restaurant *DetailModal;

@end

我知道我必须修改我的viewForAnnotation方法,但我不知道该怎么做。比你非常感谢你的帮助!

3 个答案:

答案 0 :(得分:1)

将此委托方法用于右侧详细信息按钮

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

    YourViewController *objYourVC= [[YourViewController alloc]init];
    objYourVC.title = annView.title;
    objYourVC.subtitle = annView. subtitle;

    [self.navigationController pushViewController:objYourVC animated:YES];
}

答案 1 :(得分:0)

正如另一张海报所说,使用地图视图委托方法mapView:viewCalloutAccessoryControlTapped:

我会反对使用注释的标题来查找餐厅。您不应使用显示字符串索引数据。这意味着如果您更改显示字符串或本地化其他语言,您的查找将不再有效。

我会在您的注释中添加ID字段并使用它。

答案 2 :(得分:0)

你需要这样做

- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control {
       RestMapPin *selectedAnnotation = view.annotation;
       NSLog("Title of selected pin - %@", selectedAnnotation.title);
       // Here you get the title of selected annotation
}

现在获取所选注释的标题后,您需要创建RestViewController的对象并在该控制器的变量中传递标题,与用户从列表中选择任何餐馆时的操作相同。

另外,正如@Duncan C在他的回答中提到的那样,你不应该使用String来从服务器获取数据,可以改变字符串。