Dalsi iterace planovace, snad jiz kompletni vrstvy mapy

This commit is contained in:
Michal Kváček
2024-04-17 13:21:32 +02:00
parent cee979fec8
commit de0b75163d
10 changed files with 745 additions and 430 deletions
+22 -19
View File
@@ -1,18 +1,10 @@
import {ref} from "vue";
import axios from "axios";
export const useGeocode = (emits) => {
export const useGeocode = () => {
const center = ref({
lat: 50.1314,
lng: 14.5256
});
const zoom = ref(10);
const center = ref({lat: 50.1314, lng: 14.5256});
const emitModels = (lat, lng) => {
emits("update:gpsLatitude", lat);
emits("update:gpsLongitude", lng);
}
const searchGeocode = (address) => {
return axios.get(`https://maps.googleapis.com/maps/api/geocode/json?address=${encodeURI(address)}&key=${process.env.GOOGLE_MAPS_API_KEY}`).then(response => {
@@ -24,18 +16,29 @@ export const useGeocode = (emits) => {
});
}
const setCoordinatesFromGeocode = (address) => {
// TODO: odstranit a nahradit za searchGeocode! Vlastne cela tato composable by sla nahradit
axios.get(`https://maps.googleapis.com/maps/api/geocode/json?address=${encodeURI(address)}&key=${process.env.GOOGLE_MAPS_API_KEY}`).then(response => {
if (response.data.status !== 'OK') {
emits("notFound", address);
const searchPlaceName = (lat, lng) => {
return axios.get(`https://maps.googleapis.com/maps/api/geocode/json?latlng=${lat},${lng}&key=${process.env.GOOGLE_MAPS_API_KEY}`).then(response => {
if (response.data.status !== 'OK' || !response.data.results?.length) {
return null;
}
center.value = response.data.results[0].geometry.location;
emitModels(center.value.lat, center.value.lng);
zoom.value = 13;
const results = response.data.results;
const components = {};
for (const addressComponent of results[0].address_components) {
for (const namePartType of ["locality", "postal_town", 'administrative_area_level_2']) {
if (addressComponent.types.includes(namePartType)) {
components[namePartType] = addressComponent.long_name
}
}
}
if (!Object.keys(components).length) {
return "Bod na mapě";
}
return Object.values(components).join(", ");
});
}
return {emitModels, setCoordinatesFromGeocode, zoom, center, searchGeocode}
return {center, searchGeocode, searchPlaceName}
}