vue-geojson-view-ts 1.3.19 → 1.3.21

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.
@@ -1,309 +1,309 @@
1
- <template>
2
- <div class="map-container" :style="`height:${configurationMap?.height}`">
3
- <div :id="idMap" style="height: 100%"></div>
4
- </div>
5
- </template>
6
-
7
- <script setup lang="ts">
8
- import * as L from "leaflet";
9
- import "leaflet/dist/leaflet.css";
10
- import { onMounted, ref } from "vue";
11
- import { markerDefault } from "../helpers/imgBase64";
12
- import "leaflet.fullscreen/Control.FullScreen.css";
13
- import "leaflet.fullscreen";
14
- import axios from "axios";
15
- import gpsIcon from "../assets/gps.svg";
16
- import sateliteIcon from "../assets/satelite.svg";
17
-
18
- export type TIconMaker = {
19
- iconUrl: string;
20
- iconSize: [number, number];
21
- iconAnchor: [number, number];
22
- class?: string;
23
- type: "image" | "svg";
24
- };
25
-
26
- export interface IConfigurationMap {
27
- height: string;
28
- maxZoom: number;
29
- iconMarker: TIconMaker;
30
- dragMarker: boolean;
31
- }
32
-
33
- export interface ICoords {
34
- lat: number;
35
- lng: number;
36
- zoom: number;
37
- }
38
-
39
- export interface IMapSearchAddressProps {
40
- configurationMap?: IConfigurationMap;
41
- coordinatesMap?: ICoords;
42
- isSatelite?: boolean;
43
- }
44
-
45
- const props = withDefaults(defineProps<IMapSearchAddressProps>(), {
46
- configurationMap: () => {
47
- return {
48
- height: "250px",
49
- maxZoom: 20,
50
- iconMarker: {
51
- iconUrl: markerDefault,
52
- iconSize: [25, 41],
53
- iconAnchor: [12, 41],
54
- class: "",
55
- type: "image",
56
- },
57
- dragMarker: true,
58
- };
59
- },
60
- coordinatesMap: () => {
61
- return {
62
- lat: -19.0429,
63
- lng: -65.2554,
64
- zoom: 12,
65
- };
66
- },
67
- });
68
-
69
- const emit = defineEmits(["updated:coordsMarker"]);
70
-
71
- const idMap = ref();
72
- const coords = ref(props.coordinatesMap);
73
- const mapRender = ref();
74
- const markerRender = ref();
75
- const currentTileLayer = ref<L.TileLayer | null>(null);
76
- const isSatelliteView = ref(false);
77
-
78
- onMounted(async () => {
79
- await makeid(10);
80
- await renderMap();
81
- });
82
-
83
- const makeid = async (length: number) => {
84
- let result = "";
85
- const characters = "abcdefghijklmnopqrstuvwxyz";
86
- const charactersLength = characters.length;
87
- let counter = 0;
88
- while (counter < length) {
89
- result += characters.charAt(Math.floor(Math.random() * charactersLength));
90
- counter += 1;
91
- }
92
- idMap.value = result;
93
- };
94
-
95
- const renderMap = async () => {
96
- await createMap();
97
- };
98
-
99
- const createMap = async () => {
100
- const iconMarker = props.configurationMap?.iconMarker;
101
- const map = L.map(idMap.value, { fullscreenControl: true } as any).setView(
102
- [coords.value.lat, coords.value.lng],
103
- coords.value.zoom
104
- );
105
- map.invalidateSize();
106
- mapRender.value = map;
107
-
108
- // Inicializar con la vista correcta según el prop
109
- isSatelliteView.value = props.isSatelite || false;
110
- if (isSatelliteView.value) {
111
- currentTileLayer.value = L.tileLayer(
112
- "https://api.maptiler.com/maps/satellite/{z}/{x}/{y}.jpg?key=t8mWT2ozs1JWBqMZOnZr",
113
- {
114
- attribution:
115
- '&copy; <a href="https://www.maptiler.com/">MapTiler</a> &copy; <a href="http://www.openstreetmap.org/copyright">OpenStreetMap</a>',
116
- }
117
- );
118
- } else {
119
- currentTileLayer.value = L.tileLayer(
120
- "https://tile.openstreetmap.org/{z}/{x}/{y}.png",
121
- {
122
- maxZoom: props.configurationMap?.maxZoom,
123
- attribution:
124
- '&copy; <a href="http://www.openstreetmap.org/copyright">OpenStreetMap</a>',
125
- }
126
- );
127
- }
128
- currentTileLayer.value.addTo(map);
129
-
130
- // Agregar el control satelital si está habilitado
131
- if (props.isSatelite !== undefined) {
132
- const satelliteControl = createSatelliteControl();
133
- map.addControl(satelliteControl);
134
- }
135
-
136
- if (iconMarker.type === "image") {
137
- const marker = L.marker([coords.value.lat, coords.value.lng], {
138
- icon: L.icon({
139
- iconUrl: iconMarker.iconUrl,
140
- iconAnchor: iconMarker.iconAnchor,
141
- iconSize: iconMarker.iconSize,
142
- className: iconMarker.class,
143
- }),
144
- draggable: props.configurationMap.dragMarker,
145
- }).addTo(map);
146
- markerRender.value = marker;
147
- marker.on("dragend", (event) => {
148
- const updatedCoordinates = event.target.getLatLng();
149
- const lat = updatedCoordinates.lat;
150
- const lng = updatedCoordinates.lng;
151
- setCoordinates({
152
- lat,
153
- lng,
154
- });
155
- });
156
- } else if (iconMarker.type === "svg") {
157
- const marker = L.marker([coords.value.lat, coords.value.lng], {
158
- icon: L.divIcon({
159
- html: iconMarker.iconUrl,
160
- iconAnchor: iconMarker.iconAnchor,
161
- iconSize: iconMarker.iconSize,
162
- className: iconMarker.class,
163
- }),
164
- draggable: props.configurationMap.dragMarker,
165
- }).addTo(map);
166
- markerRender.value = marker;
167
- marker.on("dragend", (event) => {
168
- const updatedCoordinates = event.target.getLatLng();
169
- const lat = updatedCoordinates.lat;
170
- const lng = updatedCoordinates.lng;
171
- setCoordinates({
172
- lat,
173
- lng,
174
- });
175
- });
176
- }
177
- };
178
-
179
- const createSatelliteControl = (): L.Control => {
180
- const SatelliteControl = L.Control.extend({
181
- onAdd: (map: L.Map) => {
182
- const container = L.DomUtil.create(
183
- "div",
184
- "leaflet-control-draw leaflet-bar leaflet-control"
185
- );
186
-
187
- const satelliteButton = L.DomUtil.create(
188
- "a",
189
- "leaflet-draw-draw-satellite",
190
- container
191
- );
192
-
193
- satelliteButton.href = "#";
194
- satelliteButton.title = isSatelliteView.value
195
- ? "Vista Normal"
196
- : "Vista Satélite";
197
-
198
- // Usar el mismo estilo que los otros botones de draw
199
- satelliteButton.style.backgroundImage = isSatelliteView.value
200
- ? `url(${gpsIcon})`
201
- : `url(${sateliteIcon})`;
202
- satelliteButton.style.backgroundPosition = "center";
203
- satelliteButton.style.backgroundRepeat = "no-repeat";
204
- satelliteButton.style.backgroundSize = "20px 20px";
205
-
206
- L.DomEvent.disableClickPropagation(satelliteButton);
207
- L.DomEvent.on(satelliteButton, "click", toggleSatelliteView);
208
-
209
- return container;
210
- },
211
- });
212
-
213
- return new SatelliteControl({ position: "topleft" });
214
- };
215
-
216
- const toggleSatelliteView = (): void => {
217
- if (!mapRender.value || !currentTileLayer.value) return;
218
-
219
- isSatelliteView.value = !isSatelliteView.value;
220
-
221
- // Remover la capa actual
222
- mapRender.value.removeLayer(currentTileLayer.value as unknown as L.Layer);
223
-
224
- // Agregar la nueva capa
225
- if (isSatelliteView.value) {
226
- currentTileLayer.value = L.tileLayer(
227
- "https://api.maptiler.com/maps/satellite/{z}/{x}/{y}.jpg?key=t8mWT2ozs1JWBqMZOnZr",
228
- {
229
- attribution:
230
- '&copy; <a href="https://www.maptiler.com/">MapTiler</a> &copy; <a href="http://www.openstreetmap.org/copyright">OpenStreetMap</a>',
231
- }
232
- );
233
- } else {
234
- currentTileLayer.value = L.tileLayer(
235
- "https://tile.openstreetmap.org/{z}/{x}/{y}.png",
236
- {
237
- attribution:
238
- '&copy; <a href="http://www.openstreetmap.org/copyright">OpenStreetMap</a>',
239
- }
240
- );
241
- }
242
-
243
- currentTileLayer.value.addTo(mapRender.value as L.Map);
244
-
245
- // Actualizar el ícono del control satelital
246
- const satelliteButtons = document.querySelectorAll(
247
- ".leaflet-draw-draw-satellite"
248
- );
249
- satelliteButtons.forEach((button) => {
250
- const htmlButton = button as HTMLElement;
251
- htmlButton.style.backgroundImage = isSatelliteView.value
252
- ? `url(${gpsIcon})`
253
- : `url(${sateliteIcon})`;
254
- htmlButton.title = isSatelliteView.value
255
- ? "Vista Normal"
256
- : "Vista Satélite";
257
- });
258
- };
259
-
260
- const searchAddress = async (query: string) => {
261
- const address = await axios.get(
262
- location.protocol + "//nominatim.openstreetmap.org/search?",
263
- {
264
- params: {
265
- //query
266
- q: query,
267
- limit: 1,
268
- format: "json",
269
- },
270
- }
271
- );
272
- if (address.data.length === 1) {
273
- const lat = parseFloat(address.data[0].lat);
274
- const lng = parseFloat(address.data[0].lon);
275
- const coord = { lng, lat };
276
- setCoordinates({ ...coord, moveMarker: true });
277
- return address.data[0];
278
- }
279
- return address.data;
280
- };
281
-
282
- const setCoordinates = ({
283
- lat,
284
- lng,
285
- moveMarker = false,
286
- }: {
287
- lat: number;
288
- lng: number;
289
- moveMarker?: boolean;
290
- }) => {
291
- emit("updated:coordsMarker", {
292
- lat,
293
- lng,
294
- });
295
- if (moveMarker) markerRender.value?.setLatLng({ lat, lng });
296
- moveCenterTo({ lat, lng });
297
- };
298
-
299
- const moveCenterTo = ({ lat, lng }: { lat: number; lng: number }) => {
300
- const newCenter = L.latLng(lat, lng);
301
- mapRender.value.panTo(newCenter, { animate: true, duration: 0.5 });
302
- };
303
-
304
- defineExpose({
305
- searchAddress,
306
- });
307
- </script>
308
-
309
- <style lang="scss"></style>
1
+ <template>
2
+ <div class="map-container" :style="`height:${configurationMap?.height}`">
3
+ <div :id="idMap" style="height: 100%"></div>
4
+ </div>
5
+ </template>
6
+
7
+ <script setup lang="ts">
8
+ import * as L from "leaflet";
9
+ import "leaflet/dist/leaflet.css";
10
+ import { onMounted, ref } from "vue";
11
+ import { markerDefault } from "../helpers/imgBase64";
12
+ import "leaflet.fullscreen/Control.FullScreen.css";
13
+ import "leaflet.fullscreen";
14
+ import axios from "axios";
15
+ import gpsIcon from "../assets/gps.svg";
16
+ import sateliteIcon from "../assets/satelite.svg";
17
+
18
+ export type TIconMaker = {
19
+ iconUrl: string;
20
+ iconSize: [number, number];
21
+ iconAnchor: [number, number];
22
+ class?: string;
23
+ type: "image" | "svg";
24
+ };
25
+
26
+ export interface IConfigurationMap {
27
+ height: string;
28
+ maxZoom: number;
29
+ iconMarker: TIconMaker;
30
+ dragMarker: boolean;
31
+ }
32
+
33
+ export interface ICoords {
34
+ lat: number;
35
+ lng: number;
36
+ zoom: number;
37
+ }
38
+
39
+ export interface IMapSearchAddressProps {
40
+ configurationMap?: IConfigurationMap;
41
+ coordinatesMap?: ICoords;
42
+ isSatelite?: boolean;
43
+ }
44
+
45
+ const props = withDefaults(defineProps<IMapSearchAddressProps>(), {
46
+ configurationMap: () => {
47
+ return {
48
+ height: "250px",
49
+ maxZoom: 20,
50
+ iconMarker: {
51
+ iconUrl: markerDefault,
52
+ iconSize: [25, 41],
53
+ iconAnchor: [12, 41],
54
+ class: "",
55
+ type: "image",
56
+ },
57
+ dragMarker: true,
58
+ };
59
+ },
60
+ coordinatesMap: () => {
61
+ return {
62
+ lat: -19.0429,
63
+ lng: -65.2554,
64
+ zoom: 12,
65
+ };
66
+ },
67
+ });
68
+
69
+ const emit = defineEmits(["updated:coordsMarker"]);
70
+
71
+ const idMap = ref();
72
+ const coords = ref(props.coordinatesMap);
73
+ const mapRender = ref();
74
+ const markerRender = ref();
75
+ const currentTileLayer = ref<L.TileLayer | null>(null);
76
+ const isSatelliteView = ref(false);
77
+
78
+ onMounted(async () => {
79
+ await makeid(10);
80
+ await renderMap();
81
+ });
82
+
83
+ const makeid = async (length: number) => {
84
+ let result = "";
85
+ const characters = "abcdefghijklmnopqrstuvwxyz";
86
+ const charactersLength = characters.length;
87
+ let counter = 0;
88
+ while (counter < length) {
89
+ result += characters.charAt(Math.floor(Math.random() * charactersLength));
90
+ counter += 1;
91
+ }
92
+ idMap.value = result;
93
+ };
94
+
95
+ const renderMap = async () => {
96
+ await createMap();
97
+ };
98
+
99
+ const createMap = async () => {
100
+ const iconMarker = props.configurationMap?.iconMarker;
101
+ const map = L.map(idMap.value, { fullscreenControl: true } as any).setView(
102
+ [coords.value.lat, coords.value.lng],
103
+ coords.value.zoom
104
+ );
105
+ map.invalidateSize();
106
+ mapRender.value = map;
107
+
108
+ // Inicializar con la vista correcta según el prop
109
+ isSatelliteView.value = props.isSatelite || false;
110
+ if (isSatelliteView.value) {
111
+ currentTileLayer.value = L.tileLayer(
112
+ "https://api.maptiler.com/maps/satellite/{z}/{x}/{y}.jpg?key=t8mWT2ozs1JWBqMZOnZr",
113
+ {
114
+ attribution:
115
+ '&copy; <a href="https://www.maptiler.com/">MapTiler</a> &copy; <a href="http://www.openstreetmap.org/copyright">OpenStreetMap</a>',
116
+ }
117
+ );
118
+ } else {
119
+ currentTileLayer.value = L.tileLayer(
120
+ "https://tile.openstreetmap.org/{z}/{x}/{y}.png",
121
+ {
122
+ maxZoom: props.configurationMap?.maxZoom,
123
+ attribution:
124
+ '&copy; <a href="http://www.openstreetmap.org/copyright">OpenStreetMap</a>',
125
+ }
126
+ );
127
+ }
128
+ currentTileLayer.value.addTo(map);
129
+
130
+ // Agregar el control satelital si está habilitado
131
+ if (props.isSatelite !== undefined) {
132
+ const satelliteControl = createSatelliteControl();
133
+ map.addControl(satelliteControl);
134
+ }
135
+
136
+ if (iconMarker.type === "image") {
137
+ const marker = L.marker([coords.value.lat, coords.value.lng], {
138
+ icon: L.icon({
139
+ iconUrl: iconMarker.iconUrl,
140
+ iconAnchor: iconMarker.iconAnchor,
141
+ iconSize: iconMarker.iconSize,
142
+ className: iconMarker.class,
143
+ }),
144
+ draggable: props.configurationMap.dragMarker,
145
+ }).addTo(map);
146
+ markerRender.value = marker;
147
+ marker.on("dragend", (event) => {
148
+ const updatedCoordinates = event.target.getLatLng();
149
+ const lat = updatedCoordinates.lat;
150
+ const lng = updatedCoordinates.lng;
151
+ setCoordinates({
152
+ lat,
153
+ lng,
154
+ });
155
+ });
156
+ } else if (iconMarker.type === "svg") {
157
+ const marker = L.marker([coords.value.lat, coords.value.lng], {
158
+ icon: L.divIcon({
159
+ html: iconMarker.iconUrl,
160
+ iconAnchor: iconMarker.iconAnchor,
161
+ iconSize: iconMarker.iconSize,
162
+ className: iconMarker.class,
163
+ }),
164
+ draggable: props.configurationMap.dragMarker,
165
+ }).addTo(map);
166
+ markerRender.value = marker;
167
+ marker.on("dragend", (event) => {
168
+ const updatedCoordinates = event.target.getLatLng();
169
+ const lat = updatedCoordinates.lat;
170
+ const lng = updatedCoordinates.lng;
171
+ setCoordinates({
172
+ lat,
173
+ lng,
174
+ });
175
+ });
176
+ }
177
+ };
178
+
179
+ const createSatelliteControl = (): L.Control => {
180
+ const SatelliteControl = L.Control.extend({
181
+ onAdd: (map: L.Map) => {
182
+ const container = L.DomUtil.create(
183
+ "div",
184
+ "leaflet-control-draw leaflet-bar leaflet-control"
185
+ );
186
+
187
+ const satelliteButton = L.DomUtil.create(
188
+ "a",
189
+ "leaflet-draw-draw-satellite",
190
+ container
191
+ );
192
+
193
+ satelliteButton.href = "#";
194
+ satelliteButton.title = isSatelliteView.value
195
+ ? "Vista Normal"
196
+ : "Vista Satélite";
197
+
198
+ // Usar el mismo estilo que los otros botones de draw
199
+ satelliteButton.style.backgroundImage = isSatelliteView.value
200
+ ? `url(${gpsIcon})`
201
+ : `url(${sateliteIcon})`;
202
+ satelliteButton.style.backgroundPosition = "center";
203
+ satelliteButton.style.backgroundRepeat = "no-repeat";
204
+ satelliteButton.style.backgroundSize = "20px 20px";
205
+
206
+ L.DomEvent.disableClickPropagation(satelliteButton);
207
+ L.DomEvent.on(satelliteButton, "click", toggleSatelliteView);
208
+
209
+ return container;
210
+ },
211
+ });
212
+
213
+ return new SatelliteControl({ position: "topleft" });
214
+ };
215
+
216
+ const toggleSatelliteView = (): void => {
217
+ if (!mapRender.value || !currentTileLayer.value) return;
218
+
219
+ isSatelliteView.value = !isSatelliteView.value;
220
+
221
+ // Remover la capa actual
222
+ mapRender.value.removeLayer(currentTileLayer.value as unknown as L.Layer);
223
+
224
+ // Agregar la nueva capa
225
+ if (isSatelliteView.value) {
226
+ currentTileLayer.value = L.tileLayer(
227
+ "https://api.maptiler.com/maps/satellite/{z}/{x}/{y}.jpg?key=t8mWT2ozs1JWBqMZOnZr",
228
+ {
229
+ attribution:
230
+ '&copy; <a href="https://www.maptiler.com/">MapTiler</a> &copy; <a href="http://www.openstreetmap.org/copyright">OpenStreetMap</a>',
231
+ }
232
+ );
233
+ } else {
234
+ currentTileLayer.value = L.tileLayer(
235
+ "https://tile.openstreetmap.org/{z}/{x}/{y}.png",
236
+ {
237
+ attribution:
238
+ '&copy; <a href="http://www.openstreetmap.org/copyright">OpenStreetMap</a>',
239
+ }
240
+ );
241
+ }
242
+
243
+ currentTileLayer.value.addTo(mapRender.value as L.Map);
244
+
245
+ // Actualizar el ícono del control satelital
246
+ const satelliteButtons = document.querySelectorAll(
247
+ ".leaflet-draw-draw-satellite"
248
+ );
249
+ satelliteButtons.forEach((button) => {
250
+ const htmlButton = button as HTMLElement;
251
+ htmlButton.style.backgroundImage = isSatelliteView.value
252
+ ? `url(${gpsIcon})`
253
+ : `url(${sateliteIcon})`;
254
+ htmlButton.title = isSatelliteView.value
255
+ ? "Vista Normal"
256
+ : "Vista Satélite";
257
+ });
258
+ };
259
+
260
+ const searchAddress = async (query: string) => {
261
+ const address = await axios.get(
262
+ location.protocol + "//nominatim.openstreetmap.org/search?",
263
+ {
264
+ params: {
265
+ //query
266
+ q: query,
267
+ limit: 1,
268
+ format: "json",
269
+ },
270
+ }
271
+ );
272
+ if (address.data.length === 1) {
273
+ const lat = parseFloat(address.data[0].lat);
274
+ const lng = parseFloat(address.data[0].lon);
275
+ const coord = { lng, lat };
276
+ setCoordinates({ ...coord, moveMarker: true });
277
+ return address.data[0];
278
+ }
279
+ return address.data;
280
+ };
281
+
282
+ const setCoordinates = ({
283
+ lat,
284
+ lng,
285
+ moveMarker = false,
286
+ }: {
287
+ lat: number;
288
+ lng: number;
289
+ moveMarker?: boolean;
290
+ }) => {
291
+ emit("updated:coordsMarker", {
292
+ lat,
293
+ lng,
294
+ });
295
+ if (moveMarker) markerRender.value?.setLatLng({ lat, lng });
296
+ moveCenterTo({ lat, lng });
297
+ };
298
+
299
+ const moveCenterTo = ({ lat, lng }: { lat: number; lng: number }) => {
300
+ const newCenter = L.latLng(lat, lng);
301
+ mapRender.value.panTo(newCenter, { animate: true, duration: 0.5 });
302
+ };
303
+
304
+ defineExpose({
305
+ searchAddress,
306
+ });
307
+ </script>
308
+
309
+ <style lang="scss"></style>