@scoova/mgl 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (565) hide show
  1. package/LICENSE.txt +116 -0
  2. package/README.md +174 -0
  3. package/build/banner.ts +12 -0
  4. package/build/bump-version-changelog.js +26 -0
  5. package/build/check-bundle-size.js +77 -0
  6. package/build/generate-dist-package.js +26 -0
  7. package/build/generate-doc-images.ts +78 -0
  8. package/build/generate-docs.ts +190 -0
  9. package/build/generate-shaders.ts +68 -0
  10. package/build/generate-struct-arrays.ts +438 -0
  11. package/build/generate-style-code.ts +283 -0
  12. package/build/readme.md +56 -0
  13. package/build/release-notes.js +45 -0
  14. package/build/rollup/bundle_prelude.js +29 -0
  15. package/build/rollup/maplibregl.js +22 -0
  16. package/build/rollup_plugins.ts +58 -0
  17. package/dist/LICENSE.txt +116 -0
  18. package/dist/maplibre-gl.css +1 -0
  19. package/dist/maplibre-gl.d.ts +13304 -0
  20. package/dist/maplibre-gl.js +59 -0
  21. package/dist/maplibre-gl.js.map +1 -0
  22. package/dist/package.json +1 -0
  23. package/package.json +192 -0
  24. package/src/css/maplibre-gl.css +854 -0
  25. package/src/css/svg/maplibregl-ctrl-attrib.svg +3 -0
  26. package/src/css/svg/maplibregl-ctrl-compass.svg +4 -0
  27. package/src/css/svg/maplibregl-ctrl-fullscreen.svg +3 -0
  28. package/src/css/svg/maplibregl-ctrl-geolocate.svg +5 -0
  29. package/src/css/svg/maplibregl-ctrl-logo.svg +11 -0
  30. package/src/css/svg/maplibregl-ctrl-shrink.svg +3 -0
  31. package/src/css/svg/maplibregl-ctrl-terrain.svg +3 -0
  32. package/src/css/svg/maplibregl-ctrl-zoom-in.svg +3 -0
  33. package/src/css/svg/maplibregl-ctrl-zoom-out.svg +3 -0
  34. package/src/data/array_types.g.ts +1130 -0
  35. package/src/data/bucket/circle_attributes.ts +8 -0
  36. package/src/data/bucket/circle_bucket.ts +197 -0
  37. package/src/data/bucket/fill_attributes.ts +8 -0
  38. package/src/data/bucket/fill_bucket.test.ts +95 -0
  39. package/src/data/bucket/fill_bucket.ts +228 -0
  40. package/src/data/bucket/fill_extrusion_attributes.ts +13 -0
  41. package/src/data/bucket/fill_extrusion_bucket.ts +305 -0
  42. package/src/data/bucket/heatmap_bucket.ts +12 -0
  43. package/src/data/bucket/line_attributes.ts +8 -0
  44. package/src/data/bucket/line_attributes_ext.ts +8 -0
  45. package/src/data/bucket/line_bucket.test.ts +142 -0
  46. package/src/data/bucket/line_bucket.ts +592 -0
  47. package/src/data/bucket/pattern_attributes.ts +9 -0
  48. package/src/data/bucket/pattern_bucket_features.ts +57 -0
  49. package/src/data/bucket/symbol_attributes.ts +122 -0
  50. package/src/data/bucket/symbol_bucket.test.ts +243 -0
  51. package/src/data/bucket/symbol_bucket.ts +975 -0
  52. package/src/data/bucket.ts +123 -0
  53. package/src/data/dem_data.test.ts +237 -0
  54. package/src/data/dem_data.ts +171 -0
  55. package/src/data/evaluation_feature.ts +18 -0
  56. package/src/data/extent.ts +13 -0
  57. package/src/data/feature_index.ts +337 -0
  58. package/src/data/feature_position_map.test.ts +33 -0
  59. package/src/data/feature_position_map.ts +126 -0
  60. package/src/data/index_array_type.ts +9 -0
  61. package/src/data/load_geometry.test.ts +49 -0
  62. package/src/data/load_geometry.ts +44 -0
  63. package/src/data/pos3d_attributes.ts +5 -0
  64. package/src/data/pos_attributes.ts +5 -0
  65. package/src/data/program_configuration.ts +735 -0
  66. package/src/data/raster_bounds_attributes.ts +6 -0
  67. package/src/data/segment.ts +89 -0
  68. package/src/geo/edge_insets.test.ts +83 -0
  69. package/src/geo/edge_insets.ts +146 -0
  70. package/src/geo/lng_lat.test.ts +65 -0
  71. package/src/geo/lng_lat.ts +169 -0
  72. package/src/geo/lng_lat_bounds.test.ts +357 -0
  73. package/src/geo/lng_lat_bounds.ts +356 -0
  74. package/src/geo/mercator_coordinate.test.ts +34 -0
  75. package/src/geo/mercator_coordinate.ts +157 -0
  76. package/src/geo/projection/projection.ts +72 -0
  77. package/src/geo/transform.test.ts +509 -0
  78. package/src/geo/transform.ts +1072 -0
  79. package/src/gl/color_mode.ts +31 -0
  80. package/src/gl/context.ts +321 -0
  81. package/src/gl/cull_face_mode.ts +22 -0
  82. package/src/gl/depth_mode.ts +26 -0
  83. package/src/gl/framebuffer.ts +48 -0
  84. package/src/gl/index_buffer.ts +55 -0
  85. package/src/gl/render_pool.test.ts +69 -0
  86. package/src/gl/render_pool.ts +93 -0
  87. package/src/gl/state.test.ts +125 -0
  88. package/src/gl/stencil_mode.ts +27 -0
  89. package/src/gl/types.ts +59 -0
  90. package/src/gl/value.ts +534 -0
  91. package/src/gl/vertex_buffer.test.ts +57 -0
  92. package/src/gl/vertex_buffer.ts +111 -0
  93. package/src/gl/webgl2.ts +12 -0
  94. package/src/index.test.ts +121 -0
  95. package/src/index.ts +250 -0
  96. package/src/render/draw_background.ts +53 -0
  97. package/src/render/draw_circle.ts +113 -0
  98. package/src/render/draw_collision_debug.ts +168 -0
  99. package/src/render/draw_custom.test.ts +73 -0
  100. package/src/render/draw_custom.ts +45 -0
  101. package/src/render/draw_debug.test.ts +122 -0
  102. package/src/render/draw_debug.ts +148 -0
  103. package/src/render/draw_fill.test.ts +137 -0
  104. package/src/render/draw_fill.ts +128 -0
  105. package/src/render/draw_fill_extrusion.ts +97 -0
  106. package/src/render/draw_heatmap.ts +228 -0
  107. package/src/render/draw_hillshade.ts +119 -0
  108. package/src/render/draw_line.ts +125 -0
  109. package/src/render/draw_raster.ts +133 -0
  110. package/src/render/draw_sky.ts +44 -0
  111. package/src/render/draw_symbol.test.ts +223 -0
  112. package/src/render/draw_symbol.ts +499 -0
  113. package/src/render/draw_terrain.ts +99 -0
  114. package/src/render/glyph_atlas.ts +83 -0
  115. package/src/render/glyph_manager.test.ts +147 -0
  116. package/src/render/glyph_manager.ts +198 -0
  117. package/src/render/image_atlas.ts +159 -0
  118. package/src/render/image_manager.ts +337 -0
  119. package/src/render/line_atlas.test.ts +38 -0
  120. package/src/render/line_atlas.ts +214 -0
  121. package/src/render/mesh.ts +25 -0
  122. package/src/render/painter.test.ts +51 -0
  123. package/src/render/painter.ts +666 -0
  124. package/src/render/program/background_program.ts +100 -0
  125. package/src/render/program/circle_program.ts +61 -0
  126. package/src/render/program/clipping_mask_program.ts +19 -0
  127. package/src/render/program/collision_program.ts +47 -0
  128. package/src/render/program/debug_program.ts +29 -0
  129. package/src/render/program/fill_extrusion_program.ts +120 -0
  130. package/src/render/program/fill_program.ts +121 -0
  131. package/src/render/program/heatmap_program.ts +76 -0
  132. package/src/render/program/hillshade_program.ts +116 -0
  133. package/src/render/program/line_program.ts +207 -0
  134. package/src/render/program/pattern.ts +102 -0
  135. package/src/render/program/program_uniforms.ts +46 -0
  136. package/src/render/program/raster_program.ts +88 -0
  137. package/src/render/program/sky_program.ts +28 -0
  138. package/src/render/program/symbol_program.ts +266 -0
  139. package/src/render/program/terrain_program.ts +116 -0
  140. package/src/render/program.ts +233 -0
  141. package/src/render/render_to_texture.test.ts +151 -0
  142. package/src/render/render_to_texture.ts +197 -0
  143. package/src/render/terrain.test.ts +322 -0
  144. package/src/render/terrain.ts +464 -0
  145. package/src/render/texture.ts +114 -0
  146. package/src/render/uniform_binding.test.ts +122 -0
  147. package/src/render/uniform_binding.ts +157 -0
  148. package/src/render/update_pattern_positions_in_program.test.ts +74 -0
  149. package/src/render/update_pattern_positions_in_program.ts +50 -0
  150. package/src/render/vertex_array_object.ts +163 -0
  151. package/src/shaders/README.md +42 -0
  152. package/src/shaders/_prelude.fragment.glsl +19 -0
  153. package/src/shaders/_prelude.fragment.glsl.g.ts +2 -0
  154. package/src/shaders/_prelude.vertex.glsl +148 -0
  155. package/src/shaders/_prelude.vertex.glsl.g.ts +2 -0
  156. package/src/shaders/background.fragment.glsl +10 -0
  157. package/src/shaders/background.fragment.glsl.g.ts +2 -0
  158. package/src/shaders/background.vertex.glsl +7 -0
  159. package/src/shaders/background.vertex.glsl.g.ts +2 -0
  160. package/src/shaders/background_pattern.fragment.glsl +28 -0
  161. package/src/shaders/background_pattern.fragment.glsl.g.ts +2 -0
  162. package/src/shaders/background_pattern.vertex.glsl +19 -0
  163. package/src/shaders/background_pattern.vertex.glsl.g.ts +2 -0
  164. package/src/shaders/circle.fragment.glsl +34 -0
  165. package/src/shaders/circle.fragment.glsl.g.ts +2 -0
  166. package/src/shaders/circle.vertex.glsl +68 -0
  167. package/src/shaders/circle.vertex.glsl.g.ts +2 -0
  168. package/src/shaders/clipping_mask.fragment.glsl +3 -0
  169. package/src/shaders/clipping_mask.fragment.glsl.g.ts +2 -0
  170. package/src/shaders/clipping_mask.vertex.glsl +7 -0
  171. package/src/shaders/clipping_mask.vertex.glsl.g.ts +2 -0
  172. package/src/shaders/collision_box.fragment.glsl +20 -0
  173. package/src/shaders/collision_box.fragment.glsl.g.ts +2 -0
  174. package/src/shaders/collision_box.vertex.glsl +27 -0
  175. package/src/shaders/collision_box.vertex.glsl.g.ts +2 -0
  176. package/src/shaders/collision_circle.fragment.glsl +17 -0
  177. package/src/shaders/collision_circle.fragment.glsl.g.ts +2 -0
  178. package/src/shaders/collision_circle.vertex.glsl +59 -0
  179. package/src/shaders/collision_circle.vertex.glsl.g.ts +2 -0
  180. package/src/shaders/debug.fragment.glsl +9 -0
  181. package/src/shaders/debug.fragment.glsl.g.ts +2 -0
  182. package/src/shaders/debug.vertex.glsl +12 -0
  183. package/src/shaders/debug.vertex.glsl.g.ts +2 -0
  184. package/src/shaders/encode_attribute.test.ts +11 -0
  185. package/src/shaders/encode_attribute.ts +13 -0
  186. package/src/shaders/fill.fragment.glsl +13 -0
  187. package/src/shaders/fill.fragment.glsl.g.ts +2 -0
  188. package/src/shaders/fill.vertex.glsl +13 -0
  189. package/src/shaders/fill.vertex.glsl.g.ts +2 -0
  190. package/src/shaders/fill_extrusion.fragment.glsl +9 -0
  191. package/src/shaders/fill_extrusion.fragment.glsl.g.ts +2 -0
  192. package/src/shaders/fill_extrusion.vertex.glsl +84 -0
  193. package/src/shaders/fill_extrusion.vertex.glsl.g.ts +2 -0
  194. package/src/shaders/fill_extrusion_pattern.fragment.glsl +47 -0
  195. package/src/shaders/fill_extrusion_pattern.fragment.glsl.g.ts +2 -0
  196. package/src/shaders/fill_extrusion_pattern.vertex.glsl +96 -0
  197. package/src/shaders/fill_extrusion_pattern.vertex.glsl.g.ts +2 -0
  198. package/src/shaders/fill_outline.fragment.glsl +17 -0
  199. package/src/shaders/fill_outline.fragment.glsl.g.ts +2 -0
  200. package/src/shaders/fill_outline.vertex.glsl +17 -0
  201. package/src/shaders/fill_outline.vertex.glsl.g.ts +2 -0
  202. package/src/shaders/fill_outline_pattern.fragment.glsl +43 -0
  203. package/src/shaders/fill_outline_pattern.fragment.glsl.g.ts +2 -0
  204. package/src/shaders/fill_outline_pattern.vertex.glsl +44 -0
  205. package/src/shaders/fill_outline_pattern.vertex.glsl.g.ts +2 -0
  206. package/src/shaders/fill_pattern.fragment.glsl +39 -0
  207. package/src/shaders/fill_pattern.fragment.glsl.g.ts +2 -0
  208. package/src/shaders/fill_pattern.vertex.glsl +39 -0
  209. package/src/shaders/fill_pattern.vertex.glsl.g.ts +2 -0
  210. package/src/shaders/heatmap.fragment.glsl +22 -0
  211. package/src/shaders/heatmap.fragment.glsl.g.ts +2 -0
  212. package/src/shaders/heatmap.vertex.glsl +54 -0
  213. package/src/shaders/heatmap.vertex.glsl.g.ts +2 -0
  214. package/src/shaders/heatmap_texture.fragment.glsl +15 -0
  215. package/src/shaders/heatmap_texture.fragment.glsl.g.ts +2 -0
  216. package/src/shaders/heatmap_texture.vertex.glsl +11 -0
  217. package/src/shaders/heatmap_texture.vertex.glsl.g.ts +2 -0
  218. package/src/shaders/hillshade.fragment.glsl +52 -0
  219. package/src/shaders/hillshade.fragment.glsl.g.ts +2 -0
  220. package/src/shaders/hillshade.vertex.glsl +11 -0
  221. package/src/shaders/hillshade.vertex.glsl.g.ts +2 -0
  222. package/src/shaders/hillshade_prepare.fragment.glsl +77 -0
  223. package/src/shaders/hillshade_prepare.fragment.glsl.g.ts +2 -0
  224. package/src/shaders/hillshade_prepare.vertex.glsl +15 -0
  225. package/src/shaders/hillshade_prepare.vertex.glsl.g.ts +2 -0
  226. package/src/shaders/line.fragment.glsl +30 -0
  227. package/src/shaders/line.fragment.glsl.g.ts +2 -0
  228. package/src/shaders/line.vertex.glsl +89 -0
  229. package/src/shaders/line.vertex.glsl.g.ts +2 -0
  230. package/src/shaders/line_gradient.fragment.glsl +34 -0
  231. package/src/shaders/line_gradient.fragment.glsl.g.ts +2 -0
  232. package/src/shaders/line_gradient.vertex.glsl +92 -0
  233. package/src/shaders/line_gradient.vertex.glsl.g.ts +2 -0
  234. package/src/shaders/line_pattern.fragment.glsl +77 -0
  235. package/src/shaders/line_pattern.fragment.glsl.g.ts +2 -0
  236. package/src/shaders/line_pattern.vertex.glsl +103 -0
  237. package/src/shaders/line_pattern.vertex.glsl.g.ts +2 -0
  238. package/src/shaders/line_sdf.fragment.glsl +45 -0
  239. package/src/shaders/line_sdf.fragment.glsl.g.ts +2 -0
  240. package/src/shaders/line_sdf.vertex.glsl +101 -0
  241. package/src/shaders/line_sdf.vertex.glsl.g.ts +2 -0
  242. package/src/shaders/raster.fragment.glsl +53 -0
  243. package/src/shaders/raster.fragment.glsl.g.ts +2 -0
  244. package/src/shaders/raster.vertex.glsl +21 -0
  245. package/src/shaders/raster.vertex.glsl.g.ts +2 -0
  246. package/src/shaders/shaders.ts +197 -0
  247. package/src/shaders/sky.fragment.glsl +17 -0
  248. package/src/shaders/sky.fragment.glsl.g.ts +2 -0
  249. package/src/shaders/sky.vertex.glsl +5 -0
  250. package/src/shaders/sky.vertex.glsl.g.ts +2 -0
  251. package/src/shaders/symbol_icon.fragment.glsl +17 -0
  252. package/src/shaders/symbol_icon.fragment.glsl.g.ts +2 -0
  253. package/src/shaders/symbol_icon.vertex.glsl +117 -0
  254. package/src/shaders/symbol_icon.vertex.glsl.g.ts +2 -0
  255. package/src/shaders/symbol_sdf.fragment.glsl +58 -0
  256. package/src/shaders/symbol_sdf.fragment.glsl.g.ts +2 -0
  257. package/src/shaders/symbol_sdf.vertex.glsl +142 -0
  258. package/src/shaders/symbol_sdf.vertex.glsl.g.ts +2 -0
  259. package/src/shaders/symbol_text_and_icon.fragment.glsl +68 -0
  260. package/src/shaders/symbol_text_and_icon.fragment.glsl.g.ts +2 -0
  261. package/src/shaders/symbol_text_and_icon.vertex.glsl +140 -0
  262. package/src/shaders/symbol_text_and_icon.vertex.glsl.g.ts +2 -0
  263. package/src/shaders/terrain.fragment.glsl +32 -0
  264. package/src/shaders/terrain.fragment.glsl.g.ts +2 -0
  265. package/src/shaders/terrain.vertex.glsl +17 -0
  266. package/src/shaders/terrain.vertex.glsl.g.ts +2 -0
  267. package/src/shaders/terrain_coords.fragment.glsl +11 -0
  268. package/src/shaders/terrain_coords.fragment.glsl.g.ts +2 -0
  269. package/src/shaders/terrain_coords.vertex.glsl +13 -0
  270. package/src/shaders/terrain_coords.vertex.glsl.g.ts +2 -0
  271. package/src/shaders/terrain_depth.fragment.glsl +15 -0
  272. package/src/shaders/terrain_depth.fragment.glsl.g.ts +2 -0
  273. package/src/shaders/terrain_depth.vertex.glsl +13 -0
  274. package/src/shaders/terrain_depth.vertex.glsl.g.ts +2 -0
  275. package/src/source/canvas_source.test.ts +210 -0
  276. package/src/source/canvas_source.ts +227 -0
  277. package/src/source/geojson_source.test.ts +492 -0
  278. package/src/source/geojson_source.ts +431 -0
  279. package/src/source/geojson_source_diff.test.ts +364 -0
  280. package/src/source/geojson_source_diff.ts +172 -0
  281. package/src/source/geojson_worker_source.test.ts +399 -0
  282. package/src/source/geojson_worker_source.ts +303 -0
  283. package/src/source/geojson_wrapper.test.ts +32 -0
  284. package/src/source/geojson_wrapper.ts +81 -0
  285. package/src/source/image_source.test.ts +235 -0
  286. package/src/source/image_source.ts +338 -0
  287. package/src/source/load_tilejson.ts +47 -0
  288. package/src/source/pixels_to_tile_units.ts +25 -0
  289. package/src/source/protocol_crud.ts +48 -0
  290. package/src/source/query_features.test.ts +31 -0
  291. package/src/source/query_features.ts +252 -0
  292. package/src/source/raster_dem_tile_source.test.ts +158 -0
  293. package/src/source/raster_dem_tile_source.ts +166 -0
  294. package/src/source/raster_dem_tile_worker_source.test.ts +36 -0
  295. package/src/source/raster_dem_tile_worker_source.ts +38 -0
  296. package/src/source/raster_tile_source.test.ts +206 -0
  297. package/src/source/raster_tile_source.ts +227 -0
  298. package/src/source/rtl_text_plugin_main_thread.test.ts +170 -0
  299. package/src/source/rtl_text_plugin_main_thread.ts +89 -0
  300. package/src/source/rtl_text_plugin_status.ts +33 -0
  301. package/src/source/rtl_text_plugin_worker.ts +49 -0
  302. package/src/source/source.test.ts +41 -0
  303. package/src/source/source.ts +186 -0
  304. package/src/source/source_cache.test.ts +2069 -0
  305. package/src/source/source_cache.ts +1102 -0
  306. package/src/source/source_state.ts +157 -0
  307. package/src/source/terrain_source_cache.test.ts +105 -0
  308. package/src/source/terrain_source_cache.ts +204 -0
  309. package/src/source/tile.test.ts +290 -0
  310. package/src/source/tile.ts +478 -0
  311. package/src/source/tile_bounds.ts +34 -0
  312. package/src/source/tile_cache.test.ts +130 -0
  313. package/src/source/tile_cache.ts +208 -0
  314. package/src/source/tile_id.test.ts +112 -0
  315. package/src/source/tile_id.ts +221 -0
  316. package/src/source/vector_tile_source.test.ts +401 -0
  317. package/src/source/vector_tile_source.ts +283 -0
  318. package/src/source/vector_tile_worker_source.test.ts +396 -0
  319. package/src/source/vector_tile_worker_source.ts +199 -0
  320. package/src/source/video_source.test.ts +124 -0
  321. package/src/source/video_source.ts +204 -0
  322. package/src/source/worker.test.ts +233 -0
  323. package/src/source/worker.ts +301 -0
  324. package/src/source/worker_source.ts +117 -0
  325. package/src/source/worker_tile.test.ts +226 -0
  326. package/src/source/worker_tile.ts +208 -0
  327. package/src/style/create_style_layer.ts +39 -0
  328. package/src/style/evaluation_parameters.ts +62 -0
  329. package/src/style/format_section_override.test.ts +62 -0
  330. package/src/style/format_section_override.ts +50 -0
  331. package/src/style/light.test.ts +86 -0
  332. package/src/style/light.ts +135 -0
  333. package/src/style/load_glyph_range.test.ts +40 -0
  334. package/src/style/load_glyph_range.ts +32 -0
  335. package/src/style/load_sprite.test.ts +227 -0
  336. package/src/style/load_sprite.ts +71 -0
  337. package/src/style/parse_glyph_pbf.ts +42 -0
  338. package/src/style/pauseable_placement.ts +138 -0
  339. package/src/style/properties.ts +726 -0
  340. package/src/style/query_utils.test.ts +107 -0
  341. package/src/style/query_utils.ts +70 -0
  342. package/src/style/sky.ts +127 -0
  343. package/src/style/style.test.ts +2654 -0
  344. package/src/style/style.ts +1814 -0
  345. package/src/style/style_glyph.ts +25 -0
  346. package/src/style/style_image.ts +188 -0
  347. package/src/style/style_layer/background_style_layer.ts +17 -0
  348. package/src/style/style_layer/background_style_layer_properties.g.ts +40 -0
  349. package/src/style/style_layer/circle_style_layer.ts +98 -0
  350. package/src/style/style_layer/circle_style_layer_properties.g.ts +76 -0
  351. package/src/style/style_layer/custom_style_layer.ts +233 -0
  352. package/src/style/style_layer/fill_extrusion_style_layer.ts +224 -0
  353. package/src/style/style_layer/fill_extrusion_style_layer_properties.g.ts +55 -0
  354. package/src/style/style_layer/fill_style_layer.test.ts +37 -0
  355. package/src/style/style_layer/fill_style_layer.ts +65 -0
  356. package/src/style/style_layer/fill_style_layer_properties.g.ts +64 -0
  357. package/src/style/style_layer/heatmap_style_layer.ts +74 -0
  358. package/src/style/style_layer/heatmap_style_layer_properties.g.ts +46 -0
  359. package/src/style/style_layer/hillshade_style_layer.ts +21 -0
  360. package/src/style/style_layer/hillshade_style_layer_properties.g.ts +49 -0
  361. package/src/style/style_layer/line_style_layer.test.ts +50 -0
  362. package/src/style/style_layer/line_style_layer.ts +131 -0
  363. package/src/style/style_layer/line_style_layer_properties.g.ts +88 -0
  364. package/src/style/style_layer/overlap_mode.test.ts +57 -0
  365. package/src/style/style_layer/overlap_mode.ts +25 -0
  366. package/src/style/style_layer/raster_style_layer.ts +17 -0
  367. package/src/style/style_layer/raster_style_layer_properties.g.ts +55 -0
  368. package/src/style/style_layer/symbol_style_layer.ts +195 -0
  369. package/src/style/style_layer/symbol_style_layer_properties.g.ts +218 -0
  370. package/src/style/style_layer/typed_style_layer.ts +9 -0
  371. package/src/style/style_layer/variable_text_anchor.test.ts +117 -0
  372. package/src/style/style_layer/variable_text_anchor.ts +163 -0
  373. package/src/style/style_layer.test.ts +372 -0
  374. package/src/style/style_layer.ts +287 -0
  375. package/src/style/style_layer_index.test.ts +99 -0
  376. package/src/style/style_layer_index.ts +78 -0
  377. package/src/style/validate_style.ts +53 -0
  378. package/src/style/zoom_history.ts +40 -0
  379. package/src/symbol/anchor.test.ts +14 -0
  380. package/src/symbol/anchor.ts +22 -0
  381. package/src/symbol/check_max_angle.test.ts +54 -0
  382. package/src/symbol/check_max_angle.ts +76 -0
  383. package/src/symbol/clip_line.test.ts +154 -0
  384. package/src/symbol/clip_line.ts +66 -0
  385. package/src/symbol/collision_feature.test.ts +98 -0
  386. package/src/symbol/collision_feature.ts +114 -0
  387. package/src/symbol/collision_index.test.ts +19 -0
  388. package/src/symbol/collision_index.ts +618 -0
  389. package/src/symbol/cross_tile_symbol_index.test.ts +260 -0
  390. package/src/symbol/cross_tile_symbol_index.ts +367 -0
  391. package/src/symbol/get_anchors.test.ts +113 -0
  392. package/src/symbol/get_anchors.ts +167 -0
  393. package/src/symbol/grid_index.test.ts +75 -0
  394. package/src/symbol/grid_index.ts +414 -0
  395. package/src/symbol/merge_lines.test.ts +30 -0
  396. package/src/symbol/merge_lines.ts +80 -0
  397. package/src/symbol/one_em.ts +3 -0
  398. package/src/symbol/opacity_state.ts +23 -0
  399. package/src/symbol/path_interpolator.test.ts +134 -0
  400. package/src/symbol/path_interpolator.ts +55 -0
  401. package/src/symbol/placement.ts +1371 -0
  402. package/src/symbol/projection.test.ts +171 -0
  403. package/src/symbol/projection.ts +833 -0
  404. package/src/symbol/quads.test.ts +157 -0
  405. package/src/symbol/quads.ts +349 -0
  406. package/src/symbol/shaping.test.ts +360 -0
  407. package/src/symbol/shaping.ts +911 -0
  408. package/src/symbol/symbol_layout.ts +739 -0
  409. package/src/symbol/symbol_size.ts +129 -0
  410. package/src/symbol/symbol_style_layer.test.ts +103 -0
  411. package/src/symbol/transform_text.ts +27 -0
  412. package/src/ui/anchor.ts +27 -0
  413. package/src/ui/camera.test.ts +2301 -0
  414. package/src/ui/camera.ts +1520 -0
  415. package/src/ui/control/attribution_control.test.ts +543 -0
  416. package/src/ui/control/attribution_control.ts +209 -0
  417. package/src/ui/control/control.ts +67 -0
  418. package/src/ui/control/fullscreen_control.test.ts +114 -0
  419. package/src/ui/control/fullscreen_control.ts +189 -0
  420. package/src/ui/control/geolocate_control.test.ts +619 -0
  421. package/src/ui/control/geolocate_control.ts +725 -0
  422. package/src/ui/control/logo_control.test.ts +88 -0
  423. package/src/ui/control/logo_control.ts +86 -0
  424. package/src/ui/control/navigation_control.test.ts +238 -0
  425. package/src/ui/control/navigation_control.ts +294 -0
  426. package/src/ui/control/scale_control.test.ts +52 -0
  427. package/src/ui/control/scale_control.ts +152 -0
  428. package/src/ui/control/terrain_control.test.ts +58 -0
  429. package/src/ui/control/terrain_control.ts +74 -0
  430. package/src/ui/default_locale.ts +25 -0
  431. package/src/ui/events.ts +758 -0
  432. package/src/ui/handler/box_zoom.test.ts +163 -0
  433. package/src/ui/handler/box_zoom.ts +171 -0
  434. package/src/ui/handler/click_zoom.ts +55 -0
  435. package/src/ui/handler/cooperative_gestures.test.ts +338 -0
  436. package/src/ui/handler/cooperative_gestures.ts +111 -0
  437. package/src/ui/handler/dblclick_zoom.test.ts +151 -0
  438. package/src/ui/handler/drag_handler.ts +174 -0
  439. package/src/ui/handler/drag_move_state_manager.ts +115 -0
  440. package/src/ui/handler/drag_pan.test.ts +487 -0
  441. package/src/ui/handler/drag_rotate.test.ts +859 -0
  442. package/src/ui/handler/handler_util.ts +10 -0
  443. package/src/ui/handler/keyboard.test.ts +235 -0
  444. package/src/ui/handler/keyboard.ts +212 -0
  445. package/src/ui/handler/map_event.test.ts +158 -0
  446. package/src/ui/handler/map_event.ts +161 -0
  447. package/src/ui/handler/mouse.ts +92 -0
  448. package/src/ui/handler/mouse_handler_interface.test.ts +111 -0
  449. package/src/ui/handler/mouse_rotate.test.ts +62 -0
  450. package/src/ui/handler/one_finger_touch_drag.ts +45 -0
  451. package/src/ui/handler/one_finger_touch_drag_handler_interface.test.ts +77 -0
  452. package/src/ui/handler/scroll_zoom.test.ts +382 -0
  453. package/src/ui/handler/scroll_zoom.ts +379 -0
  454. package/src/ui/handler/shim/dblclick_zoom.ts +64 -0
  455. package/src/ui/handler/shim/drag_pan.ts +104 -0
  456. package/src/ui/handler/shim/drag_rotate.ts +76 -0
  457. package/src/ui/handler/shim/two_fingers_touch.ts +112 -0
  458. package/src/ui/handler/tap_drag_zoom.test.ts +113 -0
  459. package/src/ui/handler/tap_drag_zoom.ts +112 -0
  460. package/src/ui/handler/tap_recognizer.ts +138 -0
  461. package/src/ui/handler/tap_zoom.ts +98 -0
  462. package/src/ui/handler/touch_pan.ts +115 -0
  463. package/src/ui/handler/transform-provider.ts +44 -0
  464. package/src/ui/handler/two_fingers_touch.test.ts +283 -0
  465. package/src/ui/handler/two_fingers_touch.ts +336 -0
  466. package/src/ui/handler_inertia.ts +157 -0
  467. package/src/ui/handler_manager.ts +637 -0
  468. package/src/ui/hash.test.ts +404 -0
  469. package/src/ui/hash.ts +153 -0
  470. package/src/ui/map.ts +3393 -0
  471. package/src/ui/map_tests/map_animation.test.ts +61 -0
  472. package/src/ui/map_tests/map_basic.test.ts +223 -0
  473. package/src/ui/map_tests/map_bounds.test.ts +123 -0
  474. package/src/ui/map_tests/map_calculate_camera_options.test.ts +113 -0
  475. package/src/ui/map_tests/map_canvas.test.ts +59 -0
  476. package/src/ui/map_tests/map_control.test.ts +61 -0
  477. package/src/ui/map_tests/map_disable_handlers.test.ts +38 -0
  478. package/src/ui/map_tests/map_events.test.ts +1001 -0
  479. package/src/ui/map_tests/map_feature_state.test.ts +421 -0
  480. package/src/ui/map_tests/map_images.test.ts +175 -0
  481. package/src/ui/map_tests/map_is_moving.test.ts +164 -0
  482. package/src/ui/map_tests/map_is_rotating.test.ts +62 -0
  483. package/src/ui/map_tests/map_is_zooming.test.ts +86 -0
  484. package/src/ui/map_tests/map_layer.test.ts +457 -0
  485. package/src/ui/map_tests/map_options.test.ts +64 -0
  486. package/src/ui/map_tests/map_pitch.test.ts +90 -0
  487. package/src/ui/map_tests/map_pixel_ratio.test.ts +75 -0
  488. package/src/ui/map_tests/map_query_rendered_features.test.ts +93 -0
  489. package/src/ui/map_tests/map_render.test.ts +90 -0
  490. package/src/ui/map_tests/map_request_render_frame.test.ts +51 -0
  491. package/src/ui/map_tests/map_resize.test.ts +120 -0
  492. package/src/ui/map_tests/map_style.test.ts +541 -0
  493. package/src/ui/map_tests/map_terrian.test.ts +104 -0
  494. package/src/ui/map_tests/map_webgl.test.ts +72 -0
  495. package/src/ui/map_tests/map_world_copies.test.ts +103 -0
  496. package/src/ui/map_tests/map_zoom.test.ts +95 -0
  497. package/src/ui/marker.test.ts +1149 -0
  498. package/src/ui/marker.ts +880 -0
  499. package/src/ui/popup.test.ts +827 -0
  500. package/src/ui/popup.ts +717 -0
  501. package/src/util/abort_error.ts +21 -0
  502. package/src/util/actor.test.ts +218 -0
  503. package/src/util/actor.ts +241 -0
  504. package/src/util/actor_messages.ts +149 -0
  505. package/src/util/ajax.test.ts +237 -0
  506. package/src/util/ajax.ts +297 -0
  507. package/src/util/browser.test.ts +23 -0
  508. package/src/util/browser.ts +63 -0
  509. package/src/util/color_ramp.test.ts +105 -0
  510. package/src/util/color_ramp.ts +56 -0
  511. package/src/util/config.ts +29 -0
  512. package/src/util/dictionary_coder.ts +23 -0
  513. package/src/util/dispatcher.test.ts +76 -0
  514. package/src/util/dispatcher.ts +78 -0
  515. package/src/util/dom.ts +135 -0
  516. package/src/util/evented.test.ts +231 -0
  517. package/src/util/evented.ts +178 -0
  518. package/src/util/find_pole_of_inaccessibility.test.ts +21 -0
  519. package/src/util/find_pole_of_inaccessibility.ts +130 -0
  520. package/src/util/geolocation_support.test.ts +43 -0
  521. package/src/util/geolocation_support.ts +23 -0
  522. package/src/util/global_worker_pool.ts +65 -0
  523. package/src/util/image.ts +150 -0
  524. package/src/util/image_request.test.ts +408 -0
  525. package/src/util/image_request.ts +246 -0
  526. package/src/util/intersection_tests.ts +206 -0
  527. package/src/util/is_char_in_unicode_block.test.ts +15 -0
  528. package/src/util/is_char_in_unicode_block.ts +345 -0
  529. package/src/util/offscreen_canvas_distorted.test.ts +13 -0
  530. package/src/util/offscreen_canvas_distorted.ts +39 -0
  531. package/src/util/offscreen_canvas_supported.ts +11 -0
  532. package/src/util/performance.ts +117 -0
  533. package/src/util/primitives.test.ts +140 -0
  534. package/src/util/primitives.ts +143 -0
  535. package/src/util/request_manager.ts +42 -0
  536. package/src/util/resolve_tokens.test.ts +45 -0
  537. package/src/util/resolve_tokens.ts +17 -0
  538. package/src/util/script_detection.test.ts +138 -0
  539. package/src/util/script_detection.ts +376 -0
  540. package/src/util/smart_wrap.test.ts +97 -0
  541. package/src/util/smart_wrap.ts +58 -0
  542. package/src/util/struct_array.test.ts +101 -0
  543. package/src/util/struct_array.ts +246 -0
  544. package/src/util/style.test.ts +34 -0
  545. package/src/util/style.ts +28 -0
  546. package/src/util/task_queue.test.ts +114 -0
  547. package/src/util/task_queue.ts +64 -0
  548. package/src/util/test/util.ts +185 -0
  549. package/src/util/throttle.test.ts +42 -0
  550. package/src/util/throttle.ts +28 -0
  551. package/src/util/throttled_invoker.ts +41 -0
  552. package/src/util/transferable_grid_index.test.ts +56 -0
  553. package/src/util/transferable_grid_index.ts +214 -0
  554. package/src/util/util.test.ts +414 -0
  555. package/src/util/util.ts +724 -0
  556. package/src/util/vectortile_to_geojson.ts +72 -0
  557. package/src/util/verticalize_punctuation.ts +110 -0
  558. package/src/util/web_worker.ts +16 -0
  559. package/src/util/web_worker_transfer.test.ts +153 -0
  560. package/src/util/web_worker_transfer.ts +254 -0
  561. package/src/util/webp_supported.ts +63 -0
  562. package/src/util/worker_pool.test.ts +43 -0
  563. package/src/util/worker_pool.ts +58 -0
  564. package/src/util/world_bounds.test.ts +59 -0
  565. package/src/util/world_bounds.ts +46 -0
