B4J Library jGoogleMaps library

Status
Not open for further replies.
This library is similar to B4A and B4i GoogleMaps libraries.

SS-2015-07-30_15.35.52.jpg


It is based on this open source project: https://github.com/dlsc-software-consulting-gmbh/GMapsFX/ (Apache 2 license).
Under the hood it uses JavaFX WebView with GoogleMaps JavaScript API V3.

Using the map is quite simple. You need to initialize GoogleMap and then wait for the Ready event.
GoogleMap.AsPane returns the pane that holds the map. You should add this pane to the nodes tree.

Once the ready event is fired you can add markers or change the position.

Updates:

V2.00 - Depends on OpenJDK 19. Previous versions will not work.

1. OpenJDK 19.0.2 + OpenJFX 17.0.6: https://www.b4x.com/b4j/files/jdk-19.0.2.7z
2. Add to main module:
B4X:
#PackagerProperty: IncludedModules = javafx.web
#PackagerProperty: AdditionalModuleInfoString = exports com.lynden.gmapsfx;    exports com.lynden.gmapsfx.javascript;    exports com.lynden.gmapsfx.javascript.event;
#PackagerProperty: AdditionalModuleInfoString = exports com.lynden.gmapsfx.javascript.object;    exports com.lynden.gmapsfx.service.directions;    exports com.lynden.gmapsfx.service.elevation;  
#PackagerProperty: AdditionalModuleInfoString = exports com.lynden.gmapsfx.service.geocoding;    exports com.lynden.gmapsfx.shapes;    exports com.lynden.gmapsfx.zoom;
3. Add reference to jOkHttpUtils2.

Tip: to fix logging encoding issues with Java 19+, add this:
B4X:
#VirtualMachineArgs: -Dfile.encoding=UTF-8 -Dsun.stdout.encoding=UTF-8 -Dsun.stderr.encoding=UTF-8
#PackagerProperty: VMArgs = -Dfile.encoding=UTF-8 -Dsun.stdout.encoding=UTF-8 -Dsun.stderr.encoding=UTF-8

The standalone package will not work without steps 2 and 3.
 

Attachments

  • jGoogleMaps.zip
    199.5 KB · Views: 330
  • GoogleMapExample.zip
    2.8 KB · Views: 204
Last edited:

javiers

Active Member
Licensed User
Longtime User
Marker.Remove removes a marker.

You need to store references to the added markers and then you will be able to remove them.

For example:
B4X:
mapOfMarkers.Put("marker1", gmap.AddMarker(...))

Later:
B4X:
Dim m1 As Marker = mapOfMarkers.Get("marker1")
m1.Remove

Thank you! always so excellent in providing assistance .
 

ivanomonti

Expert
Licensed User
Longtime User
How can I re-read the title of a maker to be used as query call record database

SelectedMarker.Title and only writing

example

B4X:
"select * fron table where field = SelectedMarker.Title"
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
Create a global Map that maps between markers and any other data you need to store (title for example).

B4X:
Sub AddMarker(..., Title As String)
Dim m As Marker = gmap.AddMarker(lat, lon, Title)
MapOfMarkers.Put(m, Title)
End Sub

Dim title As String = MapOfMarkers.Get(SelectedMarker)
 
Last edited:

ivanomonti

Expert
Licensed User
Longtime User
possible map error due to the links of credits google below right, leads to the menu page of info service, and then returns to map.


2015-10-21_004824.jpg 2015-10-21_004714.jpg 2015-10-21_003433.jpg
 

Douglas Farias

Expert
Licensed User
Longtime User
@Erel
its possible show a panel with mensage like the image on github?
gmapsfx.png


like pop up on fred wilkie with a x to close the panel, this pop up is from lib ?
 

Douglas Farias

Expert
Licensed User
Longtime User
thx man this works i want only show a car speed =) ty
 

vfafou

Well-Known Member
Licensed User
Longtime User
Hello!
Is it possible to use this library within a Web Application, or it's used only within desktop apps?
Thank you in advance!
 

valentino s

Active Member
Licensed User
Longtime User
Create a global Map that maps between markers and any other data you need to store (title for example).

B4X:
Sub AddMarker(..., Title As String)
Dim m As Marker = gmap.AddMarker(lat, lon, Title)
MapOfMarkers.Put(m, Title)
End Sub

