-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLeafletDemo5.sb
More file actions
203 lines (128 loc) · 5.12 KB
/
Copy pathLeafletDemo5.sb
File metadata and controls
203 lines (128 loc) · 5.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
EnableExplicit
XIncludeFile "Leaflet.sbi"
Enumeration
#Window
#Button
#LeafletGadget
EndEnumeration
Global NewMap Marker()
Global myMap
Global RandomMarkerNumber = 50
Procedure JsonParse(String.s, ReplaceQuotes = #True)
If ReplaceQuotes
String = ReplaceString(String, "'", Chr(34))
EndIf
! return JSON.parse(v_string);
EndProcedure
Procedure GetRandomLatLng(myMap)
! var bounds = v_mymap.getBounds(),
! southWest = bounds.getSouthWest(),
! northEast = bounds.getNorthEast(),
! lngSpan = northEast.lng - southWest.lng,
! latSpan = northEast.lat - southWest.lat;
!
! return L.latLng(
! southWest.lat + latSpan * Math.random(),
! southWest.lng + lngSpan * Math.random());
EndProcedure
Procedure Populate(myMap, myMarkerClusterGroup)
; populate the Map wtih random markers
Protected Counter
Protected myMarker
For Counter = 0 To 100
myMarker = Leaflet::NewMarker(GetRandomLatLng(myMap))
Leaflet::SetStringAttribute(myMarker, Leaflet::#Title, "Random Marker: " + Str(Counter))
Leaflet::SetStringAttribute(myMarker, Leaflet::#Tooltip, "Random Marker: " + Str(Counter))
Leaflet::AddItem(myMarkerClusterGroup, myMarker)
Marker("M" + Str(Counter)) = myMarker
Next
EndProcedure
Procedure SizeWindowEvent()
ResizeGadget(#LeafletGadget, #PB_Ignore, #PB_Ignore, WindowWidth(#Window) - 180, WindowHeight(#Window) - 20)
EndProcedure
Procedure ButtonEvent()
Static ZoomedIn
Protected myMarker
Protected LatLng
If ZoomedIn
RandomMarkerNumber = Random(0, 100)
SetGadgetText(#Button, "Zoom to Marker " + Str(RandomMarkerNumber))
LatLng = Leaflet::LatLng(50.703416, 6.634032)
Leaflet::SetView(myMap, LatLng, 5)
ZoomedIn = #False
Else
SetGadgetText(#Button, "Zoom out")
myMarker = Marker("M"+ Str(RandomMarkerNumber))
LatLng = Leaflet::GetAttribute(myMarker, Leaflet::#LatLng)
Leaflet::SetView(myMap, LatLng, 8)
ZoomedIn = #True
EndIf
EndProcedure
Procedure CreateIcon(IconText.s)
Protected IconSize = 32
Protected I
Protected ReturnValue
I = CreateImage(#PB_Any, IconSize, IconSize)
StartDrawing(ImageOutput(I))
Box(0, 0, IconSize, IconSize, #Black)
Box(1, 1, IconSize - 2, IconSize - 2, #White)
DrawText((IconSize/2)-(TextWidth(IconText)/2), (IconSize/2)-(TextHeight(IconText)/2), IconText)
StopDrawing()
! v_returnvalue = spider_ImageID(v_i).toDataURL();
FreeImage(I)
ProcedureReturn ReturnValue
EndProcedure
Procedure AddDivIconStyles()
Static AlreadyAdded
If AlreadyAdded = #False ; add only one time
AlreadyAdded = #True
! $("<style type='text/css'> .centeredText { position: absolute; top: 50%; left: 50%; transform: translate(-50%, -85%); color: red; background: white; width: 14px} </style>").appendTo("head");
! $("<style type='text/css'> .myDivIcon { width:25px !important; height:41px !important; border: 0; text-align: center; } </style>").appendTo("head");
EndIf
EndProcedure
Procedure IconCreateCallback(Cluster)
Protected ChildCount
ChildCount = Leaflet::ClusterGetChildCount(Cluster)
If #False
AddDivIconStyles()
; Option 1: Using Leaflet::NewDivIcon()
Protected Options
! v_options = {
! html: '<div class="container"><img src="https://unpkg.com/leaflet@1.3.4/dist/images/marker-icon.png"><div class="centeredText">' + v_childcount + '</div></div>',
! className: 'myDivIcon'
! }
Protected myDivIcon = Leaflet::NewDivIcon(Options)
ProcedureReturn myDivIcon
Else
; Option 2: Using Leaflet::NewIcon() with selfdrawn Icons
Protected CreatedIcon = CreateIcon(Str(ChildCount))
Protected myIcon
! v_myicon = L.icon({
! iconUrl: v_createdicon,
! iconSize: [32, 32],
! iconAnchor: [16, 16],
! popupAnchor: [16, 16]
! });
ProcedureReturn myIcon
EndIf
EndProcedure
Procedure Main()
OpenWindow(#Window, #PB_Ignore, #PB_Ignore, 800, 600, "LeafletGadgetDemo", #PB_Window_ScreenCentered | #PB_Window_SizeGadget)
BindEvent(#PB_Event_SizeWindow, @SizeWindowEvent(), #Window)
ButtonGadget(#Button, 10, 10, 150, 30, "Zoom to Marker " + Str(RandomMarkerNumber))
BindGadgetEvent(#Button, @ButtonEvent())
ContainerGadget(#LeafletGadget, 170, 10, WindowWidth(#Window) - 180, WindowHeight(#Window) - 20, #PB_Container_Flat) : CloseGadgetList()
Leaflet::BindGadget(#LeafletGadget, JsonParse("{ 'center': [50.703416, 6.634032], 'zoom': 5 }"))
myMap = Leaflet::GetMap(#LeafletGadget)
; add an OpenStreetMap-Tilelayer to the map
Leaflet::AddItem(myMap, Leaflet::NewTileLayer(Leaflet::#TileLayerType_OpenStreetMap))
; create a MarkerClusterGroup
Protected myMarkerClusterGroup = Leaflet::NewMarkerClusterGroup(@IconCreateCallback())
; set the cluster-radius
Leaflet::SetMaxClusterRadius(myMarkerClusterGroup, 100)
; ... populate it with random markers
Populate(myMap, myMarkerClusterGroup)
; ... and add it to the map:
Leaflet::AddItem(myMap, myMarkerClusterGroup)
EndProcedure
Leaflet::Init(@Main())