0

I am trying to direct my users to iOS 6 native maps for directions from their current location to a latitude, longitude location as follows:

MKPlacemark *placemark = [[MKPlacemark alloc] initWithCoordinate:placeLocation addressDictionary:nil];
MKMapItem *destination = [[MKMapItem alloc] initWithPlacemark:placemark];
MKMapItem *currentLocation = [MKMapItem mapItemForCurrentLocation];
NSArray *mapItems = @[destination, currentLocation];
NSDictionary *options = @{MKLaunchOptionsDirectionsModeKey:MKLaunchOptionsDirectionsModeDriving, MKLaunchOptionsMapTypeKey: [NSNumber numberWithInteger:MKMapTypeStandard], MKLaunchOptionsShowsTrafficKey:@YES};
[MKMapItem openMapsWithItems:mapItems launchOptions:options];

I have set my simulator to my latitude and longitude. What is wrong with my code here?

4

1 回答 1

1

the code is fine and works for me GIVEN there is a route possible

works when directions between currentLocation and destination can be given. if currentLocation is in the US, maps fails because it can't navigate from germany to the us. but if the currentLocation is in europe too, the maps app works fine :)

//placeMarkCoord fixed to germany
MKPlacemark *placemark = [[MKPlacemark alloc] initWithCoordinate:CLLocationCoordinate2DMake(51.0, 10.0) addressDictionary:nil];
MKMapItem *destination = [[MKMapItem alloc] initWithPlacemark:placemark];

//currentLocation must be reachable from destination!
//MKMapItem *currentLocation = [MKMapItem mapItemForCurrentLocation];
placemark = [[MKPlacemark alloc] initWithCoordinate:CLLocationCoordinate2DMake(51.0, 11.0) addressDictionary:nil];
MKMapItem *currentLocation = [[MKMapItem alloc] initWithPlacemark:placemark];

NSArray *mapItems = @[destination, currentLocation];
NSDictionary *options = @{MKLaunchOptionsDirectionsModeKey:MKLaunchOptionsDirectionsModeDriving, MKLaunchOptionsMapTypeKey: [NSNumber numberWithInteger:MKMapTypeStandard], MKLaunchOptionsShowsTrafficKey:@YES};
[MKMapItem openMapsWithItems:mapItems launchOptions:options];
于 2013-01-13T10:47:32.693 回答