-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
100 lines (93 loc) · 3.6 KB
/
index.html
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
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Address Suggestion App</title>
<style>
body {
font-family: Arial, sans-serif;
margin: 20px;
}
#search-container {
width: 300px;
margin: 0 auto;
}
#address-input {
width: 100%;
padding: 10px;
font-size: 16px;
}
#suggestions {
margin-top: 10px;
border: 1px solid #ccc;
max-height: 200px;
overflow-y: auto;
}
.suggestion {
padding: 10px;
cursor: pointer;
}
.suggestion:hover {
background-color: #f5f5f5;
}
</style>
</head>
<body>
<div id="search-container">
<h1>Address Suggestion</h1>
<input type="text" id="address-input" placeholder="Enter an address">
<div id="suggestions"></div>
<div id="details" class="details"></div>
</div>
<script>
// Replace with your Bing Maps API key
const apiKey = 'ApqRNJEYIoFY2HyrnuPdNR5RcR9RNQCfTcNs3iUw8JYVskIYvKNi1ZcS_F45TOeS';
const addressInput = document.getElementById('address-input');
const suggestionsContainer = document.getElementById('suggestions');
const detailsContainer = document.getElementById('details');
// Event listener for address input
addressInput.addEventListener('input', async () => {
const query = addressInput.value;
suggestionsContainer.innerHTML = ''; // Clear previous suggestions
if (query.length >= 3) {
const suggestionEndpoint = `https://dev.virtualearth.net/REST/v1/Autosuggest?query=${query}&key=${apiKey}`;
const response = await fetch(suggestionEndpoint);
const data = await response.json();
const suggestions = data.resourceSets[0].resources[0].value;
console.log(suggestions);
// Display suggestions
suggestions.forEach(suggestion => {
if (suggestion.__type === "Address") {
const formattedAddress = suggestion.address.formattedAddress;
const suggestionItem = document.createElement('div');
suggestionItem.classList.add('suggestion');
suggestionItem.textContent = formattedAddress;
suggestionItem.addEventListener('click', () => {
// Handle the suggestion here
// Display relevant details
displayDetails(suggestion);
});
suggestionsContainer.appendChild(suggestionItem);
}
});
}
});
function displayDetails(suggestion) {
if (suggestion.__type === "Address") {
const address = suggestion.address;
const details = `
<h2>Details</h2>
<p>Country/Region: ${address.countryRegion}</p>
<p>Locality: ${address.locality}</p>
<p>Admin District: ${address.adminDistrict}</p>
<p>Postal Code: ${address.postalCode}</p>
<p>Street Address: ${address.addressLine}</p>
<p>Street Name: ${address.streetName}</p>
<p>Formatted Address: ${address.formattedAddress}</p>
`;
detailsContainer.innerHTML = details;
}
}
</script>
</body>
</html>