-
Notifications
You must be signed in to change notification settings - Fork 99
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #66 from alanblack/master
add html/javascript to search addresses
- Loading branch information
Showing
2 changed files
with
64 additions
and
3 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
async function searchAddress(query) { | ||
// Define the Nominatim API endpoint | ||
const api_url = "https://nominatim.openstreetmap.org/search"; | ||
|
||
var result = ["-1", "-1"]; | ||
|
||
// Parameters for the search | ||
const params = { | ||
q: query, | ||
format: "json", | ||
limit: "1" | ||
}; | ||
|
||
// Construct the URL with query parameters | ||
const url = new URL(api_url); | ||
url.search = new URLSearchParams(params); | ||
|
||
await fetch(url) | ||
.then(response => response.json()) | ||
.then(data => { | ||
result = getCoords(data); | ||
}) | ||
.catch(error => { | ||
console.error("Error:", error); | ||
}); | ||
|
||
return result; | ||
} | ||
|
||
function getCoords(data) | ||
{ | ||
if(data.length > 0) | ||
{ | ||
var firstResult = data[0]; | ||
var lat = firstResult.lat; | ||
var long = firstResult.lon; | ||
|
||
return [lat, long]; | ||
} | ||
} |