2015-09-24 20 views
9

私はこのアプリを持っており、ユーザーが自分のアプリのボタンを押したときにGoogleマップやリンゴマップを使用して開きたいと考えています。どのように私はこれを行うことができるでしょうか?アプリを開く地図へのリンクのようなものか、それとも別のものですか?あなたが正しい方向に私を指すことができるなら、それは本当に役に立つでしょう。ありがとう!アプリでボタンを押すと、Googleマップを開くことはできますか?

override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?) { 

    for touch in (touches) { 
    let location = touch.locationInNode(self) 
    let node = self.nodeAtPoint(location) 

    if node.name == "openMaps" { 

    //code to open google maps or apple maps.... 

    } 

答えて

46

使用この:

if node.name == "openMaps" { 
    let customURL = "comgooglemaps://" 

    if UIApplication.sharedApplication().canOpenURL(NSURL(string: customURL)) { 
     UIApplication.sharedApplication().openURL(NSURL(string: customURL)) 
    } 
    else { 
     var alert = UIAlertController(title: "Error", message: "Google maps not installed", preferredStyle: UIAlertControllerStyle.Alert) 
     var ok = UIAlertAction(title: "OK", style: UIAlertActionStyle.Default, handler: nil) 
     alert.addAction(ok) 
     self.presentViewController(alert, animated:true, completion: nil) 
    } 
} 

GoogleマップURLスキームについての詳細情報here

編集を見つけることができます:あなたは、キーを追加する必要があります私はこのような下の設定ボタンがありますこれが動作するにはinfo.plistにお問い合わせください。

<key>LSApplicationQueriesSchemes</key> 
<array> 
    <string>googlechromes</string> 
    <string>comgooglemaps</string> 
</array> 

編集:Google Maps docsも上PLISTする "googlechromes" を追加、更新ごと。次のコードでボタンのクリック(アクションで)コール機能 "方向()" で

+10

これはiOS9では機能しません。 「このアプリはスキームcomgooglemapsを照会することはできません」という警告メッセージが表示されます。あなたはあなたのスキームをリストする必要もありますLSApplicationQueriesSchemes –

+0

代わりに別の方法がありますか? – coding22

+0

私はそのエラーを取得@LeoDabusは、エラーを取得せずにそれを行う別の方法があると述べた。 – coding22

2

func directions() { 
    // if GoogleMap installed 
    if (UIApplication.shared.canOpenURL(URL(string:"comgooglemaps://")!)) { 
     UIApplication.shared.openURL(NSURL(string: 
      "comgooglemaps://?saddr=&daddr=\(Float(event.addressLat)!),\(Float(event.addressLong)!)&directionsmode=driving")! as URL) 

    } else { 
     // if GoogleMap App is not installed 
     UIApplication.shared.openURL(NSURL(string: 
      "https://maps.google.com/[email protected]\(Float(event.addressLat)!),\(Float(event.addressLong)!)")! as URL) 
    } 
} 

編集LSApplicationQueriesSchemesのためのあなたのInfo.plist:

enter image description here

希望は助けます!

関連する問題