Reputation: 9394
I am using this code to open Google maps in Swift and it is not working.
if (UIApplication.sharedApplication().canOpenURL(NSURL(string:"comgooglemaps://")!)) {
UIApplication.sharedApplication().openURL(NSURL(string:
"comgooglemaps://?center=40.765819,-73.975866&zoom=14&views=traffic")!)
} else {
print("Can't use comgooglemaps://");
}
It always shows the message "Can't use comgooglemaps://"
Can any one tell me what is the problem?
EDIT
info.plist file
<key>LSApplicationQueriesSchemes</key>
<array>
<string>comgooglemaps</string>
<string>googlechromes</string>
<string>googlephotos</string>
<string>telprompt</string>
<string>tel</string>
<string>http</string>
<string>https</string>
</array>
EDIT2
let url1:NSURL = NSURL(string:"comgooglemaps://")!;
let url2:NSURL = NSURL(string:"comgooglemaps://?saddr=&daddr=\(self.selectedItem.latitude),\(self.selectedItem.longitude)&directionsmode=driving")!;
let url3:NSURL = NSURL(string:"https://www.google.com/maps?saddr=&daddr=\(self.selectedItem.latitude),\(self.selectedItem.longitude)&directionsmode=driving")!;
if (UIApplication.sharedApplication().canOpenURL(url1))
{
UIApplication.sharedApplication().openURL(url2)
} else {
UIApplication.sharedApplication().openURL(url3)
}
Upvotes: 1
Views: 1300
Reputation: 4855
Try this
private var latitude = 30.7333
private var longitude = 76.7794
/// Open Maps Action
@IBAction func openMapsAction(_ sender: Any) {
self.openGoogleMaps()
}
//MARK: Open Google maps
func openGoogleMaps(){
// Open in Google Maps
if (UIApplication.shared.canOpenURL(NSURL(string:"comgooglemaps://")! as URL))
{
/// Driving google map
UIApplication.shared.open(URL.init(string: "comgooglemaps://?saddr=&daddr=\(self.latitude),\(self.longitude)&directionsmode=driving")!, options: [:], completionHandler: nil)
}
else
{
print("Can't use Google Maps");
}
}
Keys Required
<key>LSApplicationQueriesSchemes</key>
<array>
<string>googlechromes</string>
<string>comgooglemaps</string>
</array>
Output
Upvotes: 2