react-native-maplibre-lite 0.1.1

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 (99) hide show
  1. package/LICENSE +20 -0
  2. package/README.md +203 -0
  3. package/lib/module/assets/MAP_ASSETS.js +7 -0
  4. package/lib/module/assets/MAP_ASSETS.js.map +1 -0
  5. package/lib/module/assets/dark_placeholder.jpg +0 -0
  6. package/lib/module/assets/light_placeholder.jpg +0 -0
  7. package/lib/module/components/MapPlaceholder.js +37 -0
  8. package/lib/module/components/MapPlaceholder.js.map +1 -0
  9. package/lib/module/components/MapSelectPoint.js +88 -0
  10. package/lib/module/components/MapSelectPoint.js.map +1 -0
  11. package/lib/module/components/MapView.js +244 -0
  12. package/lib/module/components/MapView.js.map +1 -0
  13. package/lib/module/components/Marker.js +27 -0
  14. package/lib/module/components/Marker.js.map +1 -0
  15. package/lib/module/components/Polygon.js +27 -0
  16. package/lib/module/components/Polygon.js.map +1 -0
  17. package/lib/module/components/Polyline.js +27 -0
  18. package/lib/module/components/Polyline.js.map +1 -0
  19. package/lib/module/components/types.js +2 -0
  20. package/lib/module/components/types.js.map +1 -0
  21. package/lib/module/components/webFunctions/addMarkerWeb.js +57 -0
  22. package/lib/module/components/webFunctions/addMarkerWeb.js.map +1 -0
  23. package/lib/module/components/webFunctions/addPolygonWeb.js +62 -0
  24. package/lib/module/components/webFunctions/addPolygonWeb.js.map +1 -0
  25. package/lib/module/components/webFunctions/addPolylineWeb.js +48 -0
  26. package/lib/module/components/webFunctions/addPolylineWeb.js.map +1 -0
  27. package/lib/module/components/webFunctions/fitBoundsWeb.js +11 -0
  28. package/lib/module/components/webFunctions/fitBoundsWeb.js.map +1 -0
  29. package/lib/module/components/webFunctions/initWeb.js +156 -0
  30. package/lib/module/components/webFunctions/initWeb.js.map +1 -0
  31. package/lib/module/components/webFunctions/removeMarkerWeb.js +12 -0
  32. package/lib/module/components/webFunctions/removeMarkerWeb.js.map +1 -0
  33. package/lib/module/components/webFunctions/removePolygonWeb.js +22 -0
  34. package/lib/module/components/webFunctions/removePolygonWeb.js.map +1 -0
  35. package/lib/module/components/webFunctions/removePolylineWeb.js +17 -0
  36. package/lib/module/components/webFunctions/removePolylineWeb.js.map +1 -0
  37. package/lib/module/index.js +9 -0
  38. package/lib/module/index.js.map +1 -0
  39. package/lib/module/package.json +1 -0
  40. package/lib/module/webContent.js +173 -0
  41. package/lib/module/webContent.js.map +1 -0
  42. package/lib/typescript/package.json +1 -0
  43. package/lib/typescript/src/assets/MAP_ASSETS.d.ts +5 -0
  44. package/lib/typescript/src/assets/MAP_ASSETS.d.ts.map +1 -0
  45. package/lib/typescript/src/components/MapPlaceholder.d.ts +5 -0
  46. package/lib/typescript/src/components/MapPlaceholder.d.ts.map +1 -0
  47. package/lib/typescript/src/components/MapSelectPoint.d.ts +12 -0
  48. package/lib/typescript/src/components/MapSelectPoint.d.ts.map +1 -0
  49. package/lib/typescript/src/components/MapView.d.ts +41 -0
  50. package/lib/typescript/src/components/MapView.d.ts.map +1 -0
  51. package/lib/typescript/src/components/Marker.d.ts +3 -0
  52. package/lib/typescript/src/components/Marker.d.ts.map +1 -0
  53. package/lib/typescript/src/components/Polygon.d.ts +3 -0
  54. package/lib/typescript/src/components/Polygon.d.ts.map +1 -0
  55. package/lib/typescript/src/components/Polyline.d.ts +3 -0
  56. package/lib/typescript/src/components/Polyline.d.ts.map +1 -0
  57. package/lib/typescript/src/components/types.d.ts +38 -0
  58. package/lib/typescript/src/components/types.d.ts.map +1 -0
  59. package/lib/typescript/src/components/webFunctions/addMarkerWeb.d.ts +3 -0
  60. package/lib/typescript/src/components/webFunctions/addMarkerWeb.d.ts.map +1 -0
  61. package/lib/typescript/src/components/webFunctions/addPolygonWeb.d.ts +3 -0
  62. package/lib/typescript/src/components/webFunctions/addPolygonWeb.d.ts.map +1 -0
  63. package/lib/typescript/src/components/webFunctions/addPolylineWeb.d.ts +3 -0
  64. package/lib/typescript/src/components/webFunctions/addPolylineWeb.d.ts.map +1 -0
  65. package/lib/typescript/src/components/webFunctions/fitBoundsWeb.d.ts +3 -0
  66. package/lib/typescript/src/components/webFunctions/fitBoundsWeb.d.ts.map +1 -0
  67. package/lib/typescript/src/components/webFunctions/initWeb.d.ts +3 -0
  68. package/lib/typescript/src/components/webFunctions/initWeb.d.ts.map +1 -0
  69. package/lib/typescript/src/components/webFunctions/removeMarkerWeb.d.ts +3 -0
  70. package/lib/typescript/src/components/webFunctions/removeMarkerWeb.d.ts.map +1 -0
  71. package/lib/typescript/src/components/webFunctions/removePolygonWeb.d.ts +3 -0
  72. package/lib/typescript/src/components/webFunctions/removePolygonWeb.d.ts.map +1 -0
  73. package/lib/typescript/src/components/webFunctions/removePolylineWeb.d.ts +3 -0
  74. package/lib/typescript/src/components/webFunctions/removePolylineWeb.d.ts.map +1 -0
  75. package/lib/typescript/src/index.d.ts +9 -0
  76. package/lib/typescript/src/index.d.ts.map +1 -0
  77. package/lib/typescript/src/webContent.d.ts +3 -0
  78. package/lib/typescript/src/webContent.d.ts.map +1 -0
  79. package/package.json +158 -0
  80. package/src/assets/MAP_ASSETS.ts +4 -0
  81. package/src/assets/dark_placeholder.jpg +0 -0
  82. package/src/assets/light_placeholder.jpg +0 -0
  83. package/src/components/MapPlaceholder.tsx +44 -0
  84. package/src/components/MapSelectPoint.tsx +131 -0
  85. package/src/components/MapView.tsx +333 -0
  86. package/src/components/Marker.tsx +31 -0
  87. package/src/components/Polygon.tsx +31 -0
  88. package/src/components/Polyline.tsx +31 -0
  89. package/src/components/types.ts +46 -0
  90. package/src/components/webFunctions/addMarkerWeb.ts +54 -0
  91. package/src/components/webFunctions/addPolygonWeb.ts +59 -0
  92. package/src/components/webFunctions/addPolylineWeb.ts +45 -0
  93. package/src/components/webFunctions/fitBoundsWeb.ts +8 -0
  94. package/src/components/webFunctions/initWeb.ts +153 -0
  95. package/src/components/webFunctions/removeMarkerWeb.ts +9 -0
  96. package/src/components/webFunctions/removePolygonWeb.ts +19 -0
  97. package/src/components/webFunctions/removePolylineWeb.ts +14 -0
  98. package/src/index.tsx +10 -0
  99. package/src/webContent.ts +182 -0
