Everything you can do from the Geo API Gouv adresse endpoint.
This library is a wrapper for using the Data Gouv API Adresse endpoint.
It offers no more features than what is possible with the endpoint, and the options name are the exact same.
I created the library because I have only seen one package, that was not tested, and not up to date.
If you use it via the CDN link, you need to use axios alongside the library (check the examples).
If you use it on Node.JS, you already have NPM and (Node or Yarn) installed in your machine, and axios will be pulled at the installation.
In the <body>
of your HTML document, add these CDNs links (don't add axios if it is already present).
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Some website</title>
</head>
<body>
<!-- ... -->
<script
type="text/javascript"
src="https://unpkg.com/axios@0.20.0/dist/axios.min.js"
></script>
<script
type="text/javascript"
src="https://unpkg.com/geo-api-gouv-adresse@0.1.0/dist/index.min.js"
></script>
<script type="text/javascript">
// Call the library here...
</script>
</body>
</html>
In your root folder, open a terminal and install the library:
npm install --save-dev geo-api-gouv-adresse
For a complete list of options, see the online documentation.
In this example, we will use a partially complete address, and get the results for this address.
geoApiGouvAdresse
.search("8 bd du port")
.then(function (response) {
response.forEach(function (address) {
console.log(
"found a matching street name:",
address.properties.name
);
});
})
.catch(function (error) {
console.log("an error occured");
});
import { search } from "geo-api-gouv-adresse";
// or
// const { search } = require("geo-api-gouv-adresse");
const main = async () => {
let results = {};
try {
results = await search("8 bd du");
} catch (exception) {
console.error("an error occured");
return;
}
for (const address of results.features) {
console.log(`found a matching street name: ${address.properties.name}`);
}
};
main();
For a detail on how the response is composed, see the official Geo API gouv adresse website, and the Geocode JSON spec.
In this example, we will limit the number of results for the street we are searching for.
geoApiGouvAdresse
.search("8 bd du port", { limit: 10 })
.then(function (response) {
response.forEach(function (address) {
console.log(
"found a matching street name:",
address.properties.name
);
});
})
.catch(function (error) {
console.log("an error occured");
});
import { search } from "geo-api-gouv-adresse";
// or
// const { search } = require("geo-api-gouv-adresse");
const main = async () => {
let response = {};
try {
response = await search("8 bd du", {
limit: 10,
});
} catch (exception) {
console.error("an error occured");
return;
}
for (const address of response.features) {
console.log(`found a matching street name: ${address.properties.name}`);
}
};
main();