shapes.Add(coMapUtilities.instanceShapeLine(fcvMap,coMapUtilities.initShapeLine(coMapUtilities.initLatLng(48.856614,2.3522219),coMapUtilities.initLatLng(31.630000,-8.008889),fxui.Color_ARGB(128,255,0,255),1dip,"Paris to Marrakech")))
shapes.Add(coMapUtilities.instanceShapeLine(fcvMap,coMapUtilities.initShapeLine(coMapUtilities.initLatLng(" & PrecLt & "," & PrecLg & "),coMapUtilities.initLatLng(" & Lg & "," & Lt & "),fxui.Color_ARGB(128,255,0,255),1dip,"test")))
Here some code I use that will hopefully get you on the right track:I'm trying to draw a GPS track on a map created using the B4XMap library.
Documentation is poor and the only useful instruction I've found is the following (draw a line between 2 dots)
B4X:shapes.Add(coMapUtilities.instanceShapeLine(fcvMap,coMapUtilities.initShapeLine(coMapUtilities.initLatLng(48.856614,2.3522219),coMapUtilities.initLatLng(31.630000,-8.008889),fxui.Color_ARGB(128,255,0,255),1dip,"Paris to Marrakech")))
However I can not indicate latitude and longitude with variables :
Point 1: (Lt, Lg)
Point 2: (PrecLt, PrecLg)
Something like that :
B4X:shapes.Add(coMapUtilities.instanceShapeLine(fcvMap,coMapUtilities.initShapeLine(coMapUtilities.initLatLng(" & PrecLt & "," & PrecLg & "),coMapUtilities.initLatLng(" & Lg & "," & Lt & "),fxui.Color_ARGB(128,255,0,255),1dip,"test")))
Sub btnMakeArea_Click
If lstPolygonPoints.Size < 2 Then Return
If Enums.bDrawnArea Then
RemovePolygon
Enums.bDrawnArea = False
Return
End If
If lstPolygonPoints.Size = 2 Then
Dim BB As TMapBoxLatLng = BoundingBoxFromtLL_List(lstPolygonPoints)
RemovePolygon
'closing the path is taken care of here!
lstPolygonPoints = BB2lstPath(BB)
BB2MinMax_LatLon(BB)
Enums.bSquareFromTwotLLs = True
Enums.bDrawnArea = True
Else
'to close the polygon in the list path!!
lstPolygonPoints.Add(lstPolygonPoints.Get(0))
End If
Dim tMSP As TMapShapePolygon = MapUtilities.initShapePolygon(lstPolygonPoints, xui.Color_ARGB(128,255,0,255), False, 2dip, "MapArea")
Dim cMSP As clsMapShapePolygon = MapUtilities.instanceShapePolygon(cvMap1, tMSP)
mapPolygonLines.put("MapArea", "MapArea")
mapShapes.Put("MapArea", cMSP)
cvMap1.Draw
Enums.bDrawnArea = True
End Sub
Sub BoundingBoxFromtLL_List(lsttLL As List) As TMapBoxLatLng
Dim BB As TMapBoxLatLng
Dim dMinLat As Double = 100
Dim dMaxLat As Double = -100
Dim dMinLng As Double = 200
Dim dMaxLng As Double = -200
For Each tLL As TMapLatLng In lsttLL
If tLL.flat > dMaxLat Then
dMaxLat = tLL.flat
End If
If tLL.flat < dMinLat Then
dMinLat = tLL.flat
End If
If tLL.flng > dMaxLng Then
dMaxLng = tLL.flng
End If
If tLL.flng < dMinLng Then
dMinLng = tLL.flng
End If
Next
Dim BB As TMapBoxLatLng = MapUtilities.initBoxLatLng(dMaxLat, dMinLng, dMinLat, dMaxLng)
Return BB
End Sub
Sub RemovePolygon
lstPolygonPoints.Clear
For Each oKey As Object In mapPolygonLines.Keys
'changed the list in type TMap (holding the shapes) to a map
mapShapes.Remove(oKey)
Next
'this map keeps track of the polygon lines and the final polygon shape
mapPolygonLines.Clear
cvMap1.Draw
End Sub
Sub BB2lstPath(BB As TMapBoxLatLng) As List
Dim lstPath As List
lstPath.Initialize
Dim tLLNW As TMapLatLng = MapUtilities.initLatLng(BB.fLeftTop.fLat, BB.fLeftTop.fLng)
lstPath.Add(tLLNW) 'NW
Dim tLLNE As TMapLatLng = MapUtilities.initLatLng(BB.fLeftTop.fLat, BB.fRightBottom.fLng)
lstPath.Add(tLLNE) 'NE
Dim tLLSE As TMapLatLng = MapUtilities.initLatLng(BB.fRightBottom.fLat, BB.fRightBottom.fLng)
lstPath.Add(tLLSE) 'SE
Dim tLLSW As TMapLatLng = MapUtilities.initLatLng(BB.fRightBottom.fLat, BB.fLeftTop.fLng)
lstPath.Add(tLLSW) 'SW
lstPath.Add(tLLNW) 'NW, again, to close the path
Enums.bDrawnArea = True
Return lstPath
End Sub
Sub BB2MinMax_LatLon(BB As TMapBoxLatLng)
Dim strSQL As String
'Need to make sure this table always has one row!
'------------------------------------------------
strSQL = "UPDATE MINMAX_LATLON SET MIN_LAT = ?, MAX_LAT = ?, MIN_LON = ?, MAX_LON = ?"
SQL_ExecNonQuery2(strSQL, Array As String(BB.fRightBottom.fLat, _
BB.fLeftTop.fLat, _
BB.fLeftTop.fLng, _
BB.fRightBottom.fLng))
End Sub
shapes.Add(coMapUtilities.instanceShapeLine(fcvMap,coMapUtilities.initShapeLine(coMapUtilities.initLatLng(" & PrecLt & "," & PrecLg & "),coMapUtilities.initLatLng(" & Lg & "," & Lt & "),fxui.Color_ARGB(128,255,0,255),1dip,"test")))
Dim PrecL,PrecLg, Lg, Lt As Double
shapes.Add(coMapUtilities.instanceShapeLine(fcvMap,coMapUtilities.initShapeLine(coMapUtilities.initLatLng( PrecL,PrecLg),coMapUtilities.initLatLng(Lt, Lg),fxui.Color_ARGB(128,255,0,255),1dip,"test")))
OK, this is the code I have to add an area point (and that means drawing a line):Thanks but that's not what I'm asking for: a GPS track (a line) and no polygons.
Sub AddAreaPoint(tLL As TMapLatLng) As Double
Dim oKey As Object
Dim Location1 As Location
Dim Location2 As Location
Dim dMiles As Double
If Enums.bDrawnArea Then
Return 0
End If
Enums.bSquareFromTwotLLs = False
Dim tLL1 As TMapLatLng
Dim tLL2 As TMapLatLng
tLL2.fLat = tLL.fLat
tLL2.fLng = tLL.fLng
If lstPolygonPoints.Size > 0 Then
tLL1 = lstPolygonPoints.get(lstPolygonPoints.Size - 1)
If tLL1.flat = tLL2.fLat Then
If tLL1.fLng = tLL2.fLng Then
Return 0
End If
End If
End If
lstPolygonPoints.Add(tLL2)
Location1.Initialize
Location2.Initialize
Location1.Latitude = tLL1.fLat
Location1.Longitude = tLL1.fLng
Location2.Latitude = tLL.fLat
Location2.Longitude = tLL.fLng
If lstPolygonPoints.Size > 1 Then
oKey = "Polygon_Line" & lstPolygonPoints.Size
Dim tMSL As TMapShapeLine = MapUtilities.initShapeLine(tLL1, tLL2, xui.Color_ARGB(128,255,0,255), 2dip, oKey)
Dim cMSL As clsMapShapeLine = MapUtilities.instanceShapeLine(cvMap1, tMSL)
mapPolygonLines.Put(oKey, oKey)
mapShapes.Put(oKey, cMSL)
cvMap1.Draw
End If
If lstPolygonPoints.Size > 1 Then
dMiles = Location1.DistanceTo(Location2) / 1000 * dKM2Miles
End If
Return dMiles
End Sub
Below is an essential summary of my code :** Activity (main) Pause, UserClosed = false **
** Activity (mappaweb) Create, isFirst = true **
download https://www.******************/Tracks/20230208.txt
** Activity (mappaweb) Resume **
Sub fcvmap_ready
Error occurred on line: 323 (cvMap)
java.lang.NullPointerException: Attempt to read from field 'double alpvir.sq.comaputilities$_tmaplatlng.fLat' on a null object reference in method 'java.lang.String alpvir.sq.cvmap._setcenterlatlng(alpvir.sq.cvmap, alpvir.sq.comaputilities$_tmaplatlng)'
at alpvir.sonoqui.cvmap._setcenterlatlng(cvmap.java:2206)
at alpvir.sonoqui.cvmap._setmap(cvmap.java:119)
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:732)
at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:351)
at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:255)
Sub Process_Globals
Private fMap As TMap
Private fxui As XUI
End Sub
Sub Globals
Private fcvMap As cvMap
Private shapes As List
Dim PrecLT,PrecLg, Lg, Lt As Double
End Sub
Sub Activity_Create(FirstTime As Boolean)
Activity.LoadLayout("Mappa_OSM")
shapes.Initialize
DownloadTraceGPS ' sub
End sub
Sub DownloadTraceGPS
Private JobDownload As HttpJob
JobDownload.Initialize("JobDownload", Me)
JobDownload.Download(URL)
Wait For (JobDownload) JobDone(JobDownload As HttpJob)
If JobDownload.Success = True Then
Dim out As OutputStream = File.OpenOutput(Main.GlobalThisApp, Main.TraceWeb, False)
File.Copy2(JobDownload.GetInputStream, out)
out.Close
ReadShapes ' sub
ShowMap ' sub
end if
End sub
Sub ReadShapes
dim N as int
TextReader1.Initialize(File.OpenInput(Main.GlobalThisApp, Main.TraceWeb))
Do While Riga <> Null
Riga = TextReader1.ReadLine
ele = Regex.Split(",",Riga)
Lt=ele(0).trim
Lg= ele(1).trim
if N>0 then
shapes.Add(coMapUtilities.instanceShapeLine(fcvMap,coMapUtilities.initShapeLine(coMapUtilities.initLatLng( PrecLT,PrecLg),coMapUtilities.initLatLng(Lt, Lg),Colors.ARGB(128,255,0,255),2dip,"test")))
end if
N=N+1
PrecLT=Lt:PrecLg=Lg
Loop
TextReader1.Close
End sub
Sub ShowMap
Log(shapes.size) ' OK
' For Each ZZ As TMapShapeLine In shapes
' Log(ZZ.fLatLng1.fLat & " " & ZZ.fLatLng1.fLng & " TO " & ZZ.fLatLng2.fLat & " " & ZZ.fLatLng2.fLng)
' Next
fMap=coMapUtilities.initMap(coMapUtilities.initLatLng(lt, lg), ZoomLevel, CompassDirection, OfflineMode , ShowMenu, ShowGrid, ShowCenter, ShowLandmark, ShowCenterLatLon, ShowZoom, ShowScale, ShowCompass, ShowShapes, _
shapes, _
ShowGPS, FollowGPS,coMapUtilities.initGPS(coMapUtilities.initLatLng(0,0),0))
end sub
Sub Activity_Pause (UserClosed As Boolean)
fMap=fcvMap.map
End Sub
private Sub fcvmap_ready
fcvMap.TileServer="https://a.tile.openstreetmap.org/" ' default
fcvMap.UserAgent="User-AgentMozilla/5.0 (Windows NT 10.0; Win64; x64; rv:108.0) Gecko/20100101 Firefox/108.0"
fcvMap.Map=fMap
'--- draw the map
fcvMap.draw
fcvMap.Map=fMap
fcvMap.draw
End Sub
Just guessing, but I think in places you will need Sleep (eg Sleep(10)) to make sure that cvMap is ready when it is called upon.By entering 3 or 4 GPS coordinates, the track that connects them is actually displayed.
However, when it comes to downloading a GPS track consisting of 2 or 3 thousand points from the internet, an error appears which in my opinion is not attributable to the library but to the (wrong) succession of subs and events (Create and/or Resume).
Here is the error :
Below is an essential summary of my code :
Is it possible to have a suggestion to remedy the malfunction?B4X:Sub Process_Globals Private fMap As TMap Private fxui As XUI End Sub Sub Globals Private fcvMap As cvMap Private shapes As List Dim PrecLT,PrecLg, Lg, Lt As Double End Sub Sub Activity_Create(FirstTime As Boolean) Activity.LoadLayout("Mappa_OSM") shapes.Initialize DownloadTraceGPS ' sub End sub Sub DownloadTraceGPS Private JobDownload As HttpJob JobDownload.Initialize("JobDownload", Me) JobDownload.Download(URL) Wait For (JobDownload) JobDone(JobDownload As HttpJob) If JobDownload.Success = True Then Dim out As OutputStream = File.OpenOutput(Main.GlobalThisApp, Main.TraceWeb, False) File.Copy2(JobDownload.GetInputStream, out) out.Close ReadShapes ' sub ShowMap ' sub end if End sub Sub ReadShapes dim N as int TextReader1.Initialize(File.OpenInput(Main.GlobalThisApp, Main.TraceWeb)) Do While Riga <> Null Riga = TextReader1.ReadLine ele = Regex.Split(",",Riga) Lt=ele(0).trim Lg= ele(1).trim if N>0 then shapes.Add(coMapUtilities.instanceShapeLine(fcvMap,coMapUtilities.initShapeLine(coMapUtilities.initLatLng( PrecLT,PrecLg),coMapUtilities.initLatLng(Lt, Lg),Colors.ARGB(128,255,0,255),2dip,"test"))) end if N=N+1 PrecLT=Lt:PrecLg=Lg Loop TextReader1.Close End sub Sub ShowMap Log(shapes.size) ' OK ' For Each ZZ As TMapShapeLine In shapes ' Log(ZZ.fLatLng1.fLat & " " & ZZ.fLatLng1.fLng & " TO " & ZZ.fLatLng2.fLat & " " & ZZ.fLatLng2.fLng) ' Next fMap=coMapUtilities.initMap(coMapUtilities.initLatLng(lt, lg), ZoomLevel, CompassDirection, OfflineMode , ShowMenu, ShowGrid, ShowCenter, ShowLandmark, ShowCenterLatLon, ShowZoom, ShowScale, ShowCompass, ShowShapes, _ shapes, _ ShowGPS, FollowGPS,coMapUtilities.initGPS(coMapUtilities.initLatLng(0,0),0)) end sub Sub Activity_Pause (UserClosed As Boolean) fMap=fcvMap.map End Sub private Sub fcvmap_ready fcvMap.TileServer="https://a.tile.openstreetmap.org/" ' default fcvMap.UserAgent="User-AgentMozilla/5.0 (Windows NT 10.0; Win64; x64; rv:108.0) Gecko/20100101 Firefox/108.0" fcvMap.Map=fMap '--- draw the map fcvMap.draw fcvMap.Map=fMap fcvMap.draw End Sub
Thanks in advance.
Could you post a small (as small as possible) demo project that demonstrates the problem?I also thought about inserting sleeps at various points but it didn't work. I have done numerous tests and attempts.
The cause is another.
Not even by inserting (for testing) MSGBOX.
Error occurred on line: 323 (cvMap)
java.lang.NullPointerException: Attempt to read from field 'double alpvir.sq.comaputilities$_tmaplatlng.fLat' on a null object reference in method 'java.lang.String alpvir.sq.cvmap._setcenterlatlng(alpvir.sonoqui.cvmap, alpvir.sq.comaputilities$_tmaplatlng)'
Could you send the full project, so including all the B4XMap classes?I made a small project that does NOT display the map.
Could you take a look at it ?
I get an error like :
EDIT: The line is missing in the project : Public gpsStarted As Boolean (but it is not significant)
OK, I understand now you are using B4XMap as a library and you have the classes of that library not in your project.I don't understand what you are referring to when you write about "B4XMap classes".
I must specify that the example attached to the previous post was created by me excluding many other modules from a very large project that includes 26 libraries, 11 modules (for now) and no B4XPage.
Compressed occupies about 800 KB and would be difficult for anyone to understand since it is written, where possible, in my native language (Italian) and interacts heavily with the Web.
Just to understand the complexity of the project, I report a part of the file Project Name.b4a
By starting the project attached to the previous post, the error (ie the NOT visualization) of the map is already evident.
Thank you if you want to help me with this project of mine which is the reworking of another one which included the use of the OSMDroid library, now no longer current.
I am again attaching the project in *.zip format
Attached all the files.Now I understand that your B4XMap code can be used as a "library" (as I have done so far) or as a "class".
I don't think these classes are available on this site.
Can you send them to me?
Thank you !
Conversely, I believe that the library can be usefully employed, without using the classes, as long as one understands the order in which the instructions relating to the map follow one another over time.clsMapTileManager - 378: Membro sconosciuto: reset
clsMapTileManager - 347: Membro sconosciuto: reset
clsMapTileManager - 338: Membro sconosciuto: reset
clsMapTileManager - 314: Membro sconosciuto: show
Main - 183: Membro sconosciuto: useragent
Main - 182: Membro sconosciuto: tileserver
MapUtilities - 301: Variabile 'general' non dichiarata, utilizzata prima che gli sia stato assegnato un valore.
MapUtilities - 300: Variabile 'general' non dichiarata, utilizzata prima che gli sia stato assegnato un valore.
clsMapTileManager - 382: Variabile 'general' non dichiarata, utilizzata prima che gli sia stato assegnato un valore.
clsMapTileManager - 293: Variabile 'general' non dichiarata, utilizzata prima che gli sia stato assegnato un valore.
Sorry, forgot about these General variables and Subs. General is a code module, but you can bypass all that.I'm sorry but I couldn't get anything done.
I inserted all 7 BAS modules that you attached to the previous post into the small test project.
I then unplugged the additional B4XMap library.
After some errors, remedied by inserting some additional modules (B4XProgressBar and other) I stopped in front of a "General" variable of which I know nothing and it is mentioned in none of the numerous modules.
These errors are difficult to interpret.
Conversely, I believe that the library can be usefully employed, without using the classes, as long as one understands the order in which the instructions relating to the map follow one another over time.
When is the fcvmap_read event called?
Where should fcvMap.draw be inserted? Immediately or after the track has been downloaded ?
I am convinced that a simple move of the instructions to the right place can finally make the test project work.
Would you please take another look?
Could you zip the full project again (with the classes added) and post it?I scrupulously followed all your instructions, but no, nothing happened.
Chasing the flow of execution of complex code like the classes in question is very daunting.
I think it is useless to report the errors that have gradually followed one another.
They returned to trying to use the library, hoping by dint of attempts to get something positive.
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?