@@ -0,0 +1,59 @@
1
+ export default /*js*/`
2
+
3
+ if(functionName === 'addPolygon') {
4
+
5
+ var sourceId = params.uniqueId + '-polygons-source';
6
+ var fillLayerId = params.uniqueId + '-polygons-fill';
7
+ var strokeLayerId = params.uniqueId + '-polygons-stroke';
8
+
9
+ if(map.getLayer(strokeLayerId)) {
10
+ map.removeLayer(strokeLayerId);
11
+ }
12
+ if(map.getLayer(fillLayerId)) {
13
+ map.removeLayer(fillLayerId);
14
+ }
15
+ if(map.getSource(sourceId)) {
16
+ map.removeSource(sourceId);
17
+ }
18
+
19
+ map.addSource(sourceId, {
20
+ 'type': 'geojson',
21
+ 'data': {
22
+ 'type': 'Feature',
23
+ 'geometry': {
24
+ 'type': 'Polygon',
25
+ 'coordinates': [params.coordinates]
26
+ }
27
+ }
28
+ });
29
+
30
+ var fillPaint = {};
31
+ fillPaint['fill-color'] = params.fillColor || 'rgba(0,0,0,0)';
32
+ if(params.fillOpacity != null) fillPaint['fill-opacity'] = params.fillOpacity;
33
+ if(params.strokeColor) fillPaint['fill-outline-color'] = params.strokeColor;
34
+
35
+ map.addLayer({
36
+ 'id': fillLayerId,
37
+ 'type': 'fill',
38
+ 'source': sourceId,
39
+ 'layout': {},
40
+ 'paint': fillPaint
41
+ });
42
+
43
+ if(params.strokeColor || params.strokeWidth) {
44
+ var linePaint = {};
45
+ if(params.strokeColor) linePaint['line-color'] = params.strokeColor;
46
+ if(params.strokeOpacity != null) linePaint['line-opacity'] = params.strokeOpacity;
47
+ if(params.strokeWidth != null) linePaint['line-width'] = params.strokeWidth;
48
+
49
+ map.addLayer({
50
+ 'id': strokeLayerId,
51
+ 'type': 'line',
52
+ 'source': sourceId,
53
+ 'layout': {},
54
+ 'paint': linePaint
55
+ });
56
+ }
57
+
58
+ }
59
+ `
@@ -0,0 +1,45 @@
1
+ export default /*js*/`
2
+
3
+ if(functionName === 'addPolyline') {
4
+
5
+
6
+
7
+ var sourceId = params.uniqueId + '-polylines-source';
8
+ var layerId = params.uniqueId + '-polylines-layout';
9
+
10
+ if(map.getLayer(layerId)) {
11
+ map.removeLayer(layerId);
12
+ }
13
+
14
+ if(map.getSource(sourceId)) {
15
+ map.removeSource(sourceId);
16
+ }
17
+
18
+ map.addSource(sourceId, {
19
+ 'type': 'geojson',
20
+ 'data': {
21
+ 'type': 'Feature',
22
+ 'properties': {},
23
+ 'geometry': {
24
+ 'type': 'LineString',
25
+ 'coordinates': params.coordinates
26
+ }
27
+ }
28
+ });
29
+
30
+ map.addLayer({
31
+ 'id': layerId,
32
+ 'type': 'line',
33
+ 'source': sourceId,
34
+ 'layout': {
35
+ 'line-join': 'round',
36
+ 'line-cap': 'round'
37
+ },
38
+ 'paint': {
39
+ 'line-color': params.color ?? '#000000',
40
+ 'line-width': params.width ?? 4
41
+ }
42
+ });
43
+
44
+ }
45
+ `
@@ -0,0 +1,8 @@
1
+ export default /*js*/`
2
+ if(functionName === 'fitBounds') {
3
+ map.fitBounds(params.bounds, {
4
+ padding: 40,
5
+ duration: 1000
6
+ });
7
+ }
8
+ `;
@@ -0,0 +1,153 @@
1
+ export default /*js*/`
2
+
3
+ if(functionName === 'init') {
4
+
5
+ function simplifyStyle(mapInstance, aggressive){
6
+ if(!mapInstance) return;
7
+ var style = mapInstance.getStyle();
8
+ var layers = style && style.layers ? style.layers : [];
9
+ for(var i = 0; i < layers.length; i++){
10
+ var layer = layers[i];
11
+ var layerId = layer.id;
12
+ var layerType = layer.type;
13
+ var shouldHide = false;
14
+
15
+ if(layerType === 'symbol' || layerType === 'fill-extrusion' || layerType === 'hillshade'){
16
+ shouldHide = true;
17
+ }
18
+
19
+ if(aggressive && (layerType === 'line' || layerType === 'circle')){
20
+ var layerIdLower = String(layerId || '').toLowerCase();
21
+ if(
22
+ layerIdLower.indexOf('road-label') !== -1 ||
23
+ layerIdLower.indexOf('poi') !== -1 ||
24
+ layerIdLower.indexOf('label') !== -1 ||
25
+ layerIdLower.indexOf('boundary') !== -1 ||
26
+ layerIdLower.indexOf('admin') !== -1
27
+ ){
28
+ shouldHide = true;
29
+ }
30
+ }
31
+
32
+ if(shouldHide){
33
+ try{
34
+ mapInstance.setLayoutProperty(layerId, 'visibility', 'none');
35
+ } catch (_) {}
36
+ }
37
+ }
38
+ }
39
+
40
+ function setOverlayLayersVisibility(mapInstance, visibility){
41
+ if(!mapInstance) return;
42
+ var style = mapInstance.getStyle();
43
+ var layers = style && style.layers ? style.layers : [];
44
+ for(var i = 0; i < layers.length; i++){
45
+ var layer = layers[i];
46
+ var layerId = String(layer.id || '');
47
+ var isCustomOverlay =
48
+ layerId.indexOf('-polylines-layout') !== -1 ||
49
+ layerId.indexOf('-polygons-fill') !== -1 ||
50
+ layerId.indexOf('-polygons-stroke') !== -1;
51
+
52
+ if(isCustomOverlay){
53
+ try{
54
+ mapInstance.setLayoutProperty(layerId, 'visibility', visibility);
55
+ } catch (_) {}
56
+ }
57
+ }
58
+ }
59
+
60
+
61
+ map = new maplibregl.Map({
62
+ container: 'map',
63
+ style: params.style,
64
+ //Moscow coordinates
65
+ center: params.center, // starting position [lng, lat]
66
+ zoom: params.zoom, // starting zoom
67
+
68
+
69
+ // общий выключатель
70
+ zoomControl: params.zoomEnabled,
71
+ // точнее
72
+ scrollZoom: params.scrollEnabled, // колесо мыши / трекпад
73
+ doubleClickZoom: params.zoomEnabled,
74
+ touchZoomRotate: params.zoomEnabled,
75
+ minZoom: params.minZoom,
76
+ maxZoom: params.maxZoom,
77
+ dragPan: params.scrollEnabled, // запрет перетаскивания
78
+ dragRotate: params.zoomEnabled,
79
+ antialias: params.antialias ?? false,
80
+ crossSourceCollisions: params.crossSourceCollisions ?? true,
81
+ fadeDuration: params.fadeDuration ?? 120,
82
+ pixelRatio: params.pixelRatio,
83
+ renderWorldCopies: params.renderWorldCopies ?? true
84
+ });
85
+
86
+ if (typeof params.maxPitch === 'number') {
87
+ map.setMaxPitch(params.maxPitch);
88
+ }
89
+
90
+ if (params.maxPitch === 0) {
91
+ map.dragRotate.disable();
92
+ map.touchZoomRotate.disableRotation();
93
+ }
94
+
95
+
96
+
97
+ function getEventParams(){
98
+ if(!map) return {
99
+ center: null,
100
+ zoom: null
101
+ }
102
+ var center = map.getCenter();
103
+ var zoom = map.getZoom();
104
+
105
+
106
+ return {
107
+ center,
108
+ zoom
109
+ }
110
+ }
111
+
112
+ //movestart moveend
113
+ map.on("movestart",(event)=>{
114
+ if(params.turboWhileMoving){
115
+ setOverlayLayersVisibility(map, 'none');
116
+ }
117
+ window.ReactNativeWebView.postMessage(JSON.stringify({ type: 'event', event: 'movestart', params: getEventParams() }));
118
+ })
119
+ map.on("moveend",(event)=>{
120
+ if(params.turboWhileMoving){
121
+ setOverlayLayersVisibility(map, 'visible');
122
+ }
123
+ window.ReactNativeWebView.postMessage(JSON.stringify({ type: 'event', event: 'moveend', params: getEventParams() }));
124
+ })
125
+ /*zoomstart zoomend*/
126
+ map.on("zoomstart",(event)=>{
127
+ window.ReactNativeWebView.postMessage(JSON.stringify({ type: 'event', event: 'zoomstart', params: getEventParams() }));
128
+ })
129
+ map.on("zoomend",(event)=>{
130
+ window.ReactNativeWebView.postMessage(JSON.stringify({ type: 'event', event: 'zoomend', params: getEventParams() }));
131
+ })
132
+
133
+
134
+ map.on("idle",(event)=>{
135
+ window.ReactNativeWebView.postMessage(JSON.stringify({ type: 'event', event: 'idle', params: getEventParams() }));
136
+ })
137
+
138
+
139
+
140
+
141
+
142
+ map.on('load', function () {
143
+ if(params.simplifyStyle){
144
+ simplifyStyle(map, Boolean(params.aggressiveSimplifyStyle));
145
+ }
146
+
147
+ window.ReactNativeWebView.postMessage(JSON.stringify({ type: 'inited' }));
148
+ });
149
+
150
+
151
+
152
+ }
153
+ `
@@ -0,0 +1,9 @@
1
+ export default /*js*/`
2
+
3
+ if(functionName === 'removeMarker') {
4
+ if(markers[params.uniqueId]){
5
+ markers[params.uniqueId].remove();
6
+ delete markers[params.uniqueId];
7
+ }
8
+ }
9
+ `
@@ -0,0 +1,19 @@
1
+ export default /*js*/`
2
+
3
+ if(functionName === 'removePolygon') {
4
+ var sourceId = params.uniqueId + '-polygons-source';
5
+ var fillLayerId = params.uniqueId + '-polygons-fill';
6
+ var strokeLayerId = params.uniqueId + '-polygons-stroke';
7
+
8
+ if(map.getLayer(strokeLayerId)) {
9
+ map.removeLayer(strokeLayerId);
10
+ }
11
+ if(map.getLayer(fillLayerId)) {
12
+ map.removeLayer(fillLayerId);
13
+ }
14
+ if(map.getSource(sourceId)) {
15
+ map.removeSource(sourceId);
16
+ }
17
+
18
+ }
19
+ `
@@ -0,0 +1,14 @@
1
+ export default /*js*/`
2
+
3
+ if(functionName === 'removePolyline') {
4
+ var sourceId = params.uniqueId + '-polylines-source';
5
+ var layerId = params.uniqueId + '-polylines-layout';
6
+
7
+ if(map.getLayer(layerId)) {
8
+ map.removeLayer(layerId);
9
+ }
10
+ if(map.getSource(sourceId)) {
11
+ map.removeSource(sourceId);
12
+ }
13
+ }
14
+ `;
package/src/index.tsx ADDED
@@ -0,0 +1,10 @@
1
+ import { MapView } from './components/MapView';
2
+ import { Marker } from './components/Marker';
3
+ import { Polygon } from './components/Polygon';
4
+ import { Polyline } from './components/Polyline';
5
+
6
+ export { MapView, Marker, Polygon, Polyline };
7
+ export default MapView;
8
+
9
+ export type { MapViewRef } from './components/MapView';
10
+ export type { MarkerProps, PolygonProps, PolylineProps, EventParams } from './components/types';
@@ -0,0 +1,182 @@
1
+ import AsyncStorage from '@react-native-async-storage/async-storage';
2
+
3
+ import type { SourcesProps } from './components/types';
4
+
5
+ const CACHE_VERSION = 'v1';
6
+
7
+ const loadResources = async (url: string) => {
8
+ const cacheKey = `${url}@${CACHE_VERSION}`;
9
+
10
+ try {
11
+ const stored = await AsyncStorage.getItem(cacheKey);
12
+ if (stored) {
13
+ return stored;
14
+ }
15
+ } catch (_) {
16
+ // cache miss — proceed to fetch
17
+ }
18
+
19
+ const response = await fetch(url);
20
+ if (!response.ok) {
21
+ throw new Error(`Failed to load resource: ${url} (${response.status})`);
22
+ }
23
+ const text = await response.text();
24
+
25
+ if (text) {
26
+ try {
27
+ await AsyncStorage.setItem(cacheKey, text);
28
+ } catch (_) {
29
+ // storage full or unavailable — non-fatal
30
+ }
31
+ }
32
+ return text;
33
+ }
34
+
35
+ export const maplibreHtmlMap = async (webFunctions: string, sources: SourcesProps) => {
36
+ //Загружаем скрипты как текст
37
+
38
+
39
+ let maplibreJS = await loadResources(sources.maplibreJS);
40
+ let maplibreCSS = await loadResources(sources.maplibreCSS);
41
+ let pmtiles = await loadResources(sources.pmtilesJS);
42
+
43
+
44
+
45
+ return `<!DOCTYPE html>
46
+ <html lang="">
47
+
48
+ <head>
49
+ <meta charset="utf-8" />
50
+ <meta name="viewport" content="width=device-width, initial-scale=1" />
51
+ <title></title>
52
+
53
+
54
+ <script>${maplibreJS}</script>
55
+
56
+ <style>${maplibreCSS}</style>
57
+
58
+
59
+ <script>${pmtiles}</script>
60
+
61
+ <style>
62
+
63
+
64
+ html,
65
+ body {
66
+
67
+ overflow: hidden;
68
+ height: 100vh;
69
+ width: 100vw;
70
+ margin: 0;
71
+ padding: 0;
72
+
73
+ }
74
+
75
+
76
+
77
+ * {
78
+ -webkit-user-select: none;
79
+ -khtml-user-select: none;
80
+ -moz-user-select: none;
81
+ -ms-user-select: none;
82
+ user-select: none;
83
+ }
84
+
85
+
86
+ * {
87
+ -webkit-touch-callout: none;
88
+ -moz-touch-callout: none;
89
+ -ms-touch-callout: none;
90
+ touch-callout: none;
91
+ }
92
+
93
+ * {
94
+ -webkit-user-drag: none;
95
+ -moz-user-drag: none;
96
+ -ms-user-drag: none;
97
+ user-drag: none;
98
+ }
99
+
100
+ .maplibregl-ctrl-bottom-right {
101
+ display: none;
102
+ }
103
+
104
+
105
+ #map {
106
+
107
+
108
+ height: 100vh;
109
+ width: 100vw;
110
+ }
111
+ </style>
112
+
113
+ </head>
114
+
115
+ <body>
116
+ <div id="map"></div>
117
+ </body>
118
+
119
+ <script>
120
+
121
+ var markers = {};
122
+ var polylines = {};
123
+ var polygons = {};
124
+ var rectangles = {};
125
+ var layouts = {};
126
+
127
+ var map = null;
128
+
129
+ try {
130
+ let protocol = new pmtiles.Protocol();
131
+ maplibregl.addProtocol("pmtiles", protocol.tile);
132
+ } catch (e) {
133
+ console.error('pmtiles init error:', e.message);
134
+ }
135
+ window.onload = function () {
136
+ document.addEventListener("message", function (event) {
137
+ receiveMessage(event.data);
138
+ });
139
+ window.addEventListener("message", function (event) {
140
+ receiveMessage(event.data);
141
+ });
142
+ if (window.ReactNativeWebView) {
143
+ window.ReactNativeWebView.postMessage(JSON.stringify({ type: 'scriptReady' }));
144
+ }
145
+ }
146
+
147
+
148
+ const receiveMessage = (message) => {
149
+
150
+ var data = JSON.parse(message);
151
+
152
+ var functionName = data.function;
153
+ var params = data.params;
154
+
155
+
156
+
157
+
158
+
159
+
160
+
161
+ try{
162
+
163
+ ${webFunctions}
164
+
165
+ } catch (e) {
166
+ console.error('WebView function error:', functionName, e.message);
167
+ window.ReactNativeWebView.postMessage(JSON.stringify({
168
+ type: 'error',
169
+ data: {
170
+ target: functionName,
171
+ message: String(e.message),
172
+ }
173
+ }));
174
+ }
175
+ };
176
+
177
+
178
+ </script>
179
+
180
+
181
+ </html>`;
182
+ };