-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix test and merge mapbox geocoder function
- Loading branch information
Showing
5 changed files
with
57 additions
and
8 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,50 @@ | ||
import axios from "axios"; | ||
|
||
export async function mapboxGeocode (api_key: string, text: string) { | ||
|
||
const search_request = 'https://api.mapbox.com/search/geocode/v6/forward' | ||
|
||
// console.log("GEOCODE EARTH API: ", import.meta.env.VITE_GEOCODE_TOKEN); | ||
console.log("MAPBOX API TOKEN: ", api_key); | ||
|
||
const res = await axios.get(search_request, //auto_request, | ||
{ | ||
params: { | ||
access_token: api_key, // api_key | ||
q: text // text | ||
} | ||
} | ||
) | ||
|
||
// console.log("GEOCODE EARTH API: ", res); | ||
console.log("MAPBOX API: ", res); | ||
|
||
const found_features = res.data.features | ||
.filter((f: any) => (f.hasOwnProperty("geometry") && f.geometry.type === "Point")); | ||
|
||
console.log("found_features: ", found_features); | ||
|
||
return found_features | ||
.filter((item: any) => { | ||
return item.hasOwnProperty("properties") | ||
&& item["properties"].hasOwnProperty("full_address") | ||
&& item["properties"].hasOwnProperty("context") | ||
&& item["properties"]["context"].hasOwnProperty("country") | ||
&& item["properties"]["context"]["country"].hasOwnProperty("country_code") | ||
&& item["properties"]["context"]["country"]["country_code"] | ||
.match(/(?:US|AS|GU|MP|PR|VI)/) !== null | ||
&& "region" in item["properties"]["context"] | ||
&& "postcode" in item["properties"]["context"]; | ||
}) | ||
.map((item: any) => { | ||
console.log(item); | ||
const properties = item["properties"]; | ||
properties["label"] = item["properties"]["full_address"]; | ||
properties['props'] = JSON.stringify(properties); | ||
|
||
return { | ||
...item, | ||
properties | ||
}; | ||
}); | ||
} |
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
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