2015-09-24 23 views
9

Bu uygulamam var ve kullanıcı benim app uygulamasında bir düğmeye bastığında açmak için google haritalar veya elma haritaları kullanmak istiyorum. Bunu nasıl yapabilirim? Uygulamayı açan haritalara bir bağlantı var mı yoksa başka bir şey mi var? Beni doğru yönde işaret ederseniz gerçekten yardımcı olur. Teşekkürler! Ben düğme böyle aşağıda kurdunuz:Uygulamamdaki bir düğmeye bastığımda Google haritalarını nasıl açabilirim?

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.... 

    } 

cevap

46

bunu kullanın: Bir anahtarı eklemek gerekir: google URL şemasını here

Düzenleme eşler hakkında Sen daha fazla bilgi bulabilirsiniz

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) 
    } 
} 

Bunun çalışması için info.plist'a.

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

Düzenleme: Google Maps docs da yukarıda Plist için "googlechromes" eklendi güncellenen Başına. düğme tıklamasıyla

+10

Bu, iOS9 için çalışmayacaktır. Muhtemelen bu uyarıyı alacaksınız: "Bu uygulama şema comgooglemaps için sorgulanmasına izin verilmiyor". Şemalarınızı listelemeye de ihtiyacınız var LSApplicationQueriesSchemes –

+0

Bunun yerine başka bir yolu var mı? – coding22

+0

Bu hatayı alıyorum @LeoDabus, hatayı almadan başka bir yol olduğunu söyledi. – coding22

2

aşağıdaki kodla çağrı fonksiyonu "yönlerde()" (eylem olarak): LSApplicationQueriesSchemes için

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) 
    } 
} 

düzenleyin Info.plist:

enter image description here

Umut yardımcı olacaktır!