Created
February 7, 2025 15:44
-
-
Save xtetsuji/69d6379e7a5d2a7448cfd4c0e5e240f4 to your computer and use it in GitHub Desktop.
Mapbox 提供 API のうちフォワードジオコーディングを行う API の比較。住所文字列を第1引数に渡すことを想定
This file contains hidden or 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
const MAPBOX_ACCESS_TOKEN = 'ご自身の Mapbox API Key' | |
// Mapbox フォワードジオコーディング API 各種の結果を比較 | |
// Search v1 API | |
// https://docs.mapbox.com/api/search/search/ | |
async function mapboxSearchV1ForwardGeocoding(address) { | |
const url = `https://api.mapbox.com/search/v1/forward/${encodeURIComponent(address)}?access_token=${MAPBOX_ACCESS_TOKEN}&language=ja&country=JP&limit=1` | |
const res = await fetch(url) | |
const json = await res.json() | |
const { ok, status } = res | |
console.log('==================== mapboxSearchV1ForwardGeocoding ====================') | |
console.dir({ | |
ok, | |
status, | |
json, | |
}, { depth: null, colors: true }) | |
// 緯度経度を抽出 | |
const coordinates = json.features[0].geometry.coordinates | |
console.log(`経緯度は [${coordinates[1]}, ${coordinates[0]}] です`) | |
} | |
// Geocoding v6 API | |
// https://docs.mapbox.com/api/search/geocoding/ | |
async function mapboxGeocodingV6ForwardGecoding(address) { | |
// https://docs.mapbox.com/api/search/geocoding#forward-geocoding-with-search-text-input | |
const url = `https://api.mapbox.com/search/geocode/v6/forward?q=${address}&access_token=${MAPBOX_ACCESS_TOKEN}&language=ja&country=jp&limit=1` | |
const res = await fetch(url) | |
const json = await res.json() | |
const { ok, status } = res | |
console.log('==================== mapboxGeocodingV6ForwardGecoding ====================') | |
console.dir({ | |
ok, | |
status, | |
json, | |
}, { depth: null, colors: true }) | |
// 緯度経度を抽出 | |
const coordinates = json.features[0].geometry.coordinates | |
console.log(`経緯度は [${coordinates[1]}, ${coordinates[0]}] です`) | |
} | |
// Seach Box API | |
// https://docs.mapbox.com/api/search/search-box/ | |
async function mapboxSearchBoxForwardGeocoding(address) { | |
const url = `https://api.mapbox.com/search/searchbox/v1/forward?q=${address}&access_token=${MAPBOX_ACCESS_TOKEN}&language=ja&country=jp&limit=1` | |
const res = await fetch(url) | |
const json = await res.json() | |
const { ok, status } = res | |
console.log('==================== mapboxSearchBoxForwardGeocoding ====================') | |
console.dir({ | |
ok, | |
status, | |
json, | |
}, { depth: null, colors: true }) | |
// 緯度経度を抽出 | |
const coordinates = json.features[0].geometry.coordinates | |
console.log(`経緯度は [${coordinates[1]}, ${coordinates[0]}] です`) | |
} | |
async function main() { | |
const address = process.argv[2] | |
if (!address) { | |
console.error('住所を引数に指定してください') | |
process.exit(1) | |
} | |
await mapboxSearchV1ForwardGeocoding(address) | |
await mapboxGeocodingV6ForwardGecoding(address) | |
await mapboxSearchBoxForwardGeocoding(address) | |
} | |
main().then( | |
() => { console.log('done') } | |
).catch(console.error) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment