首页 > 解决方案 > 如何使用 MapBox 获取单个坐标数组?

问题描述

我有 55 个国家/地区的数组:

countries = ['Albania', 'Turkey', .....]

我正在寻找以下形式的单个数组:

letlocations = [ ["Albania", 11.8166, 122.0942], ["Turkey", 11.9804, 121.9189], ..... ];

目前,我只能使用此代码使用 1 个位置

const changeMapCenter = async () => {
    const searchLocation = ['Madrid', 'London', 'Tokyo', 'Mexico City', 'Chicago', 'Munich', 'Machu Pichu']
    let item = searchLocation[Math.floor(Math.random() * searchLocation.length)];
    const searchURL = `${MAPBOX_PLACES_API}${item}${REST_PLACES_URL}`
    const response = await fetch(searchURL)
    const data = await response.json()
    const result = data
    const { coordinates } = result.features[0].geometry
    const features = { coordinates }
    const latlong = [...coordinates].reverse()
    mymap.flyTo(latlong)
    L.marker(latlong).bindPopup(`hola estás en ${item}`).addTo(mymap)
}

我的问题是如何同时绘制多个国家?

谢谢你的帮助!

标签: javascriptleafletfetchdata-visualizationmapbox

解决方案


推荐阅读