Pre iOS 6
Be kell használni Core Location, hogy a jelenlegi helyéről, de hogy lat / hosszú pár, akkor kap Maps útvonal onnan, hogy egy utcát vagy helyét. Így:
CLLocationCoordinate2D currentLocation = [self getCurrentLocation];
// this uses an address for the destination. can use lat/long, too with %f,%f format
NSString* address = @"123 Main St., New York, NY, 10001";
NSString* url = [NSString stringWithFormat: @"http://maps.google.com/maps?saddr=%f,%f&daddr=%@",
currentLocation.latitude, currentLocation.longitude,
[address stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
[[UIApplication sharedApplication] openURL: [NSURL URLWithString: url]];
Végül, ha nem akarjuk, hogy ne használja CoreLocation kifejezetten megtalálják a jelenlegi helyét, és szeretné használni a @"http://maps.google.com/maps?saddr=Current+Location&daddr=%@"url helyett, akkor látja ezt a linket, hogy én nyújtott alábbi megjegyzéseket , hogy hogyan lokalizálja a jelenlegi + Location húr. Azonban, ha kihasználják a másik dokumentált funkciót, és Jason McCreary rámutat alatti, akkor nem működik megbízhatóan a későbbi kiadások.
Frissítés az iOS 6
Eredetileg Maps használja a Google Maps, de most, az Apple és a Google is külön térképeket alkalmazásokat.
1) Ha szeretné, hogy az útvonalat a Google Maps alkalmazást, használja a comgooglemaps URL-séma :
NSString* url = [NSString stringWithFormat: @"comgooglemaps://?daddr=%@&directionsmode=driving",
[address stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
BOOL opened = [[UIApplication sharedApplication] openURL: [NSURL URLWithString: url]];
2) Ahhoz, hogy használni az Apple Maps, akkor használhatja az új MKMapItemosztály az iOS 6 Lásd az Apple API docs itt
Alapvetően, akkor használja valami ilyesmit, ha routing rendeltetési hely koordinátáit ( latlong):
MKPlacemark* place = [[MKPlacemark alloc] initWithCoordinate: latlong addressDictionary: nil];
MKMapItem* destination = [[MKMapItem alloc] initWithPlacemark: place];
destination.name = @"Name Here!";
NSArray* items = [[NSArray alloc] initWithObjects: destination, nil];
NSDictionary* options = [[NSDictionary alloc] initWithObjectsAndKeys:
MKLaunchOptionsDirectionsModeDriving,
MKLaunchOptionsDirectionsModeKey, nil];
[MKMapItem openMapsWithItems: items launchOptions: options];
Annak érdekében, hogy támogassa a két iOS 6+ és előre iOS 6 ugyanazt a kódot, javasoljuk, hogy használja valami ilyesmit kódot, hogy az Apple az MKMapItemAPI doc oldalon:
Class itemClass = [MKMapItem class];
if (itemClass && [itemClass respondsToSelector:@selector(openMapsWithItems:launchOptions:)]) {
// iOS 6 MKMapItem available
} else {
// use pre iOS 6 technique
}
Ez azt feltételezi, hogy az Xcode Base SDK iOS 6 (vagy legújabb iOS ).