|
@@ -1,7 +1,15 @@
|
|
|
import axios from 'axios';
|
|
import axios from 'axios';
|
|
|
|
|
+import { Notify } from 'quasar';
|
|
|
|
|
|
|
|
const GEOCODING_URL = 'https://maps.googleapis.com/maps/api/geocode/json';
|
|
const GEOCODING_URL = 'https://maps.googleapis.com/maps/api/geocode/json';
|
|
|
|
|
|
|
|
|
|
+// ── DEBUG via notificação ─────────────────────────────────────────
|
|
|
|
|
+const dbg = (title, data = {}) => {
|
|
|
|
|
+ const lines = Object.entries(data).map(([k, v]) => `${k}: ${JSON.stringify(v)}`).join('\n');
|
|
|
|
|
+ Notify.create({ color: 'teal-9', textColor: 'white', position: 'top', message: `[GEO] ${title}`, caption: lines || undefined, timeout: 8000, multiLine: true });
|
|
|
|
|
+};
|
|
|
|
|
+// ─────────────────────────────────────────────────────────────────
|
|
|
|
|
+
|
|
|
const parseAddressComponents = (components) => {
|
|
const parseAddressComponents = (components) => {
|
|
|
const get = (type, nameType = 'long_name') =>
|
|
const get = (type, nameType = 'long_name') =>
|
|
|
components.find((c) => c.types.includes(type))?.[nameType] ?? '';
|
|
components.find((c) => c.types.includes(type))?.[nameType] ?? '';
|
|
@@ -25,9 +33,14 @@ const geocodeViaGoogleMaps = async (request) => {
|
|
|
export const useGeocodingApi = () => {
|
|
export const useGeocodingApi = () => {
|
|
|
const geocodeCep = async (cep) => {
|
|
const geocodeCep = async (cep) => {
|
|
|
const cleaned = cep.replace(/\D/g, '');
|
|
const cleaned = cep.replace(/\D/g, '');
|
|
|
|
|
+ const apiKey = process.env.GOOGLE_MAPS_API_KEY;
|
|
|
|
|
+ const usandoGeocoderNativo = !!window.google?.maps?.Geocoder;
|
|
|
|
|
+
|
|
|
|
|
+ dbg('geocodeCep', { cep: cleaned, via: usandoGeocoderNativo ? 'Geocoder nativo' : 'HTTP', apiKey: apiKey ? `${apiKey.substring(0, 8)}...` : 'UNDEFINED' });
|
|
|
|
|
|
|
|
- if (window.google?.maps?.Geocoder) {
|
|
|
|
|
|
|
+ if (usandoGeocoderNativo) {
|
|
|
const results = await geocodeViaGoogleMaps({ address: `${cleaned}, Brazil` });
|
|
const results = await geocodeViaGoogleMaps({ address: `${cleaned}, Brazil` });
|
|
|
|
|
+ dbg('geocodeCep nativo result', { total: results.length, encontrou: results.length > 0 });
|
|
|
if (!results.length) return null;
|
|
if (!results.length) return null;
|
|
|
const result = results[0];
|
|
const result = results[0];
|
|
|
const parsed = parseAddressComponents(result.address_components);
|
|
const parsed = parseAddressComponents(result.address_components);
|
|
@@ -39,8 +52,9 @@ export const useGeocodingApi = () => {
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
const { data } = await axios.get(GEOCODING_URL, {
|
|
const { data } = await axios.get(GEOCODING_URL, {
|
|
|
- params: { address: `${cleaned}, Brazil`, key: process.env.GOOGLE_MAPS_API_KEY },
|
|
|
|
|
|
|
+ params: { address: `${cleaned}, Brazil`, key: apiKey },
|
|
|
});
|
|
});
|
|
|
|
|
+ dbg('geocodeCep HTTP result', { status: data.status, total: data.results?.length ?? 0 });
|
|
|
if (data.status !== 'OK' || !data.results.length) return null;
|
|
if (data.status !== 'OK' || !data.results.length) return null;
|
|
|
const result = data.results[0];
|
|
const result = data.results[0];
|
|
|
const { lat, lng } = result.geometry.location;
|
|
const { lat, lng } = result.geometry.location;
|
|
@@ -48,15 +62,22 @@ export const useGeocodingApi = () => {
|
|
|
};
|
|
};
|
|
|
|
|
|
|
|
const reverseGeocode = async (lat, lng) => {
|
|
const reverseGeocode = async (lat, lng) => {
|
|
|
- if (window.google?.maps?.Geocoder) {
|
|
|
|
|
|
|
+ const apiKey = process.env.GOOGLE_MAPS_API_KEY;
|
|
|
|
|
+ const usandoGeocoderNativo = !!window.google?.maps?.Geocoder;
|
|
|
|
|
+
|
|
|
|
|
+ dbg('reverseGeocode chamado', { lat, lng, via: usandoGeocoderNativo ? 'Geocoder nativo' : 'HTTP', apiKey: apiKey ? `${apiKey.substring(0, 8)}...` : 'UNDEFINED' });
|
|
|
|
|
+
|
|
|
|
|
+ if (usandoGeocoderNativo) {
|
|
|
const results = await geocodeViaGoogleMaps({ location: { lat, lng } });
|
|
const results = await geocodeViaGoogleMaps({ location: { lat, lng } });
|
|
|
|
|
+ dbg('reverseGeocode nativo result', { total: results.length });
|
|
|
if (!results.length) return null;
|
|
if (!results.length) return null;
|
|
|
return { lat, lng, ...parseAddressComponents(results[0].address_components) };
|
|
return { lat, lng, ...parseAddressComponents(results[0].address_components) };
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
const { data } = await axios.get(GEOCODING_URL, {
|
|
const { data } = await axios.get(GEOCODING_URL, {
|
|
|
- params: { latlng: `${lat},${lng}`, key: process.env.GOOGLE_MAPS_API_KEY },
|
|
|
|
|
|
|
+ params: { latlng: `${lat},${lng}`, key: apiKey },
|
|
|
});
|
|
});
|
|
|
|
|
+ dbg('reverseGeocode HTTP result', { status: data.status, total: data.results?.length ?? 0, errorMessage: data.error_message ?? null });
|
|
|
if (data.status !== 'OK' || !data.results.length) return null;
|
|
if (data.status !== 'OK' || !data.results.length) return null;
|
|
|
const result = data.results[0];
|
|
const result = data.results[0];
|
|
|
return { lat, lng, ...parseAddressComponents(result.address_components) };
|
|
return { lat, lng, ...parseAddressComponents(result.address_components) };
|
|
@@ -65,9 +86,14 @@ export const useGeocodingApi = () => {
|
|
|
const geocodeFullAddress = async ({ address, number, district, zip_code, city, state }) => {
|
|
const geocodeFullAddress = async ({ address, number, district, zip_code, city, state }) => {
|
|
|
const parts = [address, number, district, zip_code, city, state, 'Brazil'].filter(Boolean);
|
|
const parts = [address, number, district, zip_code, city, state, 'Brazil'].filter(Boolean);
|
|
|
const query = parts.join(', ');
|
|
const query = parts.join(', ');
|
|
|
|
|
+ const apiKey = process.env.GOOGLE_MAPS_API_KEY;
|
|
|
|
|
+ const usandoGeocoderNativo = !!window.google?.maps?.Geocoder;
|
|
|
|
|
+
|
|
|
|
|
+ dbg('geocodeFullAddress', { query, via: usandoGeocoderNativo ? 'Geocoder nativo' : 'HTTP', apiKey: apiKey ? `${apiKey.substring(0, 8)}...` : 'UNDEFINED' });
|
|
|
|
|
|
|
|
- if (window.google?.maps?.Geocoder) {
|
|
|
|
|
|
|
+ if (usandoGeocoderNativo) {
|
|
|
const results = await geocodeViaGoogleMaps({ address: query });
|
|
const results = await geocodeViaGoogleMaps({ address: query });
|
|
|
|
|
+ dbg('geocodeFullAddress nativo result', { total: results.length });
|
|
|
if (!results.length) return null;
|
|
if (!results.length) return null;
|
|
|
const result = results[0];
|
|
const result = results[0];
|
|
|
return {
|
|
return {
|
|
@@ -78,8 +104,9 @@ export const useGeocodingApi = () => {
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
const { data } = await axios.get(GEOCODING_URL, {
|
|
const { data } = await axios.get(GEOCODING_URL, {
|
|
|
- params: { address: query, key: process.env.GOOGLE_MAPS_API_KEY },
|
|
|
|
|
|
|
+ params: { address: query, key: apiKey },
|
|
|
});
|
|
});
|
|
|
|
|
+ dbg('geocodeFullAddress HTTP result', { status: data.status, total: data.results?.length ?? 0, errorMessage: data.error_message ?? null });
|
|
|
if (data.status !== 'OK' || !data.results.length) return null;
|
|
if (data.status !== 'OK' || !data.results.length) return null;
|
|
|
const result = data.results[0];
|
|
const result = data.results[0];
|
|
|
const { lat, lng } = result.geometry.location;
|
|
const { lat, lng } = result.geometry.location;
|