Created
July 14, 2022 11:42
-
-
Save adielBm/1f9e7992004d6015c9c5847b3ccd8063 to your computer and use it in GitHub Desktop.
get Coordinates By Address
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { writeFile, readFile } from 'node:fs'; | |
import fetch from "node-fetch"; | |
async function getCoordinatesByAddress(address) { | |
const api = `https://maps.googleapis.com/maps/api/geocode/json?address=${address}&key=MY_API_KEY`; | |
const response = await fetch(api); | |
const data = await response.json(); | |
return data; | |
} | |
readFile('./shops.json', 'utf8', (err, shops) => { | |
let shops_with_coordinates = []; | |
JSON.parse(shops).map((shop) => { | |
const coordinates = await getCoordinatesByAddress(encodeURIComponent(shop["address"])) | |
shops_with_coordinates.push({ | |
name: shop["name"], | |
address: shop["address"], | |
coordinates: coordinates, | |
}) | |
}) | |
writeFile('shops_with_coordinates.json', JSON.stringify(shops_with_coordinates), 'utf8', (err) => { | |
console.log('writeFile..') | |
}); | |
}); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment