地图视图中的方向问题

时间:2014-05-19 03:24:38

标签: ios objective-c mkmapview

我正在尝试为我在地图视图上标有地图图钉的某些区域提供路线。在那些引脚上,我显示了位置的名称和位置。我希望他们能够点击" i"这通常出现在一些Apple" pop up"它将它们引导到地图应用程序,并根据我编程的坐标给它们指示,将地图引脚放在原处。我将首先发布两张我已经拥有的和我想要完成的图片。

我现在拥有的

enter image description here

我想要添加到我的图钉(只是"我")

enter image description here

现在我将发布我的第一个截图的代码。

ViewController.h:

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

@interface ViewController : UIViewController
{
MKMapView *mapView;
}

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

-(IBAction)SetMap:(id)sender;

-(IBAction)GetLocation:(id)sender;

-(IBAction)Directions:(id)sender;

@end

ViewController.m:

#import "ViewController.h"
#import "MapPin.h"

@interface ViewController ()

@end

@implementation ViewController

@synthesize mapView;

- (void)viewDidLoad
{
[super viewDidLoad];

//Moss Preserve coordinates
MKCoordinateRegion MossPreserveRegion = { {0.0, 0.0}, {0.0, 0.0}};
MossPreserveRegion.center.latitude = 33.3816566;
MossPreserveRegion.center.longitude = -86.8415451;
MossPreserveRegion.span.longitudeDelta = 55.0;
MossPreserveRegion.span.latitudeDelta = 55.0;
[mapView setRegion:MossPreserveRegion animated:YES];

//Moss Preserve annotation and map pin
MapPin *MossPreserveAnnotation = [[MapPin alloc] init];
MossPreserveAnnotation.title = @"Moss Rock Preserve Boulder Fields";
MossPreserveAnnotation.subtitle = @"Hoover, AL";
MossPreserveAnnotation.coordinate = MossPreserveRegion.center;
[mapView addAnnotation:MossPreserveAnnotation];

//Horse Pens 40 coordinates
MKCoordinateRegion HorsePenRegion = { {0.0, 0.0}, {0.0, 0.0}};
HorsePenRegion.center.latitude = 33.9207535;
HorsePenRegion.center.longitude = -86.3089447;
HorsePenRegion.span.longitudeDelta = 55.0;
HorsePenRegion.span.latitudeDelta = 55.0;
[mapView setRegion:HorsePenRegion animated:YES];

//Horse Pens 40 annotation and map pin
MapPin *HorsePenAnnotation = [[MapPin alloc] init];
HorsePenAnnotation.title = @"Horse Pens 40";
HorsePenAnnotation.subtitle = @"Steele, AL ";
HorsePenAnnotation.coordinate = HorsePenRegion.center;
[mapView addAnnotation:HorsePenAnnotation];

// Create an MKMapItem to pass to the Maps app
MKPlacemark *placemark = [[MKPlacemark alloc]  initWithCoordinate:MossPreserveAnnotation.coordinate
                                               addressDictionary:nil];
MKMapItem *mossPreserveMapItem = [[MKMapItem alloc] initWithPlacemark:placemark];
[mossPreserveMapItem setName:MossPreserveAnnotation.title];

NSDictionary *launchOptions = @{MKLaunchOptionsDirectionsModeKey : MKLaunchOptionsDirectionsModeDriving};
// Get the "Current User Location" MKMapItem
placemark = [[MKPlacemark alloc] initWithCoordinate:HorsePenRegion.coordinate
                                  addressDictionary:nil];
MKMapItem *horsePenMapItem = [[MKMapItem alloc] initWithPlacemark:placemark];
horsePenmapItem.name = HorsePenRegion.title;

// Pass the current location and destination map items to the Maps app
// Set the direction mode in the launchOptions dictionary
[MKMapItem openMapsWithItems:@[mossPreserveMapItem, horsePenmapItem]
               launchOptions:launchOptions];

}

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

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

-(IBAction)GetLocation:(id)sender;
{
mapView.showsUserLocation = YES;
}

-(IBAction)Directions:(id)sender;
{
NSString *urlString = @"http://maps.apple.com/maps?daddr=33.3816566,-86.8415451";
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlString]];
}

@end

MapPin.h:

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

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

NSString *title;
NSString *subtitle;
}

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

@end

MapPin.m:

#import "MapPin.h"

@implementation MapPin

@synthesize coordinate, title, subtitle;

@end

我在使用的应用程序中始终看到这一点。我甚至不知道它叫什么,所以我甚至可以搜索它。我不是在找人牵着我的手告诉我答案,只是一些适当的指导和建设性的批评。

由于

2 个答案:

答案 0 :(得分:1)

您需要为您的位置创建2个地图项,并将其作为参数传递给MKMapItem的openMapsWithItems:launchOptions:

// Create an MKMapItem to pass to the Maps app
MKPlacemark *placemark = [[MKPlacemark alloc] initWithCoordinate:MossPreserveAnnotation.coordinate 
                                        addressDictionary:nil];
MKMapItem *mapItem1 = [[MKMapItem alloc] initWithPlacemark:placemark];
[mapItem setName:MossPreserveAnnotation.title];

NSDictionary *launchOptions = @{MKLaunchOptionsDirectionsModeKey : MKLaunchOptionsDirectionsModeDriving};
// Get the "Current User Location" MKMapItem
placemark = [[MKPlacemark alloc] initWithCoordinate:HorsePenAnnotation.coordinate 
                                        addressDictionary:nil];
MKMapItem *mapItem2 = [[MKMapItem alloc] initWithPlacemark:placemark];
mapItem2.name = HorsePenAnnotation.title;

// Pass the current location and destination map items to the Maps app
// Set the direction mode in the launchOptions dictionary
[MKMapItem openMapsWithItems:@[mapItem1, mapItem2] 
                launchOptions:launchOptions];

答案 1 :(得分:1)

这是你需要的吗?

#import "MapViewController.h"
#import "MapPin.h"
@import MapKit;

@interface MapViewController ()<MKMapViewDelegate>

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

@end

@implementation MapViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];

    _mapView.delegate = self;

    //Moss Preserve coordinates
    MKCoordinateRegion MossPreserveRegion = { {0.0, 0.0}, {0.0, 0.0}};
    MossPreserveRegion.center.latitude = 33.3816566;
    MossPreserveRegion.center.longitude = -86.8415451;
    MossPreserveRegion.span.longitudeDelta = 55.0;
    MossPreserveRegion.span.latitudeDelta = 55.0;
    [_mapView setRegion:MossPreserveRegion animated:YES];

    //Moss Preserve annotation and map pin
    MapPin *MossPreserveAnnotation = [[MapPin alloc] init];
    MossPreserveAnnotation.title = @"Moss Rock Preserve Boulder Fields";
    MossPreserveAnnotation.subtitle = @"Hoover, AL";
    MossPreserveAnnotation.coordinate = MossPreserveRegion.center;
    [_mapView addAnnotation:MossPreserveAnnotation];
}

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

- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control
{

        // Create an MKMapItem to pass to the Maps app
MKPlacemark *placemark = [[MKPlacemark alloc]  initWithCoordinate:view.annotation.coordinate
                                                addressDictionary:nil];
MKMapItem *mossPreserveMapItem = [[MKMapItem alloc] initWithPlacemark:placemark];
[mossPreserveMapItem setName:view.annotation.title];

NSDictionary *launchOptions = @{MKLaunchOptionsDirectionsModeKey : MKLaunchOptionsDirectionsModeDriving};

// Get the "Current User Location" MKMapItem
MKMapItem *currentLocationItem = [MKMapItem mapItemForCurrentLocation];
[MKMapItem openMapsWithItems:@[mossPreserveMapItem, currentLocationItem]
               launchOptions:launchOptions];
}

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation
{
    MKAnnotationView *annotationView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation
                                                                       reuseIdentifier:@"MKPinAnnotationView"];
    annotationView.canShowCallout = YES;

    UIButton *detailButton = [UIButton buttonWithType:UIButtonTypeInfoLight];
    annotationView.rightCalloutAccessoryView = detailButton;

    return annotationView;
}