下面是蘋果的官方途徑:
// Check for iOS 6
Class mapItemClass = [MKMapItem class];
if (mapItemClass && [mapItemClass respondsToSelector:@selector(openMapsWithItems:launchOptions:)])
{
// Create an MKMapItem to pass to the Maps app
CLLocationCoordinate2D coordinate =
CLLocationCoordinate2DMake(16.775, -3.009);
MKPlacemark *placemark = [[MKPlacemark alloc] initWithCoordinate:coordinate
addressDictionary:nil];
MKMapItem *mapItem = [[MKMapItem alloc] initWithPlacemark:placemark];
[mapItem setName:@"My Place"];
// Pass the map item to the Maps app
[mapItem openInMapsWithLaunchOptions:nil];
}
如果你想獲得駕車或步行指示的位置,可以包括在+openMapsWithItems:launchOptions:
數組中的MKMapItem
一個mapItemForCurrentLocation
,並設置啓動選項適當。
// Check for iOS 6
Class mapItemClass = [MKMapItem class];
if (mapItemClass && [mapItemClass respondsToSelector:@selector(openMapsWithItems:launchOptions:)])
{
// Create an MKMapItem to pass to the Maps app
CLLocationCoordinate2D coordinate =
CLLocationCoordinate2DMake(16.775, -3.009);
MKPlacemark *placemark = [[MKPlacemark alloc] initWithCoordinate:coordinate
addressDictionary:nil];
MKMapItem *mapItem = [[MKMapItem alloc] initWithPlacemark:placemark];
[mapItem setName:@"My Place"];
// Set the directions mode to "Walking"
// Can use MKLaunchOptionsDirectionsModeDriving instead
NSDictionary *launchOptions = @{MKLaunchOptionsDirectionsModeKey : MKLaunchOptionsDirectionsModeWalking};
// Get the "Current User Location" MKMapItem
MKMapItem *currentLocationMapItem = [MKMapItem mapItemForCurrentLocation];
// Pass the current location and destination map items to the Maps app
// Set the direction mode in the launchOptions dictionary
[MKMapItem openMapsWithItems:@[currentLocationMapItem, mapItem]
launchOptions:launchOptions];
}
時,可以保留原有的iOS 5和較低的代碼在if
後else
聲明。請注意,如果您將openMapsWithItems:
陣列中的項目順序顛倒過來,則會從您的當前位置獲取從座標到的方向。您可以通過傳遞構造的MKMapItem
而不是當前的位置圖項來使用它來獲取任意兩個位置之間的方向。我沒有嘗試過。
最後,如果您有要指示的地址(作爲字符串),請使用地址解析器創建MKPlacemark
,方式爲CLPlacemark
。
// Check for iOS 6
Class mapItemClass = [MKMapItem class];
if (mapItemClass && [mapItemClass respondsToSelector:@selector(openMapsWithItems:launchOptions:)])
{
CLGeocoder *geocoder = [[CLGeocoder alloc] init];
[geocoder geocodeAddressString:@"Piccadilly Circus, London, UK"
completionHandler:^(NSArray *placemarks, NSError *error) {
// Convert the CLPlacemark to an MKPlacemark
// Note: There's no error checking for a failed geocode
CLPlacemark *geocodedPlacemark = [placemarks objectAtIndex:0];
MKPlacemark *placemark = [[MKPlacemark alloc]
initWithCoordinate:geocodedPlacemark.location.coordinate
addressDictionary:geocodedPlacemark.addressDictionary];
// Create a map item for the geocoded address to pass to Maps app
MKMapItem *mapItem = [[MKMapItem alloc] initWithPlacemark:placemark];
[mapItem setName:geocodedPlacemark.name];
// Set the directions mode to "Driving"
// Can use MKLaunchOptionsDirectionsModeWalking instead
NSDictionary *launchOptions = @{MKLaunchOptionsDirectionsModeKey : MKLaunchOptionsDirectionsModeDriving};
// Get the "Current User Location" MKMapItem
MKMapItem *currentLocationMapItem = [MKMapItem mapItemForCurrentLocation];
// Pass the current location and destination map items to the Maps app
// Set the direction mode in the launchOptions dictionary
[MKMapItem openMapsWithItems:@[currentLocationMapItem, mapItem] launchOptions:launchOptions];
}];
}
有趣。如果您打開瀏覽器到maps.apple.com,它將重定向到maps.google.com。我想知道這會持續多久? – pir800
@ pir800 - 我實際上只是想知道如果在iOS 6的Safari上點擊指向maps.apple.com的鏈接會發生什麼。我試過了,當您點擊指向maps.google.com的鏈接時,它會以與之前的iOS版本相同的方式進入Google地圖。我認爲這是一個很好的選擇,他們將重定向到Google地圖,以便網站作者可以將地圖鏈接指向maps.apple.com,並使其在地圖上的iOS上工作,但在所有其他客戶端上正常工作。但我希望在將所有地圖鏈接更改爲maps.apple.com之前進行驗證! –
@ pir800 - 對我來說,在瀏覽器中打開http://maps.apple.com會帶我到http://www.apple.com/ios/maps/。也許我以前的評論是一廂情願的想法。 –