-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathphoton_geocode.js
147 lines (120 loc) · 3.46 KB
/
photon_geocode.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
const addressInput = document.getElementById('addressInput');
const searchResults = document.getElementById('searchResults');
let marker = null;
let resultsVisible = false;
async function geocodeAddress(address) {
const photonUrl = `https://photon.komoot.io/api/?q=${encodeURIComponent(address)}`;
try {
const response = await fetch(photonUrl);
const data = await response.json();
if (data && data.features && data.features.length > 0) {
const result = data.features[0];
const coords = result.geometry.coordinates;
updateMapAndMarker(coords);
clearSearchResults();
} else {
alert('No results found for this address.');
}
} catch (error) {
console.error('Error during geocoding', error);
alert('Error during geocoding.');
}
}
function updateMapAndMarker(coords) {
map.flyTo({
center: coords,
zoom: 15,
});
if (marker) {
marker.remove();
}
marker = new maplibregl.Marker()
.setLngLat(coords)
.addTo(map);
}
async function fetchSuggestions(address) {
if (!address) {
clearSearchResults();
return;
}
const photonUrl = `https://photon.komoot.io/api/?q=${encodeURIComponent(address)}&limit=5`;
try {
const response = await fetch(photonUrl);
const data = await response.json();
displaySuggestions(data.features);
} catch (error) {
console.error('Error fetching suggestions', error);
}
}
function displaySuggestions(results) {
clearSearchResults();
if (!results || results.length === 0) {
resultsVisible = false;
searchResults.style.display = 'none';
return;
}
results.forEach(result => {
const props = result.properties;
let addressParts = [];
if (props.name) {
addressParts.push(props.name);
}
if (props.street) {
addressParts.push(props.street);
}
if (props.housenumber) {
addressParts.push(props.housenumber)
}
if (props.postcode && props.city) {
addressParts.push(props.postcode + ' ' + props.city)
}
const addressString = addressParts.join(', ');
const listItem = document.createElement('li');
listItem.textContent = addressString;
listItem.addEventListener('click', () => {
const coords = result.geometry.coordinates;
addressInput.value = listItem.textContent;
updateMapAndMarker(coords);
clearSearchResults();
});
searchResults.appendChild(listItem);
});
resultsVisible = true;
searchResults.style.display = 'block';
}
function clearSearchResults() {
searchResults.innerHTML = '';
searchResults.style.display = 'none';
resultsVisible = false;
}
addressInput.addEventListener('keydown', (event) => {
if (event.key === 'Enter') {
event.preventDefault(); // Prevent form submission
if (searchResults.children.length > 0) {
const firstResult = searchResults.children[0];
firstResult.click();
} else {
const address = addressInput.value;
if (address) {
geocodeAddress(address);
} else {
alert('Please enter an address.');
}
}
}
});
addressInput.addEventListener('input', () => {
const address = addressInput.value;
fetchSuggestions(address);
});
addressInput.addEventListener('focus', () => {
if (resultsVisible === false && addressInput.value) {
fetchSuggestions(addressInput.value)
} else if (resultsVisible === false) {
clearSearchResults()
}
});
// Event listener to close search results on map click
map.on('click', () => {
clearSearchResults();
});