@osimatic/helpers-js 1.4.3 → 1.4.5

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.
package/location.js CHANGED
@@ -429,6 +429,10 @@ class PostalAddress {
429
429
 
430
430
  class Polygon {
431
431
  static format(geoJsonPolygon) {
432
+ if (typeof geoJsonPolygon == 'string') {
433
+ geoJsonPolygon = JSON.parse(geoJsonPolygon);
434
+ }
435
+
432
436
  const rings = geoJsonPolygon.coordinates || [];
433
437
  const ring0 = rings[0] || [];
434
438
  const n = ring0.length ? ring0.length - (JSON.stringify(ring0[0])===JSON.stringify(ring0[ring0.length-1]) ? 1 : 0) : 0;
@@ -504,6 +508,10 @@ class GeographicCoordinates {
504
508
  }
505
509
 
506
510
  static formatPoint(geoJsonPoint, fractionDigit=6) {
511
+ if (typeof geoJsonPoint == 'string') {
512
+ geoJsonPoint = JSON.parse(geoJsonPoint);
513
+ }
514
+
507
515
  const [long, lat] = geoJsonPoint.coordinates || [];
508
516
  if (typeof long == 'undefined' || typeof lat == 'undefined') {
509
517
  return '';
@@ -515,6 +523,37 @@ class GeographicCoordinates {
515
523
  return { type: 'Point', coordinates: [Number(long), Number(lat)] };
516
524
  }
517
525
 
526
+ static getAllLatLongsFromGeoJsonList(geoJsonList) {
527
+ if (typeof geoJsonList == 'string') {
528
+ geoJsonList = JSON.parse(geoJsonList);
529
+ }
530
+
531
+ const arr = [];
532
+ for (const geoJsonItem of geoJsonList) {
533
+ if (!geoJsonItem) {
534
+ continue;
535
+ }
536
+
537
+ if (geoJsonItem.type === 'Point') {
538
+ const [lon, lat] = geoJsonItem.coordinates || [];
539
+ arr.push([lat, lon]);
540
+ continue;
541
+ }
542
+
543
+ if (geoJsonItem.type === 'Polygon') {
544
+ for (const ring of geoJsonItem.coordinates || []) {
545
+ for (const [lon,lat] of ring) {
546
+ arr.push([lat, lon]);
547
+ }
548
+ }
549
+ continue;
550
+ }
551
+ }
552
+
553
+ return arr;
554
+ }
555
+
556
+
518
557
  static haversine(lat1, long1, lat2, long2) {
519
558
  const R = 6371000;
520
559
  const toRad = d => d * Math.PI / 180;
@@ -112,19 +112,43 @@ class OpenStreetMap {
112
112
  }
113
113
 
114
114
  centerOnFrance() {
115
- this.map.setView([46.52863469527167, 2.43896484375], 6);
115
+ OpenStreetMap.centerOnFrance(this.map);
116
+ }
117
+
118
+ static centerOnFrance(map) {
119
+ map.setView([46.52863469527167, 2.43896484375], 6);
116
120
  }
117
121
 
118
122
  centerOnMarkers(padding) {
119
- this.map.invalidateSize(false);
123
+ OpenStreetMap.centerMapToLocations(this.map, this.locations, padding);
124
+ }
120
125
 
121
- if (this.locations.length === 0) {
126
+ static centerMapToLocations(map, locationsList, padding=[20,20]) {
127
+ if (!map || locationsList.length === 0) {
122
128
  return;
123
129
  }
124
130
 
125
- this.map.fitBounds(new L.LatLngBounds(this.locations), {
126
- padding: typeof padding != 'undefined' ? padding : [0, 0]
127
- });
131
+ map.invalidateSize(false);
132
+
133
+ const bounds = L.latLngBounds(locationsList);
134
+ map.fitBounds(bounds, { padding: padding });
135
+ if (map.getZoom() > 18) {
136
+ map.setZoom(18);
137
+ }
138
+ }
139
+
140
+ static centerMapToGooglePlace(map, place) {
141
+ if (place?.geometry?.location) {
142
+ const loc = place.geometry.location;
143
+ OpenStreetMap.centerMapToCoordinates(map, loc.lat(), loc.lng());
144
+ }
145
+ }
146
+
147
+ static centerMapToCoordinates(map, lat, long) {
148
+ if (!map) {
149
+ return;
150
+ }
151
+ map.setView([lat, long], Math.max(map.getZoom(), 15));
128
152
  }
129
153
 
130
154
  connectMarkers() {
@@ -144,7 +168,6 @@ class OpenStreetMap {
144
168
  listLineCoordinates.forEach(line => {
145
169
  L.polyline(line, {color: '#728bec'}).addTo(this.map);
146
170
  });
147
-
148
171
  }
149
172
  }
150
173
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@osimatic/helpers-js",
3
- "version": "1.4.3",
3
+ "version": "1.4.5",
4
4
  "main": "main.js",
5
5
  "scripts": {
6
6
  "test": "echo \"Error: no test specified\" && exit 1"