fix: geocode locations
This commit is contained in:
@@ -7,31 +7,39 @@ export default function useDirectionsRender() {
|
||||
|
||||
const originCoords = ref(null);
|
||||
const destinationCoords = ref(null);
|
||||
// const polylines = ref([]);
|
||||
|
||||
// const geocodeAddress = async (address) => {
|
||||
// try {
|
||||
// const response = await fetch(
|
||||
// `https://maps.googleapis.com/maps/api/geocode/json?address=${encodeURIComponent(
|
||||
// address
|
||||
// )}&key=${mapKey}`
|
||||
// );
|
||||
// const data = await response.json();
|
||||
// const location = data.results[0].geometry.location;
|
||||
// return location;
|
||||
// } catch (error) {
|
||||
// return null;
|
||||
// }
|
||||
// };
|
||||
|
||||
const geocodeAddress = async (address) => {
|
||||
// Utiliza la API de geocodificación de Google Maps para obtener las coordenadas
|
||||
// const apiKey = 'AIzaSyAJtfvrAKy7vnUSv2nzk4dYQkOs3OP4MMs'; // Reemplaza con tu clave de API
|
||||
try {
|
||||
const response = await fetch(
|
||||
`https://maps.googleapis.com/maps/api/geocode/json?address=${encodeURIComponent(
|
||||
address
|
||||
)}&key=${mapKey}`
|
||||
);
|
||||
const data = await response.json();
|
||||
const location = data.results[0].geometry.location;
|
||||
return location;
|
||||
const geocoder = new google.maps.Geocoder();
|
||||
console.log(address)
|
||||
const data = await geocoder.geocode({ address: address });
|
||||
const latitude = data.results[0].geometry.location.lat();
|
||||
const longitude = data.results[0].geometry.location.lng();
|
||||
return {lng: longitude, lat: latitude};
|
||||
} catch (error) {
|
||||
console.log('Error location', error);
|
||||
return null;
|
||||
}
|
||||
};
|
||||
|
||||
const getDirections = async (originCoords, destinationCoords) => {
|
||||
// const originLatLng = `${originCoords.lat},${originCoords.lng}`;
|
||||
// const destinationLatLng = `${destinationCoords.lat},${destinationCoords.lng}`;
|
||||
try {
|
||||
const directionsService = new google.maps.DirectionsService();
|
||||
// const origin = google.maps.LatLng(originCoords.lat, originCoords.lng)
|
||||
// const destination = google.maps.LatLng(destinationCoords.lat, destinationCoords.lng)
|
||||
const request = {
|
||||
origin: {lat: Number.parseFloat(originCoords.lat), lng: Number.parseFloat(originCoords.lng)},
|
||||
destination: {lat: Number.parseFloat(destinationCoords.lat), lng: Number.parseFloat(destinationCoords.lng)},
|
||||
|
||||
Reference in New Issue
Block a user