Pre iOS 6
Je moet Core Location gebruiken om de huidige locatie te krijgen, maar met die lengte / breedte pair, kunt u Maps route die u vanaf daar naar een adres of locatie. Als volgt:
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]];
Tot slot, als u wilt voorkomen dat u CoreLocation expliciet op zoek naar de huidige locatie, en willen het gebruik @"http://maps.google.com/maps?saddr=Current+Location&daddr=%@"url te veranderen, dan is deze link die ik ontvangen in opmerkingen hieronder voor informatie over de te lokaliseren Huidige + Location string. U bent echter gebruik te maken van een andere ongedocumenteerde functie, en als Jason McCreary opmerkt hieronder, kan het niet betrouwbaar werken in toekomstige versies.
Update voor iOS 6
Oorspronkelijk Maps gebruikt Google maps, maar nu, Apple en Google hebben een aparte maps apps.
1) Als u route met behulp van de Google Maps-app gebruiken de comgooglemaps URL-schema :
NSString* url = [NSString stringWithFormat: @"comgooglemaps://?daddr=%@&directionsmode=driving",
[address stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
BOOL opened = [[UIApplication sharedApplication] openURL: [NSURL URLWithString: url]];
2) Voor Apple Maps te gebruiken, kunt u de nieuwe gebruiken MKMapItemklasse voor iOS 6. Bekijk de Apple API docs hier
In principe zal je iets als dit gebruiken, als een andere route naar de bestemming coördinaten ( 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];
Met het oog op zowel iOS 6+ en pre iOS 6 te ondersteunen in de dezelfde code, zou ik adviseren het gebruik van zoiets als dit code die Apple heeft op de MKMapItemAPI doc pagina:
Class itemClass = [MKMapItem class];
if (itemClass && [itemClass respondsToSelector:@selector(openMapsWithItems:launchOptions:)]) {
// iOS 6 MKMapItem available
} else {
// use pre iOS 6 technique
}
Dit zou veronderstellen dat uw Xcode Base SDK is iOS 6 (of Laatste iOS ).