@@ -0,0 +1,725 @@
1
+ import {Event, Evented} from '../../util/evented';
2
+ import {DOM} from '../../util/dom';
3
+ import {extend, warnOnce} from '../../util/util';
4
+ import {checkGeolocationSupport} from '../../util/geolocation_support';
5
+ import {LngLat} from '../../geo/lng_lat';
6
+ import {Marker} from '../marker';
7
+
8
+ import type {Map} from '../map';
9
+ import type {FitBoundsOptions} from '../camera';
10
+ import type {IControl} from './control';
11
+ import {LngLatBounds} from '../../geo/lng_lat_bounds';
12
+
13
+ /**
14
+ * The {@link GeolocateControl} options object
15
+ */
16
+ type GeolocateControlOptions = {
17
+ /**
18
+ * A Geolocation API [PositionOptions](https://developer.mozilla.org/en-US/docs/Web/API/PositionOptions) object.
19
+ * @defaultValue `{enableHighAccuracy: false, timeout: 6000}`
20
+ */
21
+ positionOptions?: PositionOptions;
22
+ /**
23
+ * A options object to use when the map is panned and zoomed to the user's location. The default is to use a `maxZoom` of 15 to limit how far the map will zoom in for very accurate locations.
24
+ */
25
+ fitBoundsOptions?: FitBoundsOptions;
26
+ /**
27
+ * If `true` the `GeolocateControl` becomes a toggle button and when active the map will receive updates to the user's location as it changes.
28
+ * @defaultValue false
29
+ */
30
+ trackUserLocation?: boolean;
31
+ /**
32
+ * By default, if `showUserLocation` is `true`, a transparent circle will be drawn around the user location indicating the accuracy (95% confidence level) of the user's location. Set to `false` to disable. Always disabled when `showUserLocation` is `false`.
33
+ * @defaultValue true
34
+ */
35
+ showAccuracyCircle?: boolean;
36
+ /**
37
+ * By default a dot will be shown on the map at the user's location. Set to `false` to disable.
38
+ * @defaultValue true
39
+ */
40
+ showUserLocation?: boolean;
41
+ };
42
+
43
+ const defaultOptions: GeolocateControlOptions = {
44
+ positionOptions: {
45
+ enableHighAccuracy: false,
46
+ maximumAge: 0,
47
+ timeout: 6000 /* 6 sec */
48
+ },
49
+ fitBoundsOptions: {
50
+ maxZoom: 15
51
+ },
52
+ trackUserLocation: false,
53
+ showAccuracyCircle: true,
54
+ showUserLocation: true
55
+ };
56
+
57
+ let numberOfWatches = 0;
58
+ let noTimeout = false;
59
+
60
+ /**
61
+ * A `GeolocateControl` control provides a button that uses the browser's geolocation
62
+ * API to locate the user on the map.
63
+ *
64
+ * Not all browsers support geolocation,
65
+ * and some users may disable the feature. Geolocation support for modern
66
+ * browsers including Chrome requires sites to be served over HTTPS. If
67
+ * geolocation support is not available, the `GeolocateControl` will show
68
+ * as disabled.
69
+ *
70
+ * The zoom level applied will depend on the accuracy of the geolocation provided by the device.
71
+ *
72
+ * The `GeolocateControl` has two modes. If `trackUserLocation` is `false` (default) the control acts as a button, which when pressed will set the map's camera to target the user location. If the user moves, the map won't update. This is most suited for the desktop. If `trackUserLocation` is `true` the control acts as a toggle button that when active the user's location is actively monitored for changes. In this mode the `GeolocateControl` has three interaction states:
73
+ * * active - the map's camera automatically updates as the user's location changes, keeping the location dot in the center. Initial state and upon clicking the `GeolocateControl` button.
74
+ * * passive - the user's location dot automatically updates, but the map's camera does not. Occurs upon the user initiating a map movement.
75
+ * * disabled - occurs if Geolocation is not available, disabled or denied.
76
+ *
77
+ * These interaction states can't be controlled programmatically, rather they are set based on user interactions.
78
+ *
79
+ * ## State Diagram
80
+ * ![GeolocateControl state diagram](https://github.com/maplibre/maplibre-gl-js/assets/3269297/78e720e5-d781-4da8-9803-a7a0e6aaaa9f)
81
+ *
82
+ * @group Markers and Controls
83
+ *
84
+ * @example
85
+ * ```ts
86
+ * map.addControl(new GeolocateControl({
87
+ * positionOptions: {
88
+ * enableHighAccuracy: true
89
+ * },
90
+ * trackUserLocation: true
91
+ * }));
92
+ * ```
93
+ * @see [Locate the user](https://maplibre.org/maplibre-gl-js/docs/examples/locate-user/)
94
+ *
95
+ * ## Events
96
+ *
97
+ * **Event** `trackuserlocationend` of type {@link Event} will be fired when the `GeolocateControl` changes to the background state, which happens when a user changes the camera during an active position lock. This only applies when `trackUserLocation` is `true`. In the background state, the dot on the map will update with location updates but the camera will not.
98
+ *
99
+ * **Event** `trackuserlocationstart` of type {@link Event} will be fired when the `GeolocateControl` changes to the active lock state, which happens either upon first obtaining a successful Geolocation API position for the user (a `geolocate` event will follow), or the user clicks the geolocate button when in the background state which uses the last known position to recenter the map and enter active lock state (no `geolocate` event will follow unless the users's location changes).
100
+ *
101
+ * **Event** `userlocationlostfocus` of type {@link Event} will be fired when the `GeolocateControl` changes to the background state, which happens when a user changes the camera during an active position lock. This only applies when `trackUserLocation` is `true`. In the background state, the dot on the map will update with location updates but the camera will not.
102
+ *
103
+ * **Event** `userlocationfocus` of type {@link Event} will be fired when the `GeolocateControl` changes to the active lock state, which happens upon the user clicks the geolocate button when in the background state which uses the last known position to recenter the map and enter active lock state.
104
+ *
105
+ * **Event** `geolocate` of type {@link Event} will be fired on each Geolocation API position update which returned as success.
106
+ * `data` - The returned [Position](https://developer.mozilla.org/en-US/docs/Web/API/Position) object from the callback in [Geolocation.getCurrentPosition()](https://developer.mozilla.org/en-US/docs/Web/API/Geolocation/getCurrentPosition) or [Geolocation.watchPosition()](https://developer.mozilla.org/en-US/docs/Web/API/Geolocation/watchPosition).
107
+ *
108
+ * **Event** `error` of type {@link Event} will be fired on each Geolocation API position update which returned as an error.
109
+ * `data` - The returned [PositionError](https://developer.mozilla.org/en-US/docs/Web/API/PositionError) object from the callback in [Geolocation.getCurrentPosition()](https://developer.mozilla.org/en-US/docs/Web/API/Geolocation/getCurrentPosition) or [Geolocation.watchPosition()](https://developer.mozilla.org/en-US/docs/Web/API/Geolocation/watchPosition).
110
+ *
111
+ * **Event** `outofmaxbounds` of type {@link Event} will be fired on each Geolocation API position update which returned as success but user position is out of map `maxBounds`.
112
+ * `data` - The returned [Position](https://developer.mozilla.org/en-US/docs/Web/API/Position) object from the callback in [Geolocation.getCurrentPosition()](https://developer.mozilla.org/en-US/docs/Web/API/Geolocation/getCurrentPosition) or [Geolocation.watchPosition()](https://developer.mozilla.org/en-US/docs/Web/API/Geolocation/watchPosition).
113
+ *
114
+ * @example
115
+ * ```ts
116
+ * // Initialize the geolocate control.
117
+ * let geolocate = new GeolocateControl({
118
+ * positionOptions: {
119
+ * enableHighAccuracy: true
120
+ * },
121
+ * trackUserLocation: true
122
+ * });
123
+ * // Add the control to the map.
124
+ * map.addControl(geolocate);
125
+ * // Set an event listener that fires
126
+ * // when a trackuserlocationend event occurs.
127
+ * geolocate.on('trackuserlocationend', () => {
128
+ * console.log('A trackuserlocationend event has occurred.')
129
+ * });
130
+ * ```
131
+ *
132
+ * @example
133
+ * ```ts
134
+ * // Initialize the geolocate control.
135
+ * let geolocate = new GeolocateControl({
136
+ * positionOptions: {
137
+ * enableHighAccuracy: true
138
+ * },
139
+ * trackUserLocation: true
140
+ * });
141
+ * // Add the control to the map.
142
+ * map.addControl(geolocate);
143
+ * // Set an event listener that fires
144
+ * // when a trackuserlocationstart event occurs.
145
+ * geolocate.on('trackuserlocationstart', () => {
146
+ * console.log('A trackuserlocationstart event has occurred.')
147
+ * });
148
+ * ```
149
+ *
150
+ * @example
151
+ * ```ts
152
+ * // Initialize the geolocate control.
153
+ * let geolocate = new GeolocateControl({
154
+ * positionOptions: {
155
+ * enableHighAccuracy: true
156
+ * },
157
+ * trackUserLocation: true
158
+ * });
159
+ * // Add the control to the map.
160
+ * map.addControl(geolocate);
161
+ * // Set an event listener that fires
162
+ * // when an userlocationlostfocus event occurs.
163
+ * geolocate.on('userlocationlostfocus', function() {
164
+ * console.log('An userlocationlostfocus event has occurred.')
165
+ * });
166
+ * ```
167
+ *
168
+ * @example
169
+ * ```ts
170
+ * // Initialize the geolocate control.
171
+ * let geolocate = new GeolocateControl({
172
+ * positionOptions: {
173
+ * enableHighAccuracy: true
174
+ * },
175
+ * trackUserLocation: true
176
+ * });
177
+ * // Add the control to the map.
178
+ * map.addControl(geolocate);
179
+ * // Set an event listener that fires
180
+ * // when an userlocationfocus event occurs.
181
+ * geolocate.on('userlocationfocus', function() {
182
+ * console.log('An userlocationfocus event has occurred.')
183
+ * });
184
+ * ```
185
+ *
186
+ * @example
187
+ * ```ts
188
+ * // Initialize the geolocate control.
189
+ * let geolocate = new GeolocateControl({
190
+ * positionOptions: {
191
+ * enableHighAccuracy: true
192
+ * },
193
+ * trackUserLocation: true
194
+ * });
195
+ * // Add the control to the map.
196
+ * map.addControl(geolocate);
197
+ * // Set an event listener that fires
198
+ * // when a geolocate event occurs.
199
+ * geolocate.on('geolocate', () => {
200
+ * console.log('A geolocate event has occurred.')
201
+ * });
202
+ * ```
203
+ *
204
+ * @example
205
+ * ```ts
206
+ * // Initialize the geolocate control.
207
+ * let geolocate = new GeolocateControl({
208
+ * positionOptions: {
209
+ * enableHighAccuracy: true
210
+ * },
211
+ * trackUserLocation: true
212
+ * });
213
+ * // Add the control to the map.
214
+ * map.addControl(geolocate);
215
+ * // Set an event listener that fires
216
+ * // when an error event occurs.
217
+ * geolocate.on('error', () => {
218
+ * console.log('An error event has occurred.')
219
+ * });
220
+ * ```
221
+ *
222
+ * @example
223
+ * ```ts
224
+ * // Initialize the geolocate control.
225
+ * let geolocate = new GeolocateControl({
226
+ * positionOptions: {
227
+ * enableHighAccuracy: true
228
+ * },
229
+ * trackUserLocation: true
230
+ * });
231
+ * // Add the control to the map.
232
+ * map.addControl(geolocate);
233
+ * // Set an event listener that fires
234
+ * // when an outofmaxbounds event occurs.
235
+ * geolocate.on('outofmaxbounds', () => {
236
+ * console.log('An outofmaxbounds event has occurred.')
237
+ * });
238
+ * ```
239
+ */
240
+ export class GeolocateControl extends Evented implements IControl {
241
+ _map: Map;
242
+ options: GeolocateControlOptions;
243
+ _container: HTMLElement;
244
+ _dotElement: HTMLElement;
245
+ _circleElement: HTMLElement;
246
+ _geolocateButton: HTMLButtonElement;
247
+ _geolocationWatchID: number;
248
+ _timeoutId: ReturnType<typeof setTimeout>;
249
+ /* Geolocate Control Watch States
250
+ * This is the private state of the control.
251
+ *
252
+ * OFF
253
+ * off/inactive
254
+ * WAITING_ACTIVE
255
+ * Geolocate Control was clicked but still waiting for Geolocation API response with user location
256
+ * ACTIVE_LOCK
257
+ * Showing the user location as a dot AND tracking the camera to be fixed to their location. If their location changes the map moves to follow.
258
+ * ACTIVE_ERROR
259
+ * There was en error from the Geolocation API while trying to show and track the user location.
260
+ * BACKGROUND
261
+ * Showing the user location as a dot but the camera doesn't follow their location as it changes.
262
+ * BACKGROUND_ERROR
263
+ * There was an error from the Geolocation API while trying to show (but not track) the user location.
264
+ */
265
+ _watchState: 'OFF' | 'ACTIVE_LOCK' | 'WAITING_ACTIVE' | 'ACTIVE_ERROR' | 'BACKGROUND' | 'BACKGROUND_ERROR';
266
+ _lastKnownPosition: any;
267
+ _userLocationDotMarker: Marker;
268
+ _accuracyCircleMarker: Marker;
269
+ _accuracy: number;
270
+ _setup: boolean; // set to true once the control has been setup
271
+
272
+ /**
273
+ * @param options - the control's options
274
+ */
275
+ constructor(options: GeolocateControlOptions) {
276
+ super();
277
+ this.options = extend({}, defaultOptions, options);
278
+ }
279
+
280
+ /** {@inheritDoc IControl.onAdd} */
281
+ onAdd(map: Map) {
282
+ this._map = map;
283
+ this._container = DOM.create('div', 'maplibregl-ctrl maplibregl-ctrl-group');
284
+ this._setupUI();
285
+ checkGeolocationSupport().then((supported) => this._finishSetupUI(supported));
286
+ return this._container;
287
+ }
288
+
289
+ /** {@inheritDoc IControl.onRemove} */
290
+ onRemove() {
291
+ // clear the geolocation watch if exists
292
+ if (this._geolocationWatchID !== undefined) {
293
+ window.navigator.geolocation.clearWatch(this._geolocationWatchID);
294
+ this._geolocationWatchID = undefined;
295
+ }
296
+
297
+ // clear the markers from the map
298
+ if (this.options.showUserLocation && this._userLocationDotMarker) {
299
+ this._userLocationDotMarker.remove();
300
+ }
301
+ if (this.options.showAccuracyCircle && this._accuracyCircleMarker) {
302
+ this._accuracyCircleMarker.remove();
303
+ }
304
+
305
+ DOM.remove(this._container);
306
+ this._map.off('zoom', this._onZoom);
307
+ this._map = undefined;
308
+ numberOfWatches = 0;
309
+ noTimeout = false;
310
+ }
311
+
312
+ /**
313
+ * Check if the Geolocation API Position is outside the map's `maxBounds`.
314
+ *
315
+ * @param position - the Geolocation API Position
316
+ * @returns `true` if position is outside the map's `maxBounds`, otherwise returns `false`.
317
+ */
318
+ _isOutOfMapMaxBounds(position: GeolocationPosition) {
319
+ const bounds = this._map.getMaxBounds();
320
+ const coordinates = position.coords;
321
+
322
+ return bounds && (
323
+ coordinates.longitude < bounds.getWest() ||
324
+ coordinates.longitude > bounds.getEast() ||
325
+ coordinates.latitude < bounds.getSouth() ||
326
+ coordinates.latitude > bounds.getNorth()
327
+ );
328
+ }
329
+
330
+ _setErrorState() {
331
+ switch (this._watchState) {
332
+ case 'WAITING_ACTIVE':
333
+ this._watchState = 'ACTIVE_ERROR';
334
+ this._geolocateButton.classList.remove('maplibregl-ctrl-geolocate-active');
335
+ this._geolocateButton.classList.add('maplibregl-ctrl-geolocate-active-error');
336
+ break;
337
+ case 'ACTIVE_LOCK':
338
+ this._watchState = 'ACTIVE_ERROR';
339
+ this._geolocateButton.classList.remove('maplibregl-ctrl-geolocate-active');
340
+ this._geolocateButton.classList.add('maplibregl-ctrl-geolocate-active-error');
341
+ this._geolocateButton.classList.add('maplibregl-ctrl-geolocate-waiting');
342
+ // turn marker grey
343
+ break;
344
+ case 'BACKGROUND':
345
+ this._watchState = 'BACKGROUND_ERROR';
346
+ this._geolocateButton.classList.remove('maplibregl-ctrl-geolocate-background');
347
+ this._geolocateButton.classList.add('maplibregl-ctrl-geolocate-background-error');
348
+ this._geolocateButton.classList.add('maplibregl-ctrl-geolocate-waiting');
349
+ // turn marker grey
350
+ break;
351
+ case 'ACTIVE_ERROR':
352
+ break;
353
+ default:
354
+ throw new Error(`Unexpected watchState ${this._watchState}`);
355
+ }
356
+ }
357
+
358
+ /**
359
+ * When the Geolocation API returns a new location, update the `GeolocateControl`.
360
+ *
361
+ * @param position - the Geolocation API Position
362
+ */
363
+ _onSuccess = (position: GeolocationPosition) => {
364
+ if (!this._map) {
365
+ // control has since been removed
366
+ return;
367
+ }
368
+
369
+ if (this._isOutOfMapMaxBounds(position)) {
370
+ this._setErrorState();
371
+
372
+ this.fire(new Event('outofmaxbounds', position));
373
+ this._updateMarker();
374
+ this._finish();
375
+
376
+ return;
377
+ }
378
+
379
+ if (this.options.trackUserLocation) {
380
+ // keep a record of the position so that if the state is BACKGROUND and the user
381
+ // clicks the button, we can move to ACTIVE_LOCK immediately without waiting for
382
+ // watchPosition to trigger _onSuccess
383
+ this._lastKnownPosition = position;
384
+
385
+ switch (this._watchState) {
386
+ case 'WAITING_ACTIVE':
387
+ case 'ACTIVE_LOCK':
388
+ case 'ACTIVE_ERROR':
389
+ this._watchState = 'ACTIVE_LOCK';
390
+ this._geolocateButton.classList.remove('maplibregl-ctrl-geolocate-waiting');
391
+ this._geolocateButton.classList.remove('maplibregl-ctrl-geolocate-active-error');
392
+ this._geolocateButton.classList.add('maplibregl-ctrl-geolocate-active');
393
+ break;
394
+ case 'BACKGROUND':
395
+ case 'BACKGROUND_ERROR':
396
+ this._watchState = 'BACKGROUND';
397
+ this._geolocateButton.classList.remove('maplibregl-ctrl-geolocate-waiting');
398
+ this._geolocateButton.classList.remove('maplibregl-ctrl-geolocate-background-error');
399
+ this._geolocateButton.classList.add('maplibregl-ctrl-geolocate-background');
400
+ break;
401
+ default:
402
+ throw new Error(`Unexpected watchState ${this._watchState}`);
403
+ }
404
+ }
405
+
406
+ // if showUserLocation and the watch state isn't off then update the marker location
407
+ if (this.options.showUserLocation && this._watchState !== 'OFF') {
408
+ this._updateMarker(position);
409
+ }
410
+
411
+ // if in normal mode (not watch mode), or if in watch mode and the state is active watch
412
+ // then update the camera
413
+ if (!this.options.trackUserLocation || this._watchState === 'ACTIVE_LOCK') {
414
+ this._updateCamera(position);
415
+ }
416
+
417
+ if (this.options.showUserLocation) {
418
+ this._dotElement.classList.remove('maplibregl-user-location-dot-stale');
419
+ }
420
+
421
+ this.fire(new Event('geolocate', position));
422
+ this._finish();
423
+ };
424
+
425
+ /**
426
+ * Update the camera location to center on the current position
427
+ *
428
+ * @param position - the Geolocation API Position
429
+ */
430
+ _updateCamera = (position: GeolocationPosition) => {
431
+ const center = new LngLat(position.coords.longitude, position.coords.latitude);
432
+ const radius = position.coords.accuracy;
433
+ const bearing = this._map.getBearing();
434
+ const options = extend({bearing}, this.options.fitBoundsOptions);
435
+ const newBounds = LngLatBounds.fromLngLat(center, radius);
436
+
437
+ this._map.fitBounds(newBounds, options, {
438
+ geolocateSource: true // tag this camera change so it won't cause the control to change to background state
439
+ });
440
+ };
441
+
442
+ /**
443
+ * Update the user location dot Marker to the current position
444
+ *
445
+ * @param position - the Geolocation API Position
446
+ */
447
+ _updateMarker = (position?: GeolocationPosition | null) => {
448
+ if (position) {
449
+ const center = new LngLat(position.coords.longitude, position.coords.latitude);
450
+ this._accuracyCircleMarker.setLngLat(center).addTo(this._map);
451
+ this._userLocationDotMarker.setLngLat(center).addTo(this._map);
452
+ this._accuracy = position.coords.accuracy;
453
+ if (this.options.showUserLocation && this.options.showAccuracyCircle) {
454
+ this._updateCircleRadius();
455
+ }
456
+ } else {
457
+ this._userLocationDotMarker.remove();
458
+ this._accuracyCircleMarker.remove();
459
+ }
460
+ };
461
+
462
+ _updateCircleRadius() {
463
+ const bounds = this._map.getBounds();
464
+ const southEastPoint = bounds.getSouthEast();
465
+ const northEastPoint = bounds.getNorthEast();
466
+ const mapHeightInMeters = southEastPoint.distanceTo(northEastPoint);
467
+ const mapHeightInPixels = this._map._container.clientHeight;
468
+ const circleDiameter = Math.ceil(2 * (this._accuracy / (mapHeightInMeters / mapHeightInPixels)));
469
+ this._circleElement.style.width = `${circleDiameter}px`;
470
+ this._circleElement.style.height = `${circleDiameter}px`;
471
+ }
472
+
473
+ _onZoom = () => {
474
+ if (this.options.showUserLocation && this.options.showAccuracyCircle) {
475
+ this._updateCircleRadius();
476
+ }
477
+ };
478
+
479
+ _onError = (error: GeolocationPositionError) => {
480
+ if (!this._map) {
481
+ // control has since been removed
482
+ return;
483
+ }
484
+
485
+ if (this.options.trackUserLocation) {
486
+ if (error.code === 1) {
487
+ // PERMISSION_DENIED
488
+ this._watchState = 'OFF';
489
+ this._geolocateButton.classList.remove('maplibregl-ctrl-geolocate-waiting');
490
+ this._geolocateButton.classList.remove('maplibregl-ctrl-geolocate-active');
491
+ this._geolocateButton.classList.remove('maplibregl-ctrl-geolocate-active-error');
492
+ this._geolocateButton.classList.remove('maplibregl-ctrl-geolocate-background');
493
+ this._geolocateButton.classList.remove('maplibregl-ctrl-geolocate-background-error');
494
+ this._geolocateButton.disabled = true;
495
+ const title = this._map._getUIString('GeolocateControl.LocationNotAvailable');
496
+ this._geolocateButton.title = title;
497
+ this._geolocateButton.setAttribute('aria-label', title);
498
+
499
+ if (this._geolocationWatchID !== undefined) {
500
+ this._clearWatch();
501
+ }
502
+ } else if (error.code === 3 && noTimeout) {
503
+ // this represents a forced error state
504
+ // this was triggered to force immediate geolocation when a watch is already present
505
+ // see https://github.com/mapbox/mapbox-gl-js/issues/8214
506
+ // and https://w3c.github.io/geolocation-api/#example-5-forcing-the-user-agent-to-return-a-fresh-cached-position
507
+ return;
508
+ } else {
509
+ this._setErrorState();
510
+ }
511
+ }
512
+
513
+ if (this._watchState !== 'OFF' && this.options.showUserLocation) {
514
+ this._dotElement.classList.add('maplibregl-user-location-dot-stale');
515
+ }
516
+
517
+ this.fire(new Event('error', error));
518
+
519
+ this._finish();
520
+ };
521
+
522
+ _finish = () => {
523
+ if (this._timeoutId) { clearTimeout(this._timeoutId); }
524
+ this._timeoutId = undefined;
525
+ };
526
+
527
+ _setupUI = () => {
528
+ // the control could have been removed before reaching here
529
+ if (!this._map) {
530
+ return;
531
+ }
532
+
533
+ this._container.addEventListener('contextmenu', (e: MouseEvent) => e.preventDefault());
534
+ this._geolocateButton = DOM.create('button', 'maplibregl-ctrl-geolocate', this._container);
535
+ DOM.create('span', 'maplibregl-ctrl-icon', this._geolocateButton).setAttribute('aria-hidden', 'true');
536
+ this._geolocateButton.type = 'button';
537
+ this._geolocateButton.disabled = true;
538
+ };
539
+
540
+ _finishSetupUI = (supported: boolean) => {
541
+ // this method is called asynchronously during onAdd
542
+ if (!this._map) {
543
+ // control has since been removed
544
+ return;
545
+ }
546
+
547
+ if (supported === false) {
548
+ warnOnce('Geolocation support is not available so the GeolocateControl will be disabled.');
549
+ const title = this._map._getUIString('GeolocateControl.LocationNotAvailable');
550
+ this._geolocateButton.disabled = true;
551
+ this._geolocateButton.title = title;
552
+ this._geolocateButton.setAttribute('aria-label', title);
553
+ } else {
554
+ const title = this._map._getUIString('GeolocateControl.FindMyLocation');
555
+ this._geolocateButton.disabled = false;
556
+ this._geolocateButton.title = title;
557
+ this._geolocateButton.setAttribute('aria-label', title);
558
+ }
559
+
560
+ if (this.options.trackUserLocation) {
561
+ this._geolocateButton.setAttribute('aria-pressed', 'false');
562
+ this._watchState = 'OFF';
563
+ }
564
+
565
+ // when showUserLocation is enabled, keep the Geolocate button disabled until the device location marker is setup on the map
566
+ if (this.options.showUserLocation) {
567
+ this._dotElement = DOM.create('div', 'maplibregl-user-location-dot');
568
+
569
+ this._userLocationDotMarker = new Marker({element: this._dotElement});
570
+
571
+ this._circleElement = DOM.create('div', 'maplibregl-user-location-accuracy-circle');
572
+ this._accuracyCircleMarker = new Marker({element: this._circleElement, pitchAlignment: 'map'});
573
+
574
+ if (this.options.trackUserLocation) this._watchState = 'OFF';
575
+
576
+ this._map.on('zoom', this._onZoom);
577
+ }
578
+
579
+ this._geolocateButton.addEventListener('click', () => this.trigger());
580
+
581
+ this._setup = true;
582
+
583
+ // when the camera is changed (and it's not as a result of the Geolocation Control) change
584
+ // the watch mode to background watch, so that the marker is updated but not the camera.
585
+ if (this.options.trackUserLocation) {
586
+ this._map.on('movestart', (event: any) => {
587
+ const fromResize = event.originalEvent && event.originalEvent.type === 'resize';
588
+ if (!event.geolocateSource && this._watchState === 'ACTIVE_LOCK' && !fromResize) {
589
+ this._watchState = 'BACKGROUND';
590
+ this._geolocateButton.classList.add('maplibregl-ctrl-geolocate-background');
591
+ this._geolocateButton.classList.remove('maplibregl-ctrl-geolocate-active');
592
+
593
+ this.fire(new Event('trackuserlocationend'));
594
+ this.fire(new Event('userlocationlostfocus'));
595
+ }
596
+ });
597
+ }
598
+ };
599
+
600
+ /**
601
+ * Programmatically request and move the map to the user's location.
602
+ *
603
+ * @returns `false` if called before control was added to a map, otherwise returns `true`.
604
+ * @example
605
+ * ```ts
606
+ * // Initialize the geolocate control.
607
+ * let geolocate = new GeolocateControl({
608
+ * positionOptions: {
609
+ * enableHighAccuracy: true
610
+ * },
611
+ * trackUserLocation: true
612
+ * });
613
+ * // Add the control to the map.
614
+ * map.addControl(geolocate);
615
+ * map.on('load', () => {
616
+ * geolocate.trigger();
617
+ * });
618
+ * ```
619
+ */
620
+ trigger(): boolean {
621
+ if (!this._setup) {
622
+ warnOnce('Geolocate control triggered before added to a map');
623
+ return false;
624
+ }
625
+ if (this.options.trackUserLocation) {
626
+ // update watchState and do any outgoing state cleanup
627
+ switch (this._watchState) {
628
+ case 'OFF':
629
+ // turn on the Geolocate Control
630
+ this._watchState = 'WAITING_ACTIVE';
631
+
632
+ this.fire(new Event('trackuserlocationstart'));
633
+ break;
634
+ case 'WAITING_ACTIVE':
635
+ case 'ACTIVE_LOCK':
636
+ case 'ACTIVE_ERROR':
637
+ case 'BACKGROUND_ERROR':
638
+ // turn off the Geolocate Control
639
+ numberOfWatches--;
640
+ noTimeout = false;
641
+ this._watchState = 'OFF';
642
+ this._geolocateButton.classList.remove('maplibregl-ctrl-geolocate-waiting');
643
+ this._geolocateButton.classList.remove('maplibregl-ctrl-geolocate-active');
644
+ this._geolocateButton.classList.remove('maplibregl-ctrl-geolocate-active-error');
645
+ this._geolocateButton.classList.remove('maplibregl-ctrl-geolocate-background');
646
+ this._geolocateButton.classList.remove('maplibregl-ctrl-geolocate-background-error');
647
+
648
+ this.fire(new Event('trackuserlocationend'));
649
+ break;
650
+ case 'BACKGROUND':
651
+ this._watchState = 'ACTIVE_LOCK';
652
+ this._geolocateButton.classList.remove('maplibregl-ctrl-geolocate-background');
653
+ // set camera to last known location
654
+ if (this._lastKnownPosition) this._updateCamera(this._lastKnownPosition);
655
+
656
+ this.fire(new Event('trackuserlocationstart'));
657
+ this.fire(new Event('userlocationfocus'));
658
+ break;
659
+ default:
660
+ throw new Error(`Unexpected watchState ${this._watchState}`);
661
+ }
662
+
663
+ // incoming state setup
664
+ switch (this._watchState) {
665
+ case 'WAITING_ACTIVE':
666
+ this._geolocateButton.classList.add('maplibregl-ctrl-geolocate-waiting');
667
+ this._geolocateButton.classList.add('maplibregl-ctrl-geolocate-active');
668
+ break;
669
+ case 'ACTIVE_LOCK':
670
+ this._geolocateButton.classList.add('maplibregl-ctrl-geolocate-active');
671
+ break;
672
+ case 'OFF':
673
+ break;
674
+ default:
675
+ throw new Error(`Unexpected watchState ${this._watchState}`);
676
+ }
677
+
678
+ // manage geolocation.watchPosition / geolocation.clearWatch
679
+ if (this._watchState === 'OFF' && this._geolocationWatchID !== undefined) {
680
+ // clear watchPosition as we've changed to an OFF state
681
+ this._clearWatch();
682
+ } else if (this._geolocationWatchID === undefined) {
683
+ // enable watchPosition since watchState is not OFF and there is no watchPosition already running
684
+
685
+ this._geolocateButton.classList.add('maplibregl-ctrl-geolocate-waiting');
686
+ this._geolocateButton.setAttribute('aria-pressed', 'true');
687
+
688
+ numberOfWatches++;
689
+ let positionOptions;
690
+ if (numberOfWatches > 1) {
691
+ positionOptions = {maximumAge: 600000, timeout: 0};
692
+ noTimeout = true;
693
+ } else {
694
+ positionOptions = this.options.positionOptions;
695
+ noTimeout = false;
696
+ }
697
+
698
+ this._geolocationWatchID = window.navigator.geolocation.watchPosition(
699
+ this._onSuccess, this._onError, positionOptions);
700
+ }
701
+ } else {
702
+ window.navigator.geolocation.getCurrentPosition(
703
+ this._onSuccess, this._onError, this.options.positionOptions);
704
+
705
+ // This timeout ensures that we still call finish() even if
706
+ // the user declines to share their location in Firefox
707
+ this._timeoutId = setTimeout(this._finish, 10000 /* 10sec */);
708
+ }
709
+
710
+ return true;
711
+ }
712
+
713
+ _clearWatch() {
714
+ window.navigator.geolocation.clearWatch(this._geolocationWatchID);
715
+
716
+ this._geolocationWatchID = undefined;
717
+ this._geolocateButton.classList.remove('maplibregl-ctrl-geolocate-waiting');
718
+ this._geolocateButton.setAttribute('aria-pressed', 'false');
719
+
720
+ if (this.options.showUserLocation) {
721
+ this._updateMarker(null);
722
+ }
723
+ }
724
+ }
725
+