Pre iOS 6
Sie müssen Core-Standort verwenden, um den aktuellen Standort zu erhalten, aber mit diesem lat / long Paar, können Sie Karten Route, die Sie von dort zu bekommen, zu einer Adresse oder Standort. Wie so:
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]];
Schließlich, wenn Sie mit Corelocation vermeiden möchten ausdrücklich den aktuellen Standort zu finden, und wollen die verwenden @"http://maps.google.com/maps?saddr=Current+Location&daddr=%@"url statt, dann sehen Sie diesen Link , die ich weiter unten in den Kommentaren versehen , wie Sie den lokalisieren Aktuelle + Location String. Sie sind jedoch die Vorteile eines anderen nicht dokumentierte Funktion nehmen, und als Jason McCreary unten weist darauf hin, es kann nicht zuverlässig in zukünftigen Versionen funktionieren.
Update für iOS 6
Ursprünglich Karten verwendet Google Maps, aber jetzt, Apple und Google haben separate Karten - Anwendungen.
1) Wenn Sie eine Route erstellen möchten die Google Maps App verwenden, verwenden Sie die comgooglemaps URL - Schema :
NSString* url = [NSString stringWithFormat: @"comgooglemaps://?daddr=%@&directionsmode=driving",
[address stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
BOOL opened = [[UIApplication sharedApplication] openURL: [NSURL URLWithString: url]];
2) Zur Verwendung von Apple Maps können Sie die neue verwenden MKMapItemKlasse für iOS 6. Sehen Sie sich die Apple - API - Dokumentation hier
Grundsätzlich werden Sie so etwas wie dieses verwenden, wenn das Routing zu Zielkoordinaten ( 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];
Um sowohl iOS 6+ und vor iOS 6 im gleichen Code zu unterstützen, würde ich mit so etwas wie diesem Code empfehlen , dass Apple auf die hat MKMapItemAPI doc Seite:
Class itemClass = [MKMapItem class];
if (itemClass && [itemClass respondsToSelector:@selector(openMapsWithItems:launchOptions:)]) {
// iOS 6 MKMapItem available
} else {
// use pre iOS 6 technique
}
Dies würde davon ausgehen , dass Ihr Xcode Base - SDK iOS 6 (oder ist Neueste iOS ).