Dim title As String = gmap.Get(SelectedMarker)

Sorry Erel, is it possible that code in post #26 doesn't work, but works the following ? :

B4X:
Dim title As String = MapOfMarkers.Get(SelectedMarker)

To me works. Is it correct ?
 

Roger Daley

Well-Known Member
Licensed User
Longtime User
Marker.Remove removes a marker.

You need to store references to the added markers and then you will be able to remove them.

For example:
B4X:
mapOfMarkers.Put("marker1", gmap.AddMarker(...))

Later:
B4X:
Dim m1 As Marker = mapOfMarkers.Get("marker1")
m1.Remove


Hi Erel/All

When I run the code below I get an error that BTSMarkerX should be first initialized. Not an option.
B4X:
Dim BTSMarkerX As Marker = markerMap.Get("BTSMarker")
        BTSMarkerX.Remove

Anyone see what I have missed this time?

Regards Roger
 

Roger Daley

Well-Known Member
Licensed User
Longtime User
Erel,

Code below.

B4X:
Sub Process_Globals
    Private  markerMap As Map
    Private MarkerFlag As String
' Etc.
End Sub

Sub gmap_Ready

   'CreatE BTSMarker
        MarkerLocation.Initialize(BTSLat,BTSLng)
        Private BTSMarker = gmap.AddMarker2(BTSLat , BTSLng, "BASE", File.GetUri(File.DirAssets, "bts.png")) As Marker
        BTSMarker.Position = MarkerLocation         'set BTSMarker new location
        markerMap.Put(BTSMarker, 1)                 'place them both in a Map so you can retrieve Marker1's location (MarkerLocation)

    'CreatE LANDMarker
        MarkerLocation.Initialize(LANDLat,LANDLng)
        Private LANDMarker = gmap.AddMarker2(LANDLat , LANDLng, "LANDMARK", File.GetUri(File.DirAssets, "landmark.png")) As Marker
        LANDMarker.Position = MarkerLocation         'set LANDMarker new location
        markerMap.Put(LANDMarker, 2)                 'place them both in a Map so you can retrieve Marker1's location (MarkerLocation)
   
        HorizonPointLatLng
        DrawLandMarkLine
        DrawAntBearingLine
       
        cp.Initialize(MapLat, MapLng, MapZoom )
        gmap.MoveCamera(cp)
End Sub

#End Region
Sub gmap_Click (Point As LatLng)
    Private MapCenter As LatLng   
    Private BTSLatLng As LatLng
    Private LandMarkLatLng As LatLng       
       
    If MarkerFlag = 1 Then
        BTSLatLng = Point
        BTSLat = BTSLatLng.Latitude
        BTSLng = BTSLatLng.Longitude

'Remove marker before placing marker at new location
        Dim BTSMarkerx As Marker  = markerMap.Get("BTSMarker")
        BTSMarkerx.Remove

'Add marker at new location       
        MarkerLocation.Initialize(BTSLat,BTSLng)
        Private BTSMarker = gmap.AddMarker2(10 , 10, "BASE", File.GetUri(File.DirAssets, "bts.png")) As Marker
        BTSMarker.Position = MarkerLocation         'set BTSMarker new location
        markerMap.Put(BTSMarker, 1)    

    End If
    If MarkerFlag = 2 Then
        LandMarkLatLng = Point
        LANDLat = LandMarkLatLng.Latitude
        LANDLng = LandMarkLatLng.Longitude        

'Remove marker before placing marker at new location       
        Dim LANDMarkerX As Marker = markerMap.Get("LANDMarker")
        LANDMarkerX.Remove


'Add marker at new location   
        MarkerLocation.Initialize(LANDLat,LANDLng)
        Private LANDMarker = gmap.AddMarker2(LANDLat , LANDLng, "LANDMARK", File.GetUri(File.DirAssets, "landmark.png")) As Marker
        LANDMarker.Position = MarkerLocation         'set LANDMarker new location
        markerMap.Put(LANDMarker, 2)                 'place them both in a Map so you can retrieve Marker1's location (MarkerLocation)    
    End If


    DispBTSLat.Text = NumberFormat2 (BTSLat, 1 ,  5, 5, False)
    DispBTSLng.Text = NumberFormat2 (BTSLng, 1 , 5, 5, False)
    DispLandLat.Text = NumberFormat2 (LANDLat, 1 , 5, 5, False)
    DispLandLng.Text = NumberFormat2 (LANDLng, 1 , 5, 5, False)
    DispAntBearing.Text = NumberFormat2 (AntBearing, 1 , 1 , 1, False)
   
    HorizonPointLatLng
    DrawLandMarkLine
    DrawAntBearingLine
   
'Force re-draw of map to remove old marker icon
    MapZoom = gmap.CameraPosition.Zoom
    MapCenter = gmap.CameraPosition.Target
    cp.Initialize(MapCenter.Latitude, MapCenter.Longitude, 10 )
    gmap.MoveCamera(cp)
    cp.Initialize(MapCenter.Latitude, MapCenter.Longitude, MapZoom )
    gmap.MoveCamera(cp)
   
    MarkerFlag = 0
End Sub

Regards Roger
 

jmon

Well-Known Member
Licensed User
Longtime User

jmon

Well-Known Member
Licensed User
Longtime User
I get an error when starting the app with no internet connection:
netscape.javascript.JSException: ReferenceError: Can't find variable: google
at com.sun.webkit.dom.JSObject.fwkMakeException(JSObject.java:128)
at com.sun.webkit.WebPage.twkExecuteScript(Native Method)
at com.sun.webkit.WebPage.executeScript(WebPage.java:1439)
at javafx.scene.web.WebEngine.executeScript(WebEngine.java:982)
at com.lynden.gmapsfx.javascript.JavaFxWebEngine.executeScript(JavaFxWebEngine.java:39)
at com.lynden.gmapsfx.javascript.JavascriptRuntime.execute(JavascriptRuntime.java:63)
at com.lynden.gmapsfx.javascript.JavascriptObject.<init>(JavascriptObject.java:76)
at com.lynden.gmapsfx.javascript.object.LatLong.<init>(LatLong.java:36)
at anywheresoftware.b4j.objects.GoogleMapWrapper$1.mapInitialized(GoogleMapWrapper.java:73)
at com.lynden.gmapsfx.GoogleMapView.fireMapInitializedListeners(GoogleMapView.java:205)
at com.lynden.gmapsfx.GoogleMapView$1.changed(GoogleMapView.java:91)
at com.lynden.gmapsfx.GoogleMapView$1.changed(GoogleMapView.java:1)
at com.sun.javafx.binding.ExpressionHelper$SingleChange.fireValueChangedEvent(ExpressionHelper.java:182)
at com.sun.javafx.binding.ExpressionHelper.fireValueChangedEvent(ExpressionHelper.java:81)
at javafx.beans.property.ReadOnlyObjectWrapper$ReadOnlyPropertyImpl.fireValueChangedEvent(ReadOnlyObjectWrapper.java:176)
at javafx.beans.property.ReadOnlyObjectWrapper.fireValueChangedEvent(ReadOnlyObjectWrapper.java:142)
at javafx.beans.property.ObjectPropertyBase.markInvalid(ObjectPropertyBase.java:112)
at javafx.beans.property.ObjectPropertyBase.set(ObjectPropertyBase.java:146)
at javafx.scene.web.WebEngine$LoadWorker.updateState(WebEngine.java:1260)
at javafx.scene.web.WebEngine$LoadWorker.dispatchLoadEvent(WebEngine.java:1371)
at javafx.scene.web.WebEngine$LoadWorker.access$1200(WebEngine.java:1253)
at javafx.scene.web.WebEngine$PageLoadListener.dispatchLoadEvent(WebEngine.java:1240)
at com.sun.webkit.WebPage.fireLoadEvent(WebPage.java:2400)
at com.sun.webkit.WebPage.fwkFireLoadEvent(WebPage.java:2244)
at com.sun.webkit.network.URLLoader.twkDidFail(Native Method)
at com.sun.webkit.network.URLLoader.notifyDidFail(URLLoader.java:862)
at com.sun.webkit.network.URLLoader.lambda$didFail$97(URLLoader.java:845)
at com.sun.javafx.application.PlatformImpl.lambda$null$173(PlatformImpl.java:295)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater$174(PlatformImpl.java:294)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null$148(WinApplication.java:191)
at java.lang.Thread.run(Thread.java:745)
And it seems that it cannot be caught, even with a Try / Catch at the initialization of Gmap, thoough it does not crash the app.
 
Status
Not open for further replies.
Top