iOS Question CanOpenURL Returning incorrect result

Andrew (Digitwell)

Well-Known Member
Licensed User
Longtime User
I want to open a map with directions, but I would prefer to open Google Maps than The Apple Maps.

so I had the following code.
B4X:
    Private googstr As String = "comgooglemaps://maps.google.com?daddr="&Lat&","&Lng
    Private mainstr As String = "http://maps.apple.com/?daddr="&Lat&","&Lng&"ll="&Lat&","&Lng&"t=m&dirflg=d"
    if (main.app.canOpenURL(googstr)) then
        Main.App.OpenURL(googstr)
    end if
    If (Main.App.CanOpenURL(mainstr)) Then
        Main.App.OpenURL(mainstr)
     End If
 end sub

However, it was always opening Apple maps.

The following code works. It seems CanOpenURL on the Google string *always* failed
B4X:
Sub NavigatetoLatLong(Lat As String, Lng As String)
    Private googstr As String = "comgooglemaps://maps.google.com?daddr="&Lat&","&Lng
    Private mainstr As String = "http://maps.apple.com/?daddr="&Lat&","&Lng&"ll="&Lat&","&Lng&"t=m&dirflg=d"
    Private done As Boolean = Main.App.OpenURL(googstr)
    Log("Opened google maps = " & done)
    If Not(done) Then
        If (Main.App.CanOpenURL(mainstr)) Then
            Main.App.OpenURL(mainstr)
        End If
    End If
End Sub

I have a queryscheme setup in Main,
#UrlScheme: comgooglemaps

Does anyone know why this is happening?

Also, is there a way to get Apple Maps to navigate to a LATLNG point. There is this article
https://developer.apple.com/library...s.html#//apple_ref/doc/uid/TP40007899-CH5-SW1
but all it says is "An address string that geolocation can understand."

Thanks
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
This code works:
B4X:
#UrlScheme: comgooglemaps
Sub Process_Globals
   Public App As Application
   Public NavControl As NavigationController
   Private Page1 As Page

End Sub

Private Sub Application_Start (Nav As NavigationController)
   'SetDebugAutoFlushLogs(True) 'Uncomment if program crashes before all logs are printed.
   NavControl = Nav
   Page1.Initialize("Page1")
   Page1.Title = "Page 1"
   Page1.RootPanel.Color = Colors.White
   NavControl.ShowPage(Page1)
   Log(App.CanOpenURL("comgooglemaps://")) 'prints true (Google Maps needs to be installed).
End Sub
 
Upvote 0

Andrew (Digitwell)

Well-Known Member
Licensed User
Longtime User
Erel,
I agree it seems to work fine by itself, but for some strange reason it does not work in my app.
I even put the NavigatetoLatLng sub in an app by itself and that works.

I will continue to investigate why it does not work in my particular app.
 
Upvote 0

BillMeyer

Well-Known Member
Licensed User
Longtime User
Hi Andrew,

This works for me. I call a new module and pass the Lat,Lng to it and then open a Webview in it with the required URL. Here's how:

Calling it:
B4X:
Sub btnNavigate_Click
        modNavigateTo.NavURL = "https://www.google.com/maps/dir/?api=1&origin="&Main.GlobLat&","&Main.GlobLon&"&destination="&Latitude&","&Longitude&"&travelmode=driving&dir_action=navigate"
        modNavigateTo.Show

End Sub

and then my module where the action happens: (it's called "modNavigateTo" and "scrWebView" is a page that contains a Webview)

Action happens here:
B4X:
Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'Public variables can be accessed from all modules.

    Dim scrWebView As Page
    Private hd As HUD
    Dim rs As ResultSet
    Dim RType As ActionSheet
    Private HOften As ActionSheet
   
    Private WebView1 As WebView
    Private HeaderIMG As ImageView
    Private lblIndicator As Label
    Public NavURL As String
    Dim ShowNav As Boolean
   
    Dim gMap As GoogleMap
    Private WebView1 As WebView
End Sub

Public Sub Show
   
    Log("In Public Show of mod Navigate To")
    
    scrWebView.Initialize("scrWebView")
    Log("Init ? "&scrWebView.IsInitialized)
   
    If scrWebView.IsInitialized = False Then
        scrWebView.Initialize("scrWebView")
        scrWebView.RootPanel.LoadLayout("scrWebView")
    Else
        scrWebView.RootPanel.LoadLayout("scrWebView")
    End If

    Main.NavControl.ShowPage2(scrWebView,True)
    Main.NavControl.ToolBarVisible = False
    hd.ProgressDialogShow("Routing.....")
    Log("Nav URL: "&NavURL)
    WebView1.LoadUrl(NavURL)

''    If Main.App.CanOpenURL("telprompt:") = True Then
'    Main.App.OpenUrl("google.navigation:q=" & Main.GlobLat & "," & Main.GlobLon)
''    End If
   
End Sub

Sub WebView1_PageFinished (Success As Boolean, Url As String)

    hd.ProgressDialogHide
End Sub

Sub HeaderIMG_Click
    Log("Header Clicked")
    Main.NavControl.RemoveCurrentPage2(True)
    Main.NavControl.ToolBarVisible = True
   
End Sub

Your end result will look like this: (my Screen has a "header" with the customers logo on it - this can be removed as you desire)

upload_2018-9-13_12-27-58.png


I trust this will be an alternative method to what you want to do - Test on an iPhone 6S+

Enjoy
 
Upvote 0
Top