B4A Class Open Street Map viewer - GPS

Hi,

This b4Xlib contains a custom view (cvMap) which can display Open Street Map.


screenshot.png
View attachment 109081



The tiles are retrieved from the internet and cached in a database. You can add shapes and images on the map.

UI :
- Lat/lng Center of the map
- Zoom Level
- Compass Direction with rotation
- Scale
- Button Menu
- Grid
- Center
- GPS position and bearing

Event :
- ready
- Lat/lng changed
- Zoom Level Changed
- Compass Direction Changed
- Shape Clicked
- Map Clicked
- Center Lat/lng clicked
- Button Menu clicked
- Scale clicked
- Compass Clicked
- GPS Clicked

Tile Server
2 new properties :
This 2 properties are available in the designer or with setter/getter from the cvmap : userAgent, tileServer

Dependencies :
- Core
- SQL
- OKHttpUtils2
- XUI
- XUI views

Other files in the b4xlib :
- coMapUtilities : code module with Types, functions, helpers
- clTileManager : standard class module to load tile from database and/or internet and save them into the database
- clMapShapeCirgle : standard class to draw circle on the map
- clMapShapeLine : standard class to draw line on the map
- clMapShapePolygon : standard class to draw polygon on the map
- clMapShapeImage : standard class to draw image on the map
- layout cvmap.bal and cvmap.bjl
- images for the compass and gps



How to use it :
- Just add the cvMap custom view with the designer
- create sub to handle events if necessary
- set options (lat/lng, zoom.....)

B4A example and B4J example included



spsp
 

Attachments

  • Screenshot_2021-02-21-11-49-10-222_map.b4A.com.jpg
    Screenshot_2021-02-21-11-49-10-222_map.b4A.com.jpg
    258.3 KB · Views: 1,878
  • B4Xmap.zip
    23 KB · Views: 1,912
  • B4Xmap.b4xlib
    23.7 KB · Views: 807
Last edited:

MbedAndroid

Well-Known Member
Licensed User
Longtime User
cvmap giving crashes. Got one of them during the exercises.
firebase output:
Fatal Exception: java.lang.RuntimeException
Object should first be initialized (B4XView).



anywheresoftware.b4a.AbsObjectWrapper.getObject (AbsObjectWrapper.java:67)


anywheresoftware.b4a.objects.B4XViewWrapper.getTag (B4XViewWrapper.java:600)


com.TrainingApp.cvmap._latlngtopoint (cvmap.java:1013)

com.TrainingApp.cvmap._update_gps (cvmap.java:1778)

com.TrainingApp.cvmap._gps_locationchanged (cvmap.java:970)

com.TrainingApp.main._updatechart (main.java:9304)

java.lang.reflect.Method.invoke (Method.java)


anywheresoftware.b4a.keywords.Common.CallSubNew2 (Common.java:1069)

com.TrainingApp.tracker._gps_locationchanged (tracker.java:205)

java.lang.reflect.Method.invoke (Method.java)


com.android.internal.os.ZygoteInit.main (ZygoteInit.java:947)


trying to solve it in this way:
B4X:
public Sub LatLngToPoint(aLatLng As TMapLatLng) As TMapPoint
    Try
    'https://www.netzwolf.info/osm/tilebrowser.html?marker.x=12&marker.y=189&tx=1&ty=0&tz=1&ts=256#tile
    Dim v As B4XView=PointToTile(0,0)
    If v.IsInitialized=False Then  'from here
        Dim pp As TMapPoint
        pp.Initialize
        pp.fX=0
        pp.fY=0
        Return pp
    End If 'to here
    Dim txy As TMapTileXY=v.tag
    Dim gpx As Double=(txy.fX+(-v.Left/coMapUtilities.cTileSize))/Power(2,fMap.fZoomLevel)
    Dim gpy As Double=(txy.fy+(-v.top/coMapUtilities.cTileSize))/Power(2,fMap.fZoomLevel)
    Dim tx As TMapTileNumberOffset=LngToTileX(aLatLng.fLng,fMap.fZoomLevel)
    Dim ty As TMapTileNumberOffset=LatToTileY(aLatLng.fLat,fMap.fZoomLevel)
    Dim ppx As Double=(tx.fTile+(tx.fOffset/coMapUtilities.cTileSize))/Power(2,fMap.fZoomLevel)
    Dim ppy As Double=(ty.fTile+(ty.fOffset/coMapUtilities.cTileSize))/Power(2,fMap.fZoomLevel)
    Dim p As TMapPoint=coMapUtilities.initPoint(fTilesCount*coMapUtilities.cTileSize*(ppx-gpx),fTilesCount*coMapUtilities.cTileSize*(ppy-gpy))
    Return p

End Sub
 
Last edited:

MbedAndroid

Well-Known Member
Licensed User
Longtime User
found another crash
Fatal Exception: java.lang.RuntimeException: Object should first be initialized (B4XView).
at anywheresoftware.b4a.AbsObjectWrapper.getObject(AbsObjectWrapper.java:67)
at anywheresoftware.b4a.objects.B4XViewWrapper.asViewWrapper(B4XViewWrapper.java:91)
at anywheresoftware.b4a.objects.B4XViewWrapper.getTag(B4XViewWrapper.java:600)
at com.TrainingApp.cvmap._latlngtopoint(cvmap.java:1013)
at com.TrainingApp.cvmap._update_gps(cvmap.java:1781)
at com.TrainingApp.cvmap._gps_locationchanged(cvmap.java:970)
at com.TrainingApp.main._updatechart(main.java:9488)
at java.lang.reflect.Method.invoke(Method.java)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:221)

hope this solves the bug in the same way as post #122
B4X:
public Sub LatLngToPoint(aLatLng As TMapLatLng) As TMapPoint
    'https://www.netzwolf.info/osm/tilebrowser.html?marker.x=12&marker.y=189&tx=1&ty=0&tz=1&ts=256#tile
    Dim v As B4XView=PointToTile(0,0)
    If v.IsInitialized Then '<----add
    Dim txy As TMapTileXY=v.tag
    Dim gpx As Double=(txy.fX+(-v.Left/coMapUtilities.cTileSize))/Power(2,fMap.fZoomLevel)
    Dim gpy As Double=(txy.fy+(-v.top/coMapUtilities.cTileSize))/Power(2,fMap.fZoomLevel)
    Dim tx As TMapTileNumberOffset=LngToTileX(aLatLng.fLng,fMap.fZoomLevel)
    Dim ty As TMapTileNumberOffset=LatToTileY(aLatLng.fLat,fMap.fZoomLevel)
    Dim ppx As Double=(tx.fTile+(tx.fOffset/coMapUtilities.cTileSize))/Power(2,fMap.fZoomLevel)
    Dim ppy As Double=(ty.fTile+(ty.fOffset/coMapUtilities.cTileSize))/Power(2,fMap.fZoomLevel)
    Dim p As TMapPoint=coMapUtilities.initPoint(fTilesCount*coMapUtilities.cTileSize*(ppx-gpx),fTilesCount*coMapUtilities.cTileSize*(ppy-gpy))
    Else 'add from here
Dim p As TMapPoint
        p.Initialize
        p.fX=0
        p.fY=0
    End If 'to here
    Return p

      
End Sub
 
Top