Replies: 1 comment
-
Hi, onSearch: ({ currentValue }) => {
const api = `https://maps.googleapis.com/maps/api/geocode/json?address=${encodeURI(currentValue)}&key=YOUR_API_KEY`;
return new Promise((resolve) => {
fetch(api)
.then((response) => response.json())
.then((data) => {
// The results are available here
resolve(data.results);
})
.catch((error) => {
console.error(error);
});
});
}, In the function onResults: ({ currentValue, matches, template }) => {
const regex = new RegExp(currentValue, "gi");
return matches === 0 ? template : `<li>${matches[0].formatted_address.replace(regex, (str) => `<b>${str}</b>`)}</li>`;
}, In this section, we only change a few lines: onSubmit: ({ object }) => {
const { formatted_address, geometry } = object;
const { lat, lng } = geometry.location;
// custom id for marker
const customId = Math.random();
const marker = L.marker([lat, lng], {
title: formatted_address,
id: customId,
});
marker.addTo(map).bindPopup(formatted_address);
... All that code depends on what json is. I relied on the fact that google will always return one element. I have not used google maps for a long time, so I do not know if, by giving, say 'london', google will return a few elements in json. Good luck! |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
I tried to insert the Google API fo searcing address with google data but don't work. With bing autosuggest API same. I have seen that the resoults from these API are in json format, and not in geojson, is maybe this the problem? Thanks for this plugin, is fantastic!!
Beta Was this translation helpful? Give feedback.
All reactions