### JavaScript API

Google Maps JavaScript API.
Reference: https://developers.google.com/maps/documentation/javascript/3.exp/reference
See also B4J jGoogleMaps Tutorial:
https://www.b4x.com/android/forum/threads/jgooglemaps-library.56744/#content
The intention to use the JavaScript API, is to enhance functionality which is not provided by the jGoogleMaps library (requires the JavaObject library).
Principle Example using Marker
Define a JavaObject holding a Marker.
Get the JavaScript Object, define a JavaScript Property and run the JavaScript Function eval with the Property as Parameter.
Return the result if required.
Hints
If PolyLines are used and markers are removed, then hide the PolyLine:
Dim PLine As MapPolyline
If PLine.IsInitialized Then PLine.Visible = False
Examples
'Set the title of the marker using standard property
`Sub SetMarkerTitle(m As Marker, title As String) 'ignore
Dim jo As JavaObject = m
jo = jo.RunMethod("getJSObject", Null)
Dim propertyjs As String = $"this.setTitle(${title});"$
jo.RunMethod("eval", Array(propertyjs))
End Sub`
'Set the title of the marker using standard property
`Sub SetMarkerTitle2(m As Marker, title As String) 'ignore
m.Title = title
End Sub`
'Return the title of a marker as string
'Example `
'Private SelectedMarker As Marker
'Log(GetMarkerTitle(SelectedMarker))
'`
`Sub GetMarkerTitle(m As Marker) As String
Dim jo As JavaObject = m
jo = jo.RunMethod("getJSObject", Null)
Dim propertyjs As String = $"this.getTitle();"$
Return jo.RunMethod("eval", Array(propertyjs))
End Sub`
'Set the marker label Text (String), TextColor (String), TextSize (Int)
'Example `
'Private SelectedMarker As Marker
'SetMarkerLabel(SelectedMarker, title, "red", 14)
'`
`Sub SetMarkerLabel(m As Marker, text As String, textcolor As String, textsize As Int)
Dim jo As JavaObject = m
jo = jo.RunMethod("getJSObject", Null)
Dim propertyjs As String = $"this.setLabel({text: "${text}",color: "${textcolor}","fontSize": "${textsize}px"});"$
jo.RunMethod("eval", Array(propertyjs))
End Sub`
'Remove a marker label
`Sub RemoveMarkerLabel(m As Marker)
Dim jo As JavaObject = m
jo = jo.RunMethod("getJSObject", Null)
Dim propertyjs As String = $"this.setLabel(null);"$
jo.RunMethod("eval", Array(propertyjs))
End Sub`
'Remove all marker labels
`Sub RemoveAllMarkerLabels
Log("Remove All Marker Labels")
If MarkerList.Size = 0 Then Return
For Each m As Marker In MarkerList
RemoveMarkerLabel(m)
Next
End Sub`
'Get the marker position as LatLng Object (Lat (Double), Lng (Double))
'Example `
'Private SelectedMarker As Marker
'Dim ll As LatLng= GetMarkerPosition(SelectedMarker)
'Log($"Position Lat/Lon: ${ll.Latitude} ${ll.Latitude}"$)
'`
`Sub GetMarkerPosition(m As Marker) As LatLng 'ignore
Dim jo As JavaObject = m
Dim PosLat As Double
Dim PosLng As Double
'Get the javascript object from the marker and from that the position (using a javascript property as parameter) which is assigned to the JavaObject
jo = jo.RunMethod("getJSObject", Null)
jo = jo.RunMethod("eval", Array($"this.getPosition();"$))
PosLat = jo.RunMethod("eval", Array($"this.lat();"$))
PosLng = jo.RunMethod("eval", Array($"this.lng();"$))
'Init the LatLng object and assign the lat / lng
Dim ll As LatLng
ll.Initialize(PosLat,PosLng)
Return ll
End Sub`
'Add a new marker using Position Lat (Double), Lng (Double), Title (String), IconUri (String).
'Returns the marker created.
'Example `
'Private SelectedMarker As Marker
'Private MarkerList As List 'used to hold the markers
'SelectedMarker = AddMarker(Lat, Lng, title, File.GetUri(File.DirAssets, "green-dot.png"))
'`
`Sub AddMarker(PosLat As Double, PosLng As Double, Title As String, IconUri As String) As Marker
If IconUri.Length = 0 Then
Dim newmarker As Marker = gmap.AddMarker(PosLat, PosLng, Title)
Else
Dim newmarker As Marker = gmap.AddMarker2(PosLat, PosLng, Title, IconUri)
End If
MarkerList.Add(newmarker)
Return newmarker
End Sub`
'Remove Marker from the map and the markerlist
'Example `
'Private SelectedMarker As Marker
'Private MarkerList As List 'used to hold the markers
'RemoveMarker(SelectedMarker)
'`
`Sub RemoveMarker(Mark As Marker) As Boolean
If Not(Mark.IsInitialized) Then Return False
gmap.RemoveMarker(Mark)
MarkerList.RemoveAt(MarkerList.IndexOf(Mark))
Return True
End Sub`
'Remove all Markers
`Sub RemoveAllMarkers
Log("Remove All Markers")
If MarkerList.Size = 0 Then Return
Do While MarkerList.Size > 0
Dim m As Marker = MarkerList.Get(0)
RemoveMarker(m)
Loop
End Sub`
'List all markers defined
`Sub ListAllMarkers
Log("List Markers")
For Each m As Marker In MarkerList
Dim ll As LatLng= GetMarkerPosition(m)
Log($"Position Lat/Lon: ${ll.Latitude} ${ll.Latitude}, Title ${GetMarkerTitle(m)}"$)
Next
End Sub`

Changed: , Category: Google Maps, File: T6C197M1447.htm, ID: 1447