2012-09-19 166 views
155

前到iOS 6,打開一個URL這樣會打開(谷歌)地圖應用:用新的蘋果地圖的實現在iOS的編程方式打開地圖應用6

NSURL *url = [NSURL URLWithString:@"http://maps.google.com/?q=New+York"]; 
[[UIApplication sharedApplication] openURL:url]; 

現在,這只是打開移動Safari瀏覽器谷歌地圖。我如何才能完成與iOS 6相同的行爲?如何以編程方式打開地圖應用並使其指向特定位置/地址/搜索/任何內容?

回答

79

找到了我自己的問題的答案。蘋果公司將其地圖URL格式文件here。看起來你可以基本上用maps.apple.com代替maps.google.com

更新:事實證明,MobileSafari在iOS 6上也是如此;點擊鏈接到http://maps.apple.com/?q=...用該搜索打開地圖應用程序,與之前版本中的http://maps.google.com/?q=...相同。這工作,並記錄在上面鏈接的頁面。

更新:這回答了我有關URL格式的問題。但是內文國王的回答here(見下文)是實際Maps API的極好摘要。

+1

有趣。如果您打開瀏覽器到maps.apple.com,它將重定向到maps.google.com。我想知道這會持續多久? – pir800

+0

@ pir800 - 我實際上只是想知道如果在iOS 6的Safari上點擊指向maps.apple.com的鏈接會發生什麼。我試過了,當您點擊指向maps.google.com的鏈接時,它會以與之前的iOS版本相同的方式進入Google地圖。我認爲這是一個很好的選擇,他們將重定向到Google地圖,以便網站作者可以將地圖鏈接指向maps.apple.com,並使其在地圖上的iOS上工作,但在所有其他客戶端上正常工作。但我希望在將所有地圖鏈接更改爲maps.apple.com之前進行驗證! –

+0

@ pir800 - 對我來說,在瀏覽器中打開http://maps.apple.com會帶我到http://www.apple.com/ios/maps/。也許我以前的評論是一廂情願的想法。 –

7

我看到你找到了maps.apple.com url「scheme」。這是一個不錯的選擇,因爲它會自動將舊設備重定向到maps.google.com。但對於iOS 6,您可能想要利用以下新類:MKMapItem

兩個方法,感興趣的你:

  1. -openInMapsWithLaunchOptions: - 稱之爲上MKMapItem比如在Maps.app
  2. 打開它
  3. +openMapsWithItems:launchOptions: - 調用它MKMapItem類打開MKMapItem數組實例。
+0

看起來很有用。但它似乎使用起來更復雜一點;你必須用'MKPlacemark'來初始化'MKMapItem',你可以通過提供一個緯度/經度對和一個地址字典來獲得它。似乎更簡單,只需打開http://maps.apple.com/?q = 1 +無限+循環+ cupertino + ca'。當你想做的只是在地圖中顯示地址時,使用'MKMapItem'是否有優勢? –

+0

如果您尚未引用您想要使用的引用,則不必初始化「MKMapItem」。只需在'MKMapItem'上使用類方法'+ openMapsWithItems:launchOptions:'來做同樣的事情。 –

+0

@MarkAdams @類方法需要一個類的實例數組,所以是的,他需要至少有一個初始化。 –

41

做到這一點,最好的方法是調用新的iOS上MKMapItemopenInMapsWithLaunchOptions:launchOptions

例6方法:

CLLocationCoordinate2D endingCoord = CLLocationCoordinate2DMake(40.446947, -102.047607); 
MKPlacemark *endLocation = [[MKPlacemark alloc] initWithCoordinate:endingCoord addressDictionary:nil]; 
MKMapItem *endingItem = [[MKMapItem alloc] initWithPlacemark:endLocation]; 

NSMutableDictionary *launchOptions = [[NSMutableDictionary alloc] init]; 
[launchOptions setObject:MKLaunchOptionsDirectionsModeDriving forKey:MKLaunchOptionsDirectionsModeKey]; 

[endingItem openInMapsWithLaunchOptions:launchOptions]; 

這將啓動導航從當前位置行駛。

+1

好吧,那麼獲取' MKATAItem'當我所有的是一個地址?這個API對於那個簡單的用例似乎有點複雜。 –

+0

MKPlacemark * endLocation = [[MKPlacemark alloc] initWithCoordinate:nil addressDictionary:yourAdDictDictHere]; 你可以在地址 – mariusLAN

+0

中遞交地址Dictonary爲我工作Xcode 6,iOS 8.0 – Jasmeet

4

我發現它很煩人,使用http://maps.apple.com?q= ...鏈接設置打開safari瀏覽器首先在較舊的設備。

所以對於iOS 5的設備打開你的應用程序有一個參考maps.apple.com的步驟是這樣的:

  1. 您單擊應用程序的東西,它是指maps.apple.com網址
  2. Safari瀏覽器打開該鏈接
  3. 的maps.apple.com服務器重定向到maps.google.com網址
  4. maps.google.com的URL被解釋並打開谷歌地圖應用。

我認爲(非常明顯和令人困惑的)第2步和第3步對用戶來說很煩人。 因此,我檢查os版本,並在設備上運行maps.google.com或maps.apple.com(用於響應ios 5或ios 6操作系統版本)。

+0

這就是我所做的。似乎是最好的方法。 –

3

我對這個問題的研究使我得出以下結論:

  1. 如果使用maps.google.com,然後它會在Safari中打開地圖爲每部iOS。
  2. 如果您使用maps.apple.com,那麼它將在ios 6的地圖應用程序中打開地圖,並且還會與ios 5一起工作,並在ios 5中以Safari瀏覽器的正常方式打開地圖。
282

下面是蘋果的官方途徑:

// 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和較低的代碼在ifelse聲明。請注意,如果您將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]; 

    }]; 
} 
+0

該代碼適用於iOS 6。值得一提的是,如果我們想要的路由是從當前用戶位置到目的地,那麼不需要傳入'currentLocationMapItem'。 – Philip007

+1

事實上,您使用Tombouctou座標只是使答案更好:) – sachadso

+0

+1提及它只是iOS 6並且需要回退 –

2
NSString *address = [NSString stringWithFormat:@"%@ %@ %@ %@" 
          ,[dataDictionary objectForKey:@"practice_address"] 
          ,[dataDictionary objectForKey:@"practice_city"] 
          ,[dataDictionary objectForKey:@"practice_state"] 
          ,[dataDictionary objectForKey:@"practice_zipcode"]]; 


     NSString *mapAddress = [@"http://maps.apple.com/?q=" stringByAppendingString:[address stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]; 

     NSLog(@"Map Address %@",mapAddress); 

     [objSpineCustomProtocol setUserDefaults:mapAddress :@"webSiteToLoad"]; 

     [self performSegueWithIdentifier: @"provider_to_web_loader_segue" sender: self]; 

// VKJ

5

下面是使用妮娃王的溶液中,斯威夫特完成了一個類:

class func openMapWithCoordinates(theLon:String, theLat:String){ 

      var coordinate = CLLocationCoordinate2DMake(CLLocationDegrees(theLon), CLLocationDegrees(theLat)) 

      var placemark:MKPlacemark = MKPlacemark(coordinate: coordinate, addressDictionary:nil) 

      var mapItem:MKMapItem = MKMapItem(placemark: placemark) 

      mapItem.name = "Target location" 

      let launchOptions:NSDictionary = NSDictionary(object: MKLaunchOptionsDirectionsModeDriving, forKey: MKLaunchOptionsDirectionsModeKey) 

      var currentLocationMapItem:MKMapItem = MKMapItem.mapItemForCurrentLocation() 

      MKMapItem.openMapsWithItems([currentLocationMapItem, mapItem], launchOptions: launchOptions) 
} 
3

如果你想打開谷歌地圖,而不是(或提供作爲輔助選項),您可以使用comgooglemaps://comgooglemaps-x-callback:// URL scheme scheme here

1

不使用地圖,只是以編程方式使用UiButton動作,這對我很好。

// Button triggers the map to be presented. 

@IBAction func toMapButton(sender: AnyObject) { 

//Empty container for the value 

var addressToLinkTo = "" 

//Fill the container with an address 

self.addressToLinkTo = "http://maps.apple.com/?q=111 Some place drive, Oak Ridge TN 37830" 

self.addressToLinkTo = self.addressToLinkTo.stringByAddingPercentEscapesUsingEncoding(NSUTF8StringEncoding)! 

let url = NSURL(string: self.addressToLinkTo) 
UIApplication.sharedApplication().openURL(url!) 

       } 

你可能會傳播一些這些代碼。例如,我把變量作爲一個類變量,用另一個函數來填充它,然後當按下按鈕時,只需將變量中的內容填入變量中,然後將其用於URL中。

3

在啓動url之前,從url中刪除任何特殊字符並用+替換空格。這將節省你有些頭疼:

NSString *mapURLStr = [NSString stringWithFormat: @"http://maps.apple.com/?q=%@",@"Limmattalstrasse 170, 8049 Zürich"]; 

    mapURLStr = [mapURLStr stringByReplacingOccurrencesOfString:@" " withString:@"+"]; 
    NSURL *url = [NSURL URLWithString:[mapURLStr stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding]]; 
    if ([[UIApplication sharedApplication] canOpenURL:url]){ 
      [[UIApplication sharedApplication] openURL:url]; 
     } 
1

更新斯威夫特4基於@ PJeremyMalouf的回答是:

private func navigateUsingAppleMaps(to coords:CLLocation, locationName: String? = nil) { 
    let placemark = MKPlacemark(coordinate: coords.coordinate, addressDictionary:nil) 
    let mapItem = MKMapItem(placemark: placemark) 
    mapItem.name = locationName 
    let launchOptions = [MKLaunchOptionsDirectionsModeKey: MKLaunchOptionsDirectionsModeDriving] 
    let currentLocationMapItem = MKMapItem.forCurrentLocation() 

    MKMapItem.openMaps(with: [currentLocationMapItem, mapItem], launchOptions: launchOptions) 
} 
相關問題