Cleanup + layer update
This commit is contained in:
84
PBMap.pb
84
PBMap.pb
@@ -37,7 +37,8 @@ DeclareModule PBMap
|
||||
#SCALE_KM = 0
|
||||
|
||||
Declare InitPBMap(window)
|
||||
Declare SetMapServer(ServerURL.s = "http://tile.openstreetmap.org/", TileSize = 256, ZoomMin = 0, ZoomMax = 18)
|
||||
Declare.i AddMapServerLayer(LayerName.s, Order.i, ServerURL.s = "http://tile.openstreetmap.org/", TileSize = 256, ZoomMin = 0, ZoomMax = 18)
|
||||
Declare DeleteLayer(Nb.i)
|
||||
Declare MapGadget(Gadget.i, X.i, Y.i, Width.i, Height.i)
|
||||
Declare SetLocation(latitude.d, longitude.d, zoom = 15, mode.i = #PB_Absolute)
|
||||
Declare Drawing()
|
||||
@@ -86,7 +87,7 @@ Module PBMap
|
||||
CacheFile.s
|
||||
GetImageThread.i
|
||||
RetryNb.i
|
||||
Layer.i
|
||||
ServerURL.s
|
||||
EndStructure
|
||||
|
||||
Structure TileBounds
|
||||
@@ -137,6 +138,12 @@ Module PBMap
|
||||
ScaleUnit.i ; Scale unit to use for measurements
|
||||
EndStructure
|
||||
|
||||
Structure Layer
|
||||
Order.i ; Layer nb
|
||||
Name.s
|
||||
ServerURL.s ; Web URL ex: http://tile.openstreetmap.org/
|
||||
EndStructure
|
||||
|
||||
;-PBMap Structure
|
||||
Structure PBMap
|
||||
Window.i ; Parent Window
|
||||
@@ -152,8 +159,7 @@ Module PBMap
|
||||
PixelCoordinates.PixelCoordinates ; Actual focus point coords in pixels (global)
|
||||
MoveStartingPoint.PixelCoordinates ; Start mouse position coords when dragging the map
|
||||
;
|
||||
Array ServerURL.s(0) ; Web URL ex: http://tile.openstreetmap.org/
|
||||
NumberOfMapLayers.i ; The number of map tile layers;
|
||||
List Layers.Layer()
|
||||
|
||||
ZoomMin.i ; Min Zoom supported by server
|
||||
ZoomMax.i ; Max Zoom supported by server
|
||||
@@ -306,8 +312,7 @@ Module PBMap
|
||||
PBMap\Window = Window
|
||||
PBMap\Timer = 1
|
||||
PBMap\Options\WheelMouseRelative = #True
|
||||
SetMapServer("http://tile.openstreetmap.org/")
|
||||
|
||||
AddMapServerLayer("OSM", 1, "http://tile.openstreetmap.org/")
|
||||
;-Preferences
|
||||
;Use this to create and customize your preferences file for the first time
|
||||
; CreatePreferences(GetHomeDirectory() + "PBMap.prefs")
|
||||
@@ -336,13 +341,24 @@ Module PBMap
|
||||
TechnicalImagesCreation()
|
||||
EndProcedure
|
||||
|
||||
Procedure SetMapServer(ServerURL.s = "http://tile.openstreetmap.org/", TileSize = 256, ZoomMin = 0, ZoomMax = 18)
|
||||
PBMAP\NumberOfMapLayers + 1
|
||||
ReDim PBMap\ServerURL(PBMAP\NumberOfMapLayers)
|
||||
PBMap\ServerURL(PBMAP\NumberOfMapLayers-1) = ServerURL
|
||||
PBMap\ZoomMin = ZoomMin
|
||||
PBMap\ZoomMax = ZoomMax
|
||||
PBMap\TileSize = TileSize
|
||||
Procedure.i AddMapServerLayer(LayerName.s, Order.i, ServerURL.s = "http://tile.openstreetmap.org/", TileSize = 256, ZoomMin = 0, ZoomMax = 18)
|
||||
Protected *Ptr = AddElement(PBMap\Layers())
|
||||
If *Ptr
|
||||
PBMap\Layers()\Name = LayerName
|
||||
PBMap\Layers()\Order = Order
|
||||
PBMap\Layers()\ServerURL = ServerURL
|
||||
SortStructuredList(PBMap\Layers(), #PB_Sort_Ascending, OffsetOf(Layer\Order),TypeOf(Layer\Order))
|
||||
ProcedureReturn *Ptr
|
||||
Else
|
||||
ProcedureReturn #False
|
||||
EndIf
|
||||
EndProcedure
|
||||
|
||||
Procedure DeleteLayer(*Ptr)
|
||||
ChangeCurrentElement(PBMap\Layers(), *Ptr)
|
||||
DeleteElement(PBMap\Layers())
|
||||
FirstElement(PBMap\Layers())
|
||||
SortStructuredList(PBMap\Layers(), #PB_Sort_Ascending, OffsetOf(Layer\Order),TypeOf(Layer\Order))
|
||||
EndProcedure
|
||||
|
||||
Procedure Quit()
|
||||
@@ -546,11 +562,10 @@ Module PBMap
|
||||
ProcedureReturn -1
|
||||
EndProcedure
|
||||
|
||||
Procedure.i GetTileFromWeb(Zoom.i, XTile.i, YTile.i, CacheFile.s, Layer.i)
|
||||
Procedure.i GetTileFromWeb(TileURL.s, CacheFile.s)
|
||||
Protected *Buffer
|
||||
Protected nImage.i = -1
|
||||
Protected FileSize.i, timg
|
||||
Protected TileURL.s = PBMap\ServerURL(Layer) + Str(Zoom) + "/" + Str(XTile) + "/" + Str(YTile) + ".png"
|
||||
If Proxy
|
||||
FileSize = CurlReceiveHTTPToFile(TileURL, CacheFile, ProxyURL$, ProxyPort$, ProxyUser$, ProxyPassword$)
|
||||
If FileSize > 0
|
||||
@@ -595,8 +610,9 @@ Module PBMap
|
||||
|
||||
Procedure GetImageThread(*Tile.Tile)
|
||||
Protected nImage.i = -1
|
||||
Protected TileURL.s = *Tile\ServerURL + Str(*Tile\PBMapZoom) + "/" + Str(*Tile\PBMapTileX) + "/" + Str(*Tile\PBMapTileY) + ".png"
|
||||
Repeat
|
||||
nImage = GetTileFromWeb(*Tile\PBMapZoom, *Tile\PBMapTileX, *Tile\PBMapTileY, *Tile\CacheFile, *Tile\Layer)
|
||||
nImage = GetTileFromWeb(TileURL, *Tile\CacheFile)
|
||||
If nImage <> -1
|
||||
MyDebug("Image key : " + *Tile\key + " web image loaded", 3)
|
||||
*Tile\RetryNb = 0
|
||||
@@ -611,7 +627,7 @@ Module PBMap
|
||||
PostEvent(#PB_Event_Gadget, PBMap\Window, PBmap\Gadget, #PB_MAP_TILE_CLEANUP, *Tile) ;To free memory outside the thread
|
||||
EndProcedure
|
||||
|
||||
Procedure.i GetTile(key.s, CacheFile.s, px.i, py.i, tilex.i, tiley.i, Layer.i)
|
||||
Procedure.i GetTile(key.s, CacheFile.s, px.i, py.i, tilex.i, tiley.i, ServerURL.s)
|
||||
Protected timg = -1
|
||||
If FindMapElement(PBMap\MemCache\Images(), key)
|
||||
MyDebug("Key : " + key + " found in memory cache!", 3)
|
||||
@@ -647,7 +663,7 @@ Module PBMap
|
||||
\PBMapZoom = PBMap\Zoom
|
||||
\key = key
|
||||
\CacheFile = CacheFile
|
||||
\Layer = Layer
|
||||
\ServerURL = ServerURL
|
||||
\RetryNb = 5
|
||||
\nImage = -1
|
||||
MyDebug(" Creating get image thread nb " + Str(\GetImageThread) + " to get " + CacheFile, 3)
|
||||
@@ -660,7 +676,7 @@ Module PBMap
|
||||
ProcedureReturn timg
|
||||
EndProcedure
|
||||
|
||||
Procedure DrawTiles(*Drawing.DrawingParameters, Layer.i, alpha.i=255)
|
||||
Procedure DrawTiles(*Drawing.DrawingParameters, Layer, alpha.i=255)
|
||||
;DisableDebugger
|
||||
Protected x.i, y.i,kq.q
|
||||
Protected tx = Int(*Drawing\TileCoordinates\x) ;Don't forget the Int() !
|
||||
@@ -669,6 +685,7 @@ Module PBMap
|
||||
Protected ny = *Drawing\CenterY / PBMap\TileSize
|
||||
Protected px, py, img, tilex,tiley, key.s, CacheFile.s
|
||||
Protected tilemax = 1<<PBMap\Zoom
|
||||
SelectElement(PBMap\Layers(), Layer)
|
||||
MyDebug("Drawing tiles")
|
||||
For y = - ny - 1 To ny + 1
|
||||
For x = - nx - 1 To nx + 1
|
||||
@@ -680,11 +697,10 @@ Module PBMap
|
||||
EndIf
|
||||
tiley = ty + y
|
||||
If tiley >= 0 And tiley < tilemax
|
||||
kq = Layer | (pbmap\zoom << 8) | (tilex << 16) | (tiley << 36)
|
||||
key = Str(kq)
|
||||
kq = (PBMap\zoom << 8) | (tilex << 16) | (tiley << 36)
|
||||
key = PBMap\Layers()\Name + Str(kq)
|
||||
CacheFile = PBMap\HDDCachePath + key + ".png"
|
||||
|
||||
img = GetTile(key, CacheFile, px, py, tilex, tiley, Layer)
|
||||
img = GetTile(key, CacheFile, px, py, tilex, tiley, PBMap\Layers()\ServerURL)
|
||||
If img <> -1
|
||||
MovePathCursor(px, py)
|
||||
DrawVectorImage(ImageID(img), alpha)
|
||||
@@ -693,7 +709,7 @@ Module PBMap
|
||||
DrawVectorImage(ImageID(PBMap\ImgLoading), alpha)
|
||||
EndIf
|
||||
Else
|
||||
If Layer = 0
|
||||
If PBMap\Layers()\Name = ""
|
||||
MovePathCursor(px, py)
|
||||
DrawVectorImage(ImageID(PBMap\ImgNothing))
|
||||
EndIf
|
||||
@@ -948,8 +964,8 @@ Module PBMap
|
||||
StartVectorDrawing(CanvasVectorOutput(PBMap\Gadget))
|
||||
;TODO add in layers of tiles ;this way we can cache them as 0 base 1.n layers
|
||||
;such as for openseamap tiles which are overlaid. not that efficent from here though.
|
||||
For a = 0 To PBMap\NumberOfMapLayers - 1
|
||||
DrawTiles(*Drawing, a)
|
||||
ForEach PBMap\Layers()
|
||||
DrawTiles(*Drawing, ListIndex(PBMap\Layers()))
|
||||
Next
|
||||
DrawTrack(*Drawing)
|
||||
DrawMarkers(*Drawing)
|
||||
@@ -1308,6 +1324,7 @@ CompilerIf #PB_Compiler_IsMainFile
|
||||
#String_1
|
||||
#Gdt_LoadGpx
|
||||
#Gdt_AddMarker
|
||||
#Gdt_AddOpenseaMap
|
||||
EndEnumeration
|
||||
|
||||
Structure Location
|
||||
@@ -1356,6 +1373,7 @@ CompilerIf #PB_Compiler_IsMainFile
|
||||
ResizeGadget(#Text_4,WindowWidth(#Window_0)-170,#PB_Ignore,#PB_Ignore,#PB_Ignore)
|
||||
ResizeGadget(#Gdt_AddMarker,WindowWidth(#Window_0)-170,#PB_Ignore,#PB_Ignore,#PB_Ignore)
|
||||
ResizeGadget(#Gdt_LoadGpx,WindowWidth(#Window_0)-170,#PB_Ignore,#PB_Ignore,#PB_Ignore)
|
||||
ResizeGadget(#Gdt_AddOpenseaMap,WindowWidth(#Window_0)-170,#PB_Ignore,#PB_Ignore,#PB_Ignore)
|
||||
PBMap::Refresh()
|
||||
EndProcedure
|
||||
|
||||
@@ -1379,9 +1397,11 @@ CompilerIf #PB_Compiler_IsMainFile
|
||||
StringGadget(#String_1, 600, 250, 90, 20, "")
|
||||
ButtonGadget(#Gdt_AddMarker, 530, 280, 150, 30, "Add Marker")
|
||||
ButtonGadget(#Gdt_LoadGpx, 530, 310, 150, 30, "Load GPX")
|
||||
ButtonGadget(#Gdt_AddOpenseaMap, 530, 340, 150, 30, "OpenSeaMap")
|
||||
|
||||
Define Event.i, Gadget.i, Quit.b = #False
|
||||
Define pfValue.d
|
||||
Define OpenSeaMap = 0
|
||||
|
||||
;Our main gadget
|
||||
PBMap::InitPBMap(#Window_0)
|
||||
@@ -1390,7 +1410,6 @@ CompilerIf #PB_Compiler_IsMainFile
|
||||
PBMap::SetCallBackLocation(@UpdateLocation())
|
||||
PBMap::SetLocation(-36.81148, 175.08634,12)
|
||||
;PBMap::SetLocation(0, 0)
|
||||
PBMap::SetMapServer("http://t1.openseamap.org/seamark/") ;add a special osm overlay map
|
||||
PBMAP::SetMapScaleUnit(PBMAP::#SCALE_NAUTICAL)
|
||||
PBMap::AddMarker(49.0446828398, 2.0349812508, -1, @MyMarker())
|
||||
|
||||
@@ -1418,6 +1437,14 @@ CompilerIf #PB_Compiler_IsMainFile
|
||||
PBMap::ZoomToArea() ; <-To center the view, and zoom on the tracks
|
||||
Case #Gdt_AddMarker
|
||||
PBMap::AddMarker(ValD(GetGadgetText(#String_0)), ValD(GetGadgetText(#String_1)), RGBA(Random(255), Random(255), Random(255), 255), @MyMarker())
|
||||
Case #Gdt_AddOpenseaMap
|
||||
If OpenSeaMap = 0
|
||||
OpenSeaMap = PBMap::AddMapServerLayer("OpenSeaMap", 2, "http://t1.openseamap.org/seamark/") ;add a special osm overlay map
|
||||
Else
|
||||
PBMap::DeleteLayer(OpenSeaMap)
|
||||
OpenSeaMap = 0
|
||||
EndIf
|
||||
PBMAP::Refresh()
|
||||
EndSelect
|
||||
Case #PB_Event_SizeWindow
|
||||
ResizeAll()
|
||||
@@ -1429,7 +1456,8 @@ CompilerIf #PB_Compiler_IsMainFile
|
||||
|
||||
CompilerEndIf
|
||||
; IDE Options = PureBasic 5.50 (Windows - x64)
|
||||
; CursorPosition = 8
|
||||
; CursorPosition = 310
|
||||
; FirstLine = 291
|
||||
; Folding = -----------
|
||||
; EnableThread
|
||||
; EnableXP
|
||||
|
Reference in New Issue
Block a user