2015-09-24 17 views
9

Ho questa app e voglio usare google maps o apple maps per aprire quando l'utente preme un pulsante nella mia app. Come potrei essere in grado di fare questo? C'è un collegamento alle mappe che apre l'app o è qualcos'altro? Se riesci a indicarmi la giusta direzione, sarebbe molto utile. Grazie! Ho il tasto impostato di seguito in questo modo:Come potrei essere in grado di aprire google maps quando premo un pulsante nella mia app?

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

    } 

risposta

46

Utilizzare questa:

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

Si possono trovare maggiori informazioni sul google maps schema URL here

Edit: È necessario aggiungere una chiave al tuo info.plist affinché funzioni.

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

Edit: Per aggiornati Google Maps docs aggiunto "googlechromes" per plist sopra anche.

+10

Questo non funzionerà per iOS9. probabilmente riceverai questo avviso: "Questa app non può interrogare per schema comgooglemaps". Devi anche elencare i tuoi schemi LSApplicationQueriesSchemes –

+0

C'è un altro modo per farlo, invece? – coding22

+0

Ho capito che l'errore @LeoDabus ha detto che esiste un altro modo per farlo senza ottenere l'errore. – coding22

2

A Fare clic sul pulsante (in azione) funzione di chiamata "direzioni()" con codice seguente:

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

Modifica il tuo info.plist per LSApplicationQueriesSchemes:

enter image description here

Speranza vi aiuterà!

Problemi correlati