Hello,
Ok, so very simple question. I've searched a lot about this and found a sample code (webview and javascript) but kinda confused as this code does not seem to handle "click" message passing from javascript.
Goal: Program should center the map around current GPS location and clicked location lat/lon should be obtained.
Question: How do I get the GPS lat/lon of a clicked location on Google Maps? Adding a marker at this location is fine.
Code I'm trying:
Ok, so very simple question. I've searched a lot about this and found a sample code (webview and javascript) but kinda confused as this code does not seem to handle "click" message passing from javascript.
Goal: Program should center the map around current GPS location and clicked location lat/lon should be obtained.
Question: How do I get the GPS lat/lon of a clicked location on Google Maps? Adding a marker at this location is fine.
Code I'm trying:
B4X:
Sub Activity_Create(FirstTime As Boolean)
gpstimer.Initialize("gpstimer",20000)
gpstimer.Enabled = False
gps1.Initialize("GPS")
If gps1.GPSEnabled = False Then
ToastMessageShow("Turn on GPS.", True)
StartActivity(gps1.LocationSettingsIntent) 'Will open the relevant settings screen.
Else
gps1.Start(0, 0) 'Listen to GPS with no filters.
gpstimer.enabled = True
End If
WebView1.Initialize("WebView1")
' use WebViewExtras to add the JavascriptInterface to the WebView
' the javascript can now call B4A Subs
Dim WebViewExtras1 As WebViewExtras
WebViewExtras1.addJavascriptInterface(WebView1, "B4A")
Activity.LoadLayout("test.bal")
WebView1.LoadUrl("file:///android_asset/draggable-markers.html")
End Sub
Sub Map_Ready
Log("map ready")
gmap = mFragment.GetMap
If gmap.IsInitialized = False Then
ToastMessageShow("Error initializing map.", True)
Else
m1=gmap.AddMarker(INIT_LAT, INIT_LON, "Hello!!!")
Dim cp As CameraPosition
cp.Initialize(INIT_LAT, INIT_LON, gmap.CameraPosition.Zoom)
gmap.AnimateCamera(cp)
m1.Draggable = True
EditText1.Text = m1.Position.Latitude & "," & m1.Position.Longitude
End If
markerDragListener.Initialize("markerDragListener")
GoogleMapsExtras1.SetOnMarkerDragListener(gmap,markerDragListener)
End Sub
''''This function does not seem to work!!!
Sub markerDragListener_DragEnd(NewLatitude As String, NewLongitude As String)
' only String types can be passed from the javascript to B4A so we need to convert the String types to Double
Dim lat As Double=NewLatitude
Dim lon As Double=NewLongitude
EditText1.Text = lat & "," & lon
End Sub
Sub gpstimer_Tick
Msgbox("No GPS!","")
gpstimer.Enabled = False
gps1.Stop
End Sub
Sub GPS_LocationChanged (Location1 As Location)
EditText1.Text = NumberFormat(Location1.Latitude,0,6) & "," & NumberFormat(Location1.Longitude,0,6)
INIT_LAT = Location1.Latitude
INIT_LON = Location1.Longitude
gps1.Stop
gpstimer.Enabled = False
End Sub