Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion src/controllers/mapsController.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// const fetch = require('node-fetch')
const getAutoCompletion = require('../utils/getAutoCompletion')
const getInformationPlace = require('../utils/gtInformationPlace')

const autocomplete = async (req, res) => {
const { input } = req.body
Expand All @@ -12,4 +13,16 @@ const autocomplete = async (req, res) => {
}
}

module.exports = { autocomplete }
const dataCoordinate = async (req, res) => {
const { lat, lon } = req.body

getInformationPlace(lat, lon)
.then((information) => {
res.send(information)
})
.catch((error) => {
console.error('Error:', error)
})
}

module.exports = { autocomplete, dataCoordinate }
1 change: 1 addition & 0 deletions src/utils/getAutoCompletion.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ async function getAutoCompletion (consulta) {
// Verificar si la solicitud fue exitosa
if (respuesta.ok) {
const opciones = await respuesta.json()
console.log(opciones)

// Filtrar las primeras 10 opciones
const opcionesAutocompletado = opciones.slice(0, 10).map(opcion => {
Expand Down
24 changes: 24 additions & 0 deletions src/utils/getInformationPlace.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
const fetch = require('node-fetch')

async function getInformationPlace (lat, lon) {
const url = `https://nominatim.openstreetmap.org/reverse?format=json&lat=${lat}&lon=${lon}`

try {
const answer = await fetch(url)
const data = await answer.json()
// Comprueba si se obtuvo una respuesta exitosa
if (answer.ok) {
const placeId = data.place_id
const place = data.display_name
const address = data.address
return { placeId, place, address }
} else {
throw new Error(data.error || 'Could not get location information')
}
} catch (error) {
throw new Error(`
Failed to make the request: ${error.message}`)
}
}

module.exports = getInformationPlace
1 change: 1 addition & 0 deletions src/v1/routes/mapsRouter.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@ const mapsController = require('../../controllers/mapsController')
const mapsRouter = express.Router()

mapsRouter.route('/autocomplete').post(mapsController.autocomplete)
mapsRouter.route('/data-coordinates').post(mapsController.dataCoordinate)

module.exports = mapsRouter