2012-09-21 22 views

回答

4

我看着上週做了這件事,並沒有找到辦法。看起來你可以給一個目的地,你可以給它的不僅僅是座標,但它總是假設你的起始位置是當前位置。當你目前不在目的地的時候,你可能正計劃一次旅行,這是限制性的。 (但也許我只是沒有看到它是怎麼做的,我希望有人可以糾正我,如果這是真的。)

前陣子我看着路由選項的iOS 6和這裏聚集的結果...

How would you providing routing for directions between points on a map? What are the missing pieces?

你仍然可能無法打開蘋果的地圖與你想要的確切路徑,但也許你可以借鑑自己的實例的MKMapView與疊加和註解的路線。這可能是你現在可以做的最好的。

以下是我用於路由到某個位置併爲目的地提供至少一個標籤而不是僅將其留給座標的代碼。我發現僅僅給目的地提供完整地址詳細信息的標籤不起作用,所以我只提供了一個值。

if (flag != DirectionsFlag_PublicTransit && itemClass && [itemClass respondsToSelector:@selector(openMapsWithItems:launchOptions:)]) { 
    NSDictionary *address = @{ (NSString *)kABPersonAddressStreetKey : location.title }; 
    MKPlacemark *destinationPlacemark = [[MKPlacemark alloc] initWithCoordinate:location.coordinate addressDictionary:address]; 

    MKMapItem *destinationMapItem = [[MKMapItem alloc] initWithPlacemark:destinationPlacemark]; 
    if (flag == DirectionsFlag_Driving) { 
     [destinationMapItem openInMapsWithLaunchOptions:@{MKLaunchOptionsDirectionsModeKey:MKLaunchOptionsDirectionsModeDriving}]; 
    } 
    else if (flag == DirectionsFlag_Walking) { 
     [destinationMapItem openInMapsWithLaunchOptions:@{MKLaunchOptionsDirectionsModeKey:MKLaunchOptionsDirectionsModeWalking}]; 
    } 
} 

此代碼特別不處理公共交通方向,因爲Apple地圖不這樣做。我反而用它之前使用的URL打開Goog​​le Maps,現在可以在這些方向上打開Safari。該標誌是駕駛,步行或公共交通的枚舉值。位置是一個模型,其中包含各種細節,包括標題和座標。

+2

MKMapItem :: openMapsWithItems中的文檔說:「如果您在launchOptions字典中指定MKLaunchOptionsDirectionsModeKey選項,則mapItems數組中必須只有兩個項目,如果該數組包含一個項目,則地圖應用程序將生成路線從用戶的當前位置到地圖項指定的位置,如果數組包含兩個項目,則地圖應用程序將生成從第一個項目的位置到第二個項目在陣列中的位置的路線。因此,如果您將兩組位置傳遞給openInMapsWithLaunchOptions,它應該可以工作。 –

+0

對不起,我不能給示例代碼,但Objective-C的語法只是讓我感到困惑(我是一個Monotouch/C#人)。 –

相關問題