This commit is contained in:
djes
2016-09-06 12:46:50 +02:00
parent 17f999f11c
commit b2b6a48d89

212
PBMap.pb
View File

@@ -65,16 +65,16 @@ Module PBMap
Latitude.d Latitude.d
EndStructure EndStructure
Structure PixelCoordinates
x.i
y.i
EndStructure
Structure Coordinates Structure Coordinates
x.d x.d
y.d y.d
EndStructure EndStructure
Structure PixelPosition
x.i
y.i
EndStructure
;- Tile Structure ;- Tile Structure
Structure Tile Structure Tile
Position.Coordinates Position.Coordinates
@@ -95,13 +95,13 @@ Module PBMap
EndStructure EndStructure
Structure DrawingParameters Structure DrawingParameters
TilePosition.Coordinates TileCoordinates.Coordinates
Bounds.TileBounds Bounds.TileBounds
Canvas.i Canvas.i
PBMapTileX.i PBMapTileX.i
PBMapTileY.i PBMapTileY.i
PBMapZoom.i PBMapZoom.i
TargetLocation.GeographicCoordinates GeographicCoordinates.GeographicCoordinates
CenterX.i CenterX.i
CenterY.i CenterY.i
DeltaX.i DeltaX.i
@@ -128,7 +128,7 @@ Module PBMap
EndStructure EndStructure
Structure Marker Structure Marker
Location.GeographicCoordinates ; Marker latitude and longitude GeographicCoordinates.GeographicCoordinates ; Marker latitude and longitude
color.l ; Marker color color.l ; Marker color
CallBackPointer.i ; @Procedure(X.i, Y.i) to DrawPointer (you must use VectorDrawing lib) CallBackPointer.i ; @Procedure(X.i, Y.i) to DrawPointer (you must use VectorDrawing lib)
EndStructure EndStructure
@@ -144,14 +144,14 @@ Module PBMap
Gadget.i ; Canvas Gadget Id Gadget.i ; Canvas Gadget Id
Font.i ; Font to uses when write on the map Font.i ; Font to uses when write on the map
Timer.i Timer.i
TargetLocation.GeographicCoordinates ; Latitude and Longitude from focus point GeographicCoordinates.GeographicCoordinates ; Latitude and Longitude from focus point
Drawing.DrawingParameters ; Drawing parameters based on focus point Drawing.DrawingParameters ; Drawing parameters based on focus point
; ;
CallBackLocation.i ; @Procedure(latitude.d,lontitude.d) CallBackLocation.i ; @Procedure(latitude.d,lontitude.d)
CallBackMainPointer.i ; @Procedure(X.i, Y.i) to DrawPointer (you must use VectorDrawing lib) CallBackMainPointer.i ; @Procedure(X.i, Y.i) to DrawPointer (you must use VectorDrawing lib)
; ;
PixelPosition.PixelPosition ; Actual focus point coords in pixels (global) PixelCoordinates.PixelCoordinates ; Actual focus point coords in pixels (global)
MoveStartingPoint.PixelPosition ; Start mouse position coords when dragging the map MoveStartingPoint.PixelCoordinates ; Start mouse position coords when dragging the map
; ;
Array ServerURL.s(0) ; Web URL ex: http://tile.openstreetmap.org/ Array ServerURL.s(0) ; Web URL ex: http://tile.openstreetmap.org/
NumberOfMapLayers.i ; The number of map tile layers; NumberOfMapLayers.i ; The number of map tile layers;
@@ -401,22 +401,22 @@ Module PBMap
;Warning, structures used in parameters are not tested ;Warning, structures used in parameters are not tested
Procedure TileXY2LatLon(*Coords.Coordinates, *Location.GeographicCoordinates, Zoom) Procedure TileXY2LatLon(*Coords.Coordinates, *Location.GeographicCoordinates, Zoom)
Protected n.d = Pow(2.0, Zoom) Protected n.d = Pow(2.0, Zoom)
Protected LatitudeRad.d
;Ensures the longitude to be in the range [-180;180[ ;Ensures the longitude to be in the range [-180;180[
*Location\Longitude = Mod(Mod(*Coords\x / n * 360.0, 360.0) + 360.0, 360.0) - 180 *Location\Longitude = Mod(Mod(*Coords\x / n * 360.0, 360.0) + 360.0, 360.0) - 180
; *Location\Longitude = Mod(*Coords\x / n * 360.0, 360.0) *Location\Latitude = Degree(ATan(SinH(#PI * (1.0 - 2.0 * *Coords\y / n))))
; If *Location\Longitude < 0
; *Location\Longitude + 360
; EndIf
; *Location\Longitude = Mod(*Location\Longitude, 360.0) - 180
LatitudeRad = ATan(SinH(#PI * (1.0 - 2.0 * *Coords\y / n)))
*Location\Latitude = Degree(LatitudeRad)
EndProcedure EndProcedure
Procedure LatLon2Pixel(*Location.GeographicCoordinates, *Pixel.PixelPosition, Zoom) Procedure Pixel2LatLon(*Coords.PixelCoordinates, *Location.GeographicCoordinates, Zoom)
Protected n.d = PBMap\TileSize * Pow(2.0, Zoom)
;Ensures the longitude to be in the range [-180;180[
*Location\Longitude = Mod(Mod(*Coords\x / n * 360.0, 360.0) + 360.0, 360.0) - 180
*Location\Latitude = Degree(ATan(SinH(#PI * (1.0 - 2.0 * *Coords\y / n))))
EndProcedure
Procedure LatLon2Pixel(*Location.GeographicCoordinates, *Pixel.PixelCoordinates, Zoom)
Protected Pos.Coordinates Protected Pos.Coordinates
Protected tilemax = Pow(2.0, Zoom) Protected tilemax = Pow(2.0, Zoom)
Protected cx.d = PBMap\Drawing\CenterX, dpx.d = PBMap\Drawing\TilePosition\x Protected cx.d = PBMap\Drawing\CenterX, dpx.d = PBMap\Drawing\TileCoordinates\x
LatLon2TileXY(*Location, @Pos, Zoom) LatLon2TileXY(*Location, @Pos, Zoom)
Protected px.d = Pos\x Protected px.d = Pos\x
;check the x boundaries of the map to adjust the position (coz of the longitude wrapping) ;check the x boundaries of the map to adjust the position (coz of the longitude wrapping)
@@ -433,7 +433,7 @@ Module PBMap
;Debug "c0" ;Debug "c0"
*Pixel\x = cx + (px - dpx) * PBMap\TileSize *Pixel\x = cx + (px - dpx) * PBMap\TileSize
EndIf EndIf
*Pixel\y = PBMap\Drawing\CenterY + (Pos\y - PBMap\Drawing\TilePosition\y) * PBMap\TileSize *Pixel\y = PBMap\Drawing\CenterY + (Pos\y - PBMap\Drawing\TileCoordinates\y) * PBMap\TileSize
;Debug "Longitude : " + StrD(*Location\Longitude) + " ; Pos : " + StrD(Pos\x) + " ; Drawing pos : " + StrD(PBMap\Drawing\Position\x) + "/" + Str(tilemax) + ;Debug "Longitude : " + StrD(*Location\Longitude) + " ; Pos : " + StrD(Pos\x) + " ; Drawing pos : " + StrD(PBMap\Drawing\Position\x) + "/" + Str(tilemax) +
; " ; XY : " + Str(*Pixel\x) + "," + Str(*Pixel\y) ; " ; XY : " + Str(*Pixel\x) + "," + Str(*Pixel\y)
EndProcedure EndProcedure
@@ -456,7 +456,7 @@ Module PBMap
ProcedureReturn (1000 * HaversineInKM(@*posA,@*posB)); ProcedureReturn (1000 * HaversineInKM(@*posA,@*posB));
EndProcedure EndProcedure
Procedure GetPixelCoordFromLocation(*Location.GeographicCoordinates, *Pixel.PixelPosition, Zoom) ; TODO to Optimize Procedure GetPixelCoordFromLocation(*Location.GeographicCoordinates, *Pixel.PixelCoordinates, Zoom) ; TODO to Optimize
Protected mapWidth.l = Pow(2, Zoom + 8) Protected mapWidth.l = Pow(2, Zoom + 8)
Protected mapHeight.l = Pow(2, Zoom + 8) Protected mapHeight.l = Pow(2, Zoom + 8)
Protected x1.l,y1.l Protected x1.l,y1.l
@@ -471,13 +471,13 @@ Module PBMap
; Debug y1 ; Debug y1
Protected x2.l, y2.l Protected x2.l, y2.l
; get x value ; get x value
x2 = (PBMap\TargetLocation\Longitude+180)*(mapWidth/360) x2 = (PBMap\GeographicCoordinates\Longitude+180)*(mapWidth/360)
; convert from degrees To radians ; convert from degrees To radians
latRad = PBMap\TargetLocation\Latitude*#PI/180; latRad = PBMap\GeographicCoordinates\Latitude*#PI/180;
; get y value ; get y value
mercN = Log(Tan((#PI/4)+(latRad/2))) mercN = Log(Tan((#PI/4)+(latRad/2)))
y2 = (mapHeight/2)-(mapWidth*mercN/(2*#PI)); y2 = (mapHeight/2)-(mapWidth*mercN/(2*#PI));
; Debug "targetlocation" ; Debug "GeographicCoordinates"
; Debug x1 ; Debug x1
; Debug y1 ; Debug y1
*Pixel\x=GadgetWidth(PBMap\Gadget)/2 - (x2-x1) *Pixel\x=GadgetWidth(PBMap\Gadget)/2 - (x2-x1)
@@ -654,8 +654,8 @@ Module PBMap
Procedure DrawTiles(*Drawing.DrawingParameters, Layer.i, alpha.i=255) Procedure DrawTiles(*Drawing.DrawingParameters, Layer.i, alpha.i=255)
;DisableDebugger ;DisableDebugger
Protected x.i, y.i,kq.q Protected x.i, y.i,kq.q
Protected tx = Int(*Drawing\TilePosition\x) ;Don't forget the Int() ! Protected tx = Int(*Drawing\TileCoordinates\x) ;Don't forget the Int() !
Protected ty = Int(*Drawing\TilePosition\y) Protected ty = Int(*Drawing\TileCoordinates\y)
Protected nx = *Drawing\CenterX / PBMap\TileSize ;How many tiles around the point Protected nx = *Drawing\CenterX / PBMap\TileSize ;How many tiles around the point
Protected ny = *Drawing\CenterY / PBMap\TileSize Protected ny = *Drawing\CenterY / PBMap\TileSize
Protected px, py, img, tilex,tiley, key.s, CacheFile.s Protected px, py, img, tilex,tiley, key.s, CacheFile.s
@@ -702,11 +702,11 @@ Module PBMap
; Else ; Else
; MaxNbTile=GadgetHeight(PBMap\Gadget)/PBMap\TileSize ; MaxNbTile=GadgetHeight(PBMap\Gadget)/PBMap\TileSize
; EndIf ; EndIf
; Protected Scale.d= 40075*Cos(Radian(PBMap\TargetLocation\Latitude))/Pow(2,PBMap\Zoom) ; Protected Scale.d= 40075*Cos(Radian(PBMap\GeographicCoordinates\Latitude))/Pow(2,PBMap\Zoom)
; Protected Limit.d=Scale*(MaxNbTile)*1.5 ; Protected Limit.d=Scale*(MaxNbTile)*1.5
; Debug "Cache cleaning" ; Debug "Cache cleaning"
; ForEach PBMap\MemCache\Images() ; ForEach PBMap\MemCache\Images()
; Protected Distance.d = HaversineInKM(@PBMap\MemCache\Images()\Location, @PBMap\TargetLocation) ; Protected Distance.d = HaversineInKM(@PBMap\MemCache\Images()\Location, @PBMap\GeographicCoordinates)
; Debug "Limit:"+StrD(Limit)+" Distance:"+StrD(Distance) ; Debug "Limit:"+StrD(Limit)+" Distance:"+StrD(Distance)
; If Distance>Limit And IsImage(PBMap\MemCache\Images()\nImage) ; If Distance>Limit And IsImage(PBMap\MemCache\Images()\nImage)
; LockMutex(PBMap\MemCache\Images()\Mutex) ; LockMutex(PBMap\MemCache\Images()\Mutex)
@@ -741,7 +741,7 @@ Module PBMap
Procedure DrawScale(*Drawing.DrawingParameters,x,y,alpha=80) Procedure DrawScale(*Drawing.DrawingParameters,x,y,alpha=80)
;TODO Add Option and function to display Scale on Map ;TODO Add Option and function to display Scale on Map
Protected sunit.s Protected sunit.s
Protected Scale.d= 40075*Cos(Radian(PBMap\TargetLocation\Latitude))/Pow(2,PBMap\Zoom) / 2 Protected Scale.d= 40075*Cos(Radian(PBMap\GeographicCoordinates\Latitude))/Pow(2,PBMap\Zoom) / 2
Select PBMap\Options\ScaleUnit Select PBMap\Options\ScaleUnit
Case #SCALE_Nautical Case #SCALE_Nautical
@@ -762,10 +762,10 @@ Module PBMap
Procedure DrawDegrees(*Drawing.DrawingParameters, alpha=192) Procedure DrawDegrees(*Drawing.DrawingParameters, alpha=192)
Protected tx, ty, nx,ny,nx1,ny1,x,y,n,cx,dperpixel.d Protected tx, ty, nx,ny,nx1,ny1,x,y,n,cx,dperpixel.d
Protected pos1.PixelPosition,pos2.PixelPosition,Degrees1.GeographicCoordinates,degrees2.GeographicCoordinates Protected pos1.PixelCoordinates,pos2.PixelCoordinates,Degrees1.GeographicCoordinates,degrees2.GeographicCoordinates
Protected realx Protected realx
tx = Int(*Drawing\TilePosition\x) tx = Int(*Drawing\TileCoordinates\x)
ty = Int(*Drawing\TilePosition\y) ty = Int(*Drawing\TileCoordinates\y)
nx = *Drawing\CenterX / PBMap\TileSize ;How many tiles around the point nx = *Drawing\CenterX / PBMap\TileSize ;How many tiles around the point
ny = *Drawing\CenterY / PBMap\TileSize ny = *Drawing\CenterY / PBMap\TileSize
*Drawing\Bounds\NorthWest\x = tx-nx-1 *Drawing\Bounds\NorthWest\x = tx-nx-1
@@ -834,13 +834,13 @@ Module PBMap
EndProcedure EndProcedure
Procedure DrawTrack(*Drawing.DrawingParameters) Procedure DrawTrack(*Drawing.DrawingParameters)
Protected Pixel.PixelPosition Protected Pixel.PixelCoordinates
Protected Location.GeographicCoordinates Protected Location.GeographicCoordinates
Protected km.f, memKm.i Protected km.f, memKm.i
If ListSize(PBMap\track())>0 If ListSize(PBMap\track())>0
;Trace Track ;Trace Track
ForEach PBMap\track() ForEach PBMap\track()
If *Drawing\TargetLocation\Latitude<>0 And *Drawing\TargetLocation\Longitude<>0 If *Drawing\GeographicCoordinates\Latitude<>0 And *Drawing\GeographicCoordinates\Longitude<>0
;GetPixelCoordFromLocation(@PBMap\track(),@Pixel) ;GetPixelCoordFromLocation(@PBMap\track(),@Pixel)
LatLon2Pixel(@PBMap\track(), @Pixel, PBMap\Zoom) LatLon2Pixel(@PBMap\track(), @Pixel, PBMap\Zoom)
If ListIndex(PBMap\track())=0 If ListIndex(PBMap\track())=0
@@ -894,8 +894,8 @@ Module PBMap
; Add a Marker To the Map ; Add a Marker To the Map
Procedure AddMarker(Latitude.d, Longitude.d, color.l=-1, CallBackPointer.i = -1) Procedure AddMarker(Latitude.d, Longitude.d, color.l=-1, CallBackPointer.i = -1)
AddElement(PBMap\Marker()) AddElement(PBMap\Marker())
PBMap\Marker()\Location\Latitude = Latitude PBMap\Marker()\GeographicCoordinates\Latitude = Latitude
PBMap\Marker()\Location\Longitude = Mod(Mod(Longitude, 360) + 360, 360) PBMap\Marker()\GeographicCoordinates\Longitude = Mod(Mod(Longitude, 360) + 360, 360)
PBMap\Marker()\color = color PBMap\Marker()\color = color
PBMap\Marker()\CallBackPointer = CallBackPointer PBMap\Marker()\CallBackPointer = CallBackPointer
PBMap\Redraw = #True PBMap\Redraw = #True
@@ -903,11 +903,11 @@ Module PBMap
; Draw all markers on the screen ! ; Draw all markers on the screen !
Procedure DrawMarkers(*Drawing.DrawingParameters) Procedure DrawMarkers(*Drawing.DrawingParameters)
Protected Pixel.PixelPosition Protected Pixel.PixelCoordinates
ForEach PBMap\Marker() ForEach PBMap\Marker()
If PBMap\Marker()\Location\Latitude <> 0 And PBMap\Marker()\Location\Longitude <> 0 If PBMap\Marker()\GeographicCoordinates\Latitude <> 0 And PBMap\Marker()\GeographicCoordinates\Longitude <> 0
;GetPixelCoordFromLocation(PBMap\Marker()\Location, @Pixel) ;GetPixelCoordFromLocation(PBMap\Marker()\GeographicCoordinates, @Pixel)
LatLon2Pixel(PBMap\Marker()\Location, @Pixel, PBMap\Zoom) LatLon2Pixel(PBMap\Marker()\GeographicCoordinates, @Pixel, PBMap\Zoom)
If Pixel\X >= 0 And Pixel\Y >= 0 And Pixel\X < GadgetWidth(PBMap\Gadget) And Pixel\Y < GadgetHeight(PBMap\Gadget) ; Only if visible ^_^ If Pixel\X >= 0 And Pixel\Y >= 0 And Pixel\X < GadgetWidth(PBMap\Gadget) And Pixel\Y < GadgetHeight(PBMap\Gadget) ; Only if visible ^_^
If PBMap\Marker()\CallBackPointer > 0 If PBMap\Marker()\CallBackPointer > 0
CallFunctionFast(PBMap\Marker()\CallBackPointer, Pixel\X, Pixel\Y) CallFunctionFast(PBMap\Marker()\CallBackPointer, Pixel\X, Pixel\Y)
@@ -928,12 +928,13 @@ Module PBMap
;Precalc some values ;Precalc some values
*Drawing\CenterX = GadgetWidth(PBMap\Gadget) / 2 *Drawing\CenterX = GadgetWidth(PBMap\Gadget) / 2
*Drawing\CenterY = GadgetHeight(PBMap\Gadget) / 2 *Drawing\CenterY = GadgetHeight(PBMap\Gadget) / 2
*Drawing\GeographicCoordinates\Latitude = PBMap\GeographicCoordinates\Latitude
*Drawing\GeographicCoordinates\Longitude = PBMap\GeographicCoordinates\Longitude
LatLon2TileXY(*Drawing\GeographicCoordinates, *Drawing\TileCoordinates, PBMap\Zoom)
;Pixel shift, aka position in the tile ;Pixel shift, aka position in the tile
Px = *Drawing\TilePosition\x : Py = *Drawing\TilePosition\y Px = *Drawing\TileCoordinates\x : Py = *Drawing\TileCoordinates\y
*Drawing\DeltaX = Px * ts - (Int(Px) * ts) ;Don't forget the Int() ! *Drawing\DeltaX = Px * ts - (Int(Px) * ts) ;Don't forget the Int() !
*Drawing\DeltaY = Py * ts - (Int(Py) * ts) *Drawing\DeltaY = Py * ts - (Int(Py) * ts)
*Drawing\TargetLocation\Latitude = PBMap\TargetLocation\Latitude
*Drawing\TargetLocation\Longitude = PBMap\TargetLocation\Longitude
;Main drawing stuff ;Main drawing stuff
StartVectorDrawing(CanvasVectorOutput(PBMap\Gadget)) StartVectorDrawing(CanvasVectorOutput(PBMap\Gadget))
;TODO add in layers of tiles ;this way we can cache them as 0 base 1.n layers ;TODO add in layers of tiles ;this way we can cache them as 0 base 1.n layers
@@ -972,27 +973,27 @@ Module PBMap
EndProcedure EndProcedure
Procedure.d Longitude(x) Procedure.d Longitude(x)
Protected NewX.d = (PBMap\PixelPosition\x - GadgetWidth(PBMap\Gadget) / 2 + x) / PBMap\TileSize Protected NewX.d = (PBMap\PixelCoordinates\x - GadgetWidth(PBMap\Gadget) / 2 + x) / PBMap\TileSize
Protected n.d = Pow(2.0, PBMap\Zoom) Protected n.d = Pow(2.0, PBMap\Zoom)
;double mod is to ensure the longitude to be in the range [-180;180[ ;double mod is to ensure the longitude to be in the range [-180;180[
ProcedureReturn Mod(Mod(NewX / n * 360.0, 360.0) + 360.0, 360.0) - 180 ProcedureReturn Mod(Mod(NewX / n * 360.0, 360.0) + 360.0, 360.0) - 180
EndProcedure EndProcedure
Procedure.d Latitude(y) Procedure.d Latitude(y)
Protected NewY.d = (PBMap\PixelPosition\y - GadgetHeight(PBMap\Gadget) / 2 + y) / PBMap\TileSize Protected NewY.d = (PBMap\PixelCoordinates\y - GadgetHeight(PBMap\Gadget) / 2 + y) / PBMap\TileSize
Protected n.d = Pow(2.0, PBMap\Zoom) Protected n.d = Pow(2.0, PBMap\Zoom)
ProcedureReturn Degree(ATan(SinH(#PI * (1.0 - 2.0 * NewY / n)))) ProcedureReturn Degree(ATan(SinH(#PI * (1.0 - 2.0 * NewY / n))))
EndProcedure EndProcedure
Procedure.d MouseLongitude() Procedure.d MouseLongitude()
Protected MouseX.d = (PBMap\PixelPosition\x - GadgetWidth(PBMap\Gadget) / 2 + GetGadgetAttribute(PBMap\Gadget, #PB_Canvas_MouseX)) / PBMap\TileSize Protected MouseX.d = (PBMap\PixelCoordinates\x - GadgetWidth(PBMap\Gadget) / 2 + GetGadgetAttribute(PBMap\Gadget, #PB_Canvas_MouseX)) / PBMap\TileSize
Protected n.d = Pow(2.0, PBMap\Zoom) Protected n.d = Pow(2.0, PBMap\Zoom)
;double mod is to ensure the longitude to be in the range [-180;180[ ;double mod is to ensure the longitude to be in the range [-180;180[
ProcedureReturn Mod(Mod(MouseX / n * 360.0, 360.0) + 360.0, 360.0) - 180 ProcedureReturn Mod(Mod(MouseX / n * 360.0, 360.0) + 360.0, 360.0) - 180
EndProcedure EndProcedure
Procedure.d MouseLatitude() Procedure.d MouseLatitude()
Protected MouseY.d = (PBMap\PixelPosition\y - GadgetHeight(PBMap\Gadget) / 2 + GetGadgetAttribute(PBMap\Gadget, #PB_Canvas_MouseY)) / PBMap\TileSize Protected MouseY.d = (PBMap\PixelCoordinates\y - GadgetHeight(PBMap\Gadget) / 2 + GetGadgetAttribute(PBMap\Gadget, #PB_Canvas_MouseY)) / PBMap\TileSize
Protected n.d = Pow(2.0, PBMap\Zoom) Protected n.d = Pow(2.0, PBMap\Zoom)
ProcedureReturn Degree(ATan(SinH(#PI * (1.0 - 2.0 * MouseY / n)))) ProcedureReturn Degree(ATan(SinH(#PI * (1.0 - 2.0 * MouseY / n))))
EndProcedure EndProcedure
@@ -1000,25 +1001,25 @@ Module PBMap
Procedure SetLocation(latitude.d, longitude.d, zoom = 15, Mode.i = #PB_Absolute) Procedure SetLocation(latitude.d, longitude.d, zoom = 15, Mode.i = #PB_Absolute)
Select Mode Select Mode
Case #PB_Absolute Case #PB_Absolute
PBMap\TargetLocation\Latitude = latitude PBMap\GeographicCoordinates\Latitude = latitude
PBMap\TargetLocation\Longitude = longitude PBMap\GeographicCoordinates\Longitude = longitude
PBMap\Zoom = zoom PBMap\Zoom = zoom
Case #PB_Relative Case #PB_Relative
PBMap\TargetLocation\Latitude + latitude PBMap\GeographicCoordinates\Latitude + latitude
PBMap\TargetLocation\Longitude + longitude PBMap\GeographicCoordinates\Longitude + longitude
PBMap\Zoom + zoom PBMap\Zoom + zoom
EndSelect EndSelect
If PBMap\Zoom > PBMap\ZoomMax : PBMap\Zoom = PBMap\ZoomMax : EndIf If PBMap\Zoom > PBMap\ZoomMax : PBMap\Zoom = PBMap\ZoomMax : EndIf
If PBMap\Zoom < PBMap\ZoomMin : PBMap\Zoom = PBMap\ZoomMin : EndIf If PBMap\Zoom < PBMap\ZoomMin : PBMap\Zoom = PBMap\ZoomMin : EndIf
LatLon2TileXY(@PBMap\TargetLocation, @PBMap\Drawing, PBMap\Zoom) LatLon2TileXY(@PBMap\GeographicCoordinates, @PBMap\Drawing\TileCoordinates, PBMap\Zoom)
;Convert X, Y in tile.decimal into real pixels ;Convert X, Y in tile.decimal into real pixels
PBMap\PixelPosition\x = PBMap\Drawing\TilePosition\x * PBMap\TileSize PBMap\PixelCoordinates\x = PBMap\Drawing\TileCoordinates\x * PBMap\TileSize
PBMap\PixelPosition\y = PBMap\Drawing\TilePosition\y * PBMap\TileSize PBMap\PixelCoordinates\y = PBMap\Drawing\TileCoordinates\y * PBMap\TileSize
PBMap\Drawing\PassNb = 1 PBMap\Drawing\PassNb = 1
PBMap\Redraw = #True PBMap\Redraw = #True
;Drawing() ;Drawing()
If PBMap\CallBackLocation > 0 If PBMap\CallBackLocation > 0
CallFunctionFast(PBMap\CallBackLocation, @PBMap\TargetLocation) CallFunctionFast(PBMap\CallBackLocation, @PBMap\GeographicCoordinates)
EndIf EndIf
EndProcedure EndProcedure
@@ -1060,7 +1061,7 @@ Module PBMap
Protected lat.d = centerY; Protected lat.d = centerY;
SetLocation(lat,lon, Round(zoom,#PB_Round_Down)) SetLocation(lat,lon, Round(zoom,#PB_Round_Down))
Else Else
SetLocation(PBMap\TargetLocation\Latitude, PBMap\TargetLocation\Longitude, 15) SetLocation(PBMap\GeographicCoordinates\Latitude, PBMap\GeographicCoordinates\Longitude, 15)
EndIf EndIf
EndProcedure EndProcedure
@@ -1073,15 +1074,15 @@ Module PBMap
EndSelect EndSelect
If PBMap\Zoom > PBMap\ZoomMax : PBMap\Zoom = PBMap\ZoomMax : EndIf If PBMap\Zoom > PBMap\ZoomMax : PBMap\Zoom = PBMap\ZoomMax : EndIf
If PBMap\Zoom < PBMap\ZoomMin : PBMap\Zoom = PBMap\ZoomMin : EndIf If PBMap\Zoom < PBMap\ZoomMin : PBMap\Zoom = PBMap\ZoomMin : EndIf
LatLon2TileXY(@PBMap\TargetLocation, @PBMap\Drawing\TilePosition, PBMap\Zoom) LatLon2TileXY(@PBMap\GeographicCoordinates, @PBMap\Drawing\TileCoordinates, PBMap\Zoom)
;Convert X, Y in tile.decimal into real pixels ;Convert X, Y in tile.decimal into real pixels
PBMap\PixelPosition\X = PBMap\Drawing\TilePosition\x * PBMap\TileSize PBMap\PixelCoordinates\X = PBMap\Drawing\TileCoordinates\x * PBMap\TileSize
PBMap\PixelPosition\Y = PBMap\Drawing\TilePosition\y * PBMap\TileSize PBMap\PixelCoordinates\Y = PBMap\Drawing\TileCoordinates\y * PBMap\TileSize
;First drawing ;First drawing
PBMap\Redraw = #True PBMap\Redraw = #True
;Drawing() ;Drawing()
If PBMap\CallBackLocation > 0 If PBMap\CallBackLocation > 0
CallFunctionFast(PBMap\CallBackLocation, @PBMap\TargetLocation) CallFunctionFast(PBMap\CallBackLocation, @PBMap\GeographicCoordinates)
EndIf EndIf
EndProcedure EndProcedure
@@ -1110,7 +1111,7 @@ Module PBMap
Protected CenterX = GadgetWidth(PBMap\Gadget) / 2, CenterY = GadgetHeight(PBMap\Gadget) / 2 Protected CenterX = GadgetWidth(PBMap\Gadget) / 2, CenterY = GadgetHeight(PBMap\Gadget) / 2
;Fast and dirty code ;Fast and dirty code
OldPx = PBMap\PixelPosition\x : OldPy = PBMap\PixelPosition\y OldPx = PBMap\PixelCoordinates\x : OldPy = PBMap\PixelCoordinates\y
OldMx = OldPx + CenterX - x OldMx = OldPx + CenterX - x
OldMy = OldPy + CenterY - y OldMy = OldPy + CenterY - y
PBMap\Zoom = PBMap\Zoom + zoom PBMap\Zoom = PBMap\Zoom + zoom
@@ -1119,10 +1120,10 @@ Module PBMap
Protected MapWidth = Pow(2, PBMap\Zoom) * PBMap\TileSize Protected MapWidth = Pow(2, PBMap\Zoom) * PBMap\TileSize
;Centered Zoom ;Centered Zoom
LatLon2TileXY(@PBMap\TargetLocation, @PBMap\Drawing\TilePosition, PBMap\Zoom) LatLon2TileXY(@PBMap\GeographicCoordinates, @PBMap\Drawing\TileCoordinates, PBMap\Zoom)
;Convert X, Y in tile.decimal into real pixels ;Convert X, Y in tile.decimal into real pixels
; Px = PBMap\Drawing\TilePosition\x * PBMap\TileSize ; Px = PBMap\Drawing\TileCoordinates\x * PBMap\TileSize
; Py = PBMap\Drawing\TilePosition\y * PBMap\TileSize ; Py = PBMap\Drawing\TileCoordinates\y * PBMap\TileSize
; MouseX = Px + CenterX - x ; MouseX = Px + CenterX - x
; MouseY = Py + CenterY - y ; MouseY = Py + CenterY - y
; ;
@@ -1131,33 +1132,33 @@ Module PBMap
; EndIf ; EndIf
; ; Debug "------" ;TODO bug when zoom near the wrap ; ; Debug "------" ;TODO bug when zoom near the wrap
; ; Debug "Mx : " +StrD(MouseX) ; ; Debug "Mx : " +StrD(MouseX)
; ; Debug PBMap\TilePosition\x ; ; Debug PBMap\TileCoordinates\x
; ;Cross-multiply to get the new center ; ;Cross-multiply to get the new center
; PBMap\PixelPosition\x = (OldPx * MouseX) / OldMx ; PBMap\PixelCoordinates\x = (OldPx * MouseX) / OldMx
; PBMap\PixelPosition\y = (OldPy * MouseY) / OldMy ; PBMap\PixelCoordinates\y = (OldPy * MouseY) / OldMy
; ;PBMap tile position in tile.decimal ; ;PBMap tile position in tile.decimal
; PBMap\Drawing\TilePosition\x = PBMap\PixelPosition\x / PBMap\TileSize ; PBMap\Drawing\TileCoordinates\x = PBMap\PixelCoordinates\x / PBMap\TileSize
; PBMap\Drawing\TilePosition\y = PBMap\PixelPosition\y / PBMap\TileSize ; PBMap\Drawing\TileCoordinates\y = PBMap\PixelCoordinates\y / PBMap\TileSize
; PBMap\Drawing\PassNb = 1 ; PBMap\Drawing\PassNb = 1
;TileXY2LatLon(@PBMap\Drawing\TilePosition, @PBMap\TargetLocation, PBMap\Zoom) ;TileXY2LatLon(@PBMap\Drawing\TileCoordinates, @PBMap\GeographicCoordinates, PBMap\Zoom)
LatLon2Pixel(@PBMap\TargetLocation, @PBMap\PixelPosition, PBMap\Zoom) LatLon2Pixel(@PBMap\GeographicCoordinates, @PBMap\PixelCoordinates, PBMap\Zoom)
;Start drawing ;Start drawing
PBMap\Redraw = #True PBMap\Redraw = #True
;Drawing() ;Drawing()
;If CallBackLocation send Location to function ;If CallBackLocation send Location to function
If PBMap\CallBackLocation > 0 If PBMap\CallBackLocation > 0
CallFunctionFast(PBMap\CallBackLocation, @PBMap\TargetLocation) CallFunctionFast(PBMap\CallBackLocation, @PBMap\GeographicCoordinates)
EndIf EndIf
EndProcedure EndProcedure
Procedure.d GetLatitude() Procedure.d GetLatitude()
; ProcedureReturn 0-(90-Mod((PBMap\TargetLocation\Latitude+90),180)) ; ProcedureReturn 0-(90-Mod((PBMap\GeographicCoordinates\Latitude+90),180))
ProcedureReturn PBMap\TargetLocation\Latitude ProcedureReturn PBMap\GeographicCoordinates\Latitude
EndProcedure EndProcedure
Procedure.d GetLongitude() Procedure.d GetLongitude()
; ProcedureReturn 0-(180-Mod((PBMap\TargetLocation\Longitude+180),360)) ; ProcedureReturn 0-(180-Mod((PBMap\GeographicCoordinates\Longitude+180),360))
ProcedureReturn PBMap\TargetLocation\Longitude ProcedureReturn PBMap\GeographicCoordinates\Longitude
EndProcedure EndProcedure
Procedure.i GetZoom() Procedure.i GetZoom()
@@ -1168,7 +1169,7 @@ Module PBMap
Procedure CanvasEvents() Procedure CanvasEvents()
Protected MouseX.i, MouseY.i Protected MouseX.i, MouseY.i
Protected Marker.Coordinates, *Tile.Tile, MapWidth = Pow(2, PBMap\Zoom) * PBMap\TileSize Protected MarkerCoords.Coordinates, *Tile.Tile, MapWidth = Pow(2, PBMap\Zoom) * PBMap\TileSize
PBMap\Moving = #False PBMap\Moving = #False
Select EventType() Select EventType()
Case #PB_EventType_MouseWheel Case #PB_EventType_MouseWheel
@@ -1177,20 +1178,20 @@ Module PBMap
SetZoomOnPosition(GetGadgetAttribute(PBMap\Gadget, #PB_Canvas_MouseX), GetGadgetAttribute(PBMap\Gadget, #PB_Canvas_MouseY), GetGadgetAttribute(PBMap\Gadget, #PB_Canvas_WheelDelta)) SetZoomOnPosition(GetGadgetAttribute(PBMap\Gadget, #PB_Canvas_MouseX), GetGadgetAttribute(PBMap\Gadget, #PB_Canvas_MouseY), GetGadgetAttribute(PBMap\Gadget, #PB_Canvas_WheelDelta))
Else Else
;Absolute zoom (centered on the center of the map) ;Absolute zoom (centered on the center of the map)
SetZoom(GetGadgetAttribute(PBMap\Gadget,#PB_Canvas_WheelDelta), #PB_Relative) SetZoom(GetGadgetAttribute(PBMap\Gadget, #PB_Canvas_WheelDelta), #PB_Relative)
EndIf EndIf
Case #PB_EventType_LeftButtonDown Case #PB_EventType_LeftButtonDown
;Check if we select a marker ;Check if we select a marker
MouseX = PBMap\PixelPosition\x - GadgetWidth(PBMap\Gadget) / 2 + GetGadgetAttribute(PBMap\Gadget, #PB_Canvas_MouseX) MouseX = PBMap\PixelCoordinates\x - GadgetWidth(PBMap\Gadget) / 2 + GetGadgetAttribute(PBMap\Gadget, #PB_Canvas_MouseX)
MouseY = PBMap\PixelPosition\y - GadgetHeight(PBMap\Gadget) / 2 + GetGadgetAttribute(PBMap\Gadget, #PB_Canvas_MouseY) MouseY = PBMap\PixelCoordinates\y - GadgetHeight(PBMap\Gadget) / 2 + GetGadgetAttribute(PBMap\Gadget, #PB_Canvas_MouseY)
;Clip MouseX to the map range (in X, the map is infinite) ;Clip MouseX to the map range (in X, the map is infinite)
MouseX = Mod(Mod(MouseX, MapWidth) + MapWidth, MapWidth) MouseX = Mod(Mod(MouseX, MapWidth) + MapWidth, MapWidth)
ForEach PBMap\Marker() ForEach PBMap\Marker()
LatLon2TileXY(@PBMap\Marker()\Location, @Marker, PBMap\Zoom) LatLon2TileXY(@PBMap\Marker()\GeographicCoordinates, @MarkerCoords, PBMap\Zoom)
Marker\x * PBMap\TileSize MarkerCoords\x * PBMap\TileSize
Marker\y * PBMap\TileSize MarkerCoords\y * PBMap\TileSize
;Debug "Pos : " + StrD(Marker\x) + " ; Drawing pos : " + StrD(PBMap\Drawing\TilePosition\x) ;Debug "Pos : " + StrD(Marker\x) + " ; Drawing pos : " + StrD(PBMap\Drawing\TileCoordinates\x)
If Distance(Marker\x, Marker\y, MouseX, MouseY) < 8 If Distance(MarkerCoords\x, MarkerCoords\y, MouseX, MouseY) < 8
PBMap\EditMarkerIndex = ListIndex(PBMap\Marker()) PBMap\EditMarkerIndex = ListIndex(PBMap\Marker())
Break Break
EndIf EndIf
@@ -1206,27 +1207,24 @@ Module PBMap
;Move marker ;Move marker
If PBMap\EditMarkerIndex > -1 If PBMap\EditMarkerIndex > -1
SelectElement(PBMap\Marker(), PBMap\EditMarkerIndex) SelectElement(PBMap\Marker(), PBMap\EditMarkerIndex)
LatLon2TileXY(@PBMap\Marker()\Location, @Marker, PBMap\Zoom) LatLon2TileXY(@PBMap\Marker()\GeographicCoordinates, @MarkerCoords, PBMap\Zoom)
Marker\x + MouseX / PBMap\TileSize MarkerCoords\x + MouseX / PBMap\TileSize
Marker\y + MouseY / PBMap\TileSize MarkerCoords\y + MouseY / PBMap\TileSize
TileXY2LatLon(@Marker, @PBMap\Marker()\Location, PBMap\Zoom) TileXY2LatLon(@MarkerCoords, @PBMap\Marker()\GeographicCoordinates, PBMap\Zoom)
Else Else
;New move values ;New move values
;PBMap\Position\x - MouseX ;PBMap\Position\x - MouseX
;Ensures that pixel position stay in the range [0..2^Zoom*PBMap\TileSize[ coz of the wrapping of the map ;Ensures that pixel position stay in the range [0..2^Zoom*PBMap\TileSize[ coz of the wrapping of the map
PBMap\PixelPosition\x - MouseX PBMap\PixelCoordinates\x - MouseX
PBMap\PixelPosition\x = Mod(Mod(PBMap\PixelPosition\x, MapWidth) + MapWidth, MapWidth) PBMap\PixelCoordinates\x = Mod(Mod(PBMap\PixelCoordinates\x, MapWidth) + MapWidth, MapWidth)
PBMap\PixelPosition\y - MouseY PBMap\PixelCoordinates\y - MouseY
;PBMap tile position in tile.decimal Pixel2LatLon(@PBMap\PixelCoordinates, @PBMap\GeographicCoordinates, PBMap\Zoom)
PBMap\Drawing\TilePosition\x = PBMap\PixelPosition\x / PBMap\TileSize
PBMap\Drawing\TilePosition\y = PBMap\PixelPosition\y / PBMap\TileSize
PBMap\Drawing\PassNb = 1
TileXY2LatLon(@PBMap\Drawing, @PBMap\TargetLocation, PBMap\Zoom)
;If CallBackLocation send Location to function ;If CallBackLocation send Location to function
If PBMap\CallBackLocation > 0 If PBMap\CallBackLocation > 0
CallFunctionFast(PBMap\CallBackLocation, @PBMap\TargetLocation) CallFunctionFast(PBMap\CallBackLocation, @PBMap\GeographicCoordinates)
EndIf EndIf
EndIf EndIf
PBMap\Drawing\PassNb = 1
PBMap\Redraw = #True PBMap\Redraw = #True
PBMap\MoveStartingPoint\x = GetGadgetAttribute(PBMap\Gadget, #PB_Canvas_MouseX) PBMap\MoveStartingPoint\x = GetGadgetAttribute(PBMap\Gadget, #PB_Canvas_MouseX)
PBMap\MoveStartingPoint\y = GetGadgetAttribute(PBMap\Gadget, #PB_Canvas_MouseY) PBMap\MoveStartingPoint\y = GetGadgetAttribute(PBMap\Gadget, #PB_Canvas_MouseY)
@@ -1236,10 +1234,10 @@ Module PBMap
If PBMap\EditMarkerIndex > -1 If PBMap\EditMarkerIndex > -1
PBMap\EditMarkerIndex = -1 PBMap\EditMarkerIndex = -1
Else ;Move Map Else ;Move Map
PBMap\Drawing\TilePosition\x = PBMap\PixelPosition\x / PBMap\TileSize ; PBMap\Drawing\TileCoordinates\x = PBMap\PixelCoordinates\x / PBMap\TileSize
PBMap\Drawing\TilePosition\y = PBMap\PixelPosition\y / PBMap\TileSize ; PBMap\Drawing\TileCoordinates\y = PBMap\PixelCoordinates\y / PBMap\TileSize
MyDebug("PBMap\Drawing\TilePosition\x " + Str(PBMap\Drawing\TilePosition\x) + " ; PBMap\Drawing\TilePosition\y " + Str(PBMap\Drawing\TilePosition\y) ) ; MyDebug("PBMap\Drawing\TileCoordinates\x " + Str(PBMap\Drawing\TileCoordinates\x) + " ; PBMap\Drawing\TileCoordinates\y " + Str(PBMap\Drawing\TileCoordinates\y) )
TileXY2LatLon(@PBMap\Drawing, @PBMap\TargetLocation, PBMap\Zoom) ; TileXY2LatLon(@PBMap\Drawing\TileCoordinates, @PBMap\GeographicCoordinates, PBMap\Zoom)
PBMap\Redraw = #True PBMap\Redraw = #True
EndIf EndIf
Case #PB_MAP_REDRAW Case #PB_MAP_REDRAW
@@ -1425,9 +1423,9 @@ CompilerIf #PB_Compiler_IsMainFile
CompilerEndIf CompilerEndIf
; IDE Options = PureBasic 5.50 (Windows - x64) ; IDE Options = PureBasic 5.50 (Windows - x64)
; CursorPosition = 430 ; CursorPosition = 408
; FirstLine = 415 ; FirstLine = 397
; Folding = ---------- ; Folding = -----------
; EnableThread ; EnableThread
; EnableXP ; EnableXP
; EnableUnicode ; EnableUnicode