【发布时间】:2021-05-17 02:44:09
【问题描述】:
我正在编写一个 React/Next.js 应用程序,并且我正在使用三词地址将项目映射到一个位置。我的代码接收三词地址,将其转换为坐标,然后应该在 Mapbox 地图上显示该位置。
const Item = () => {
const location = "daring.lion.race";
const api = require("@what3words/api");
api.setOptions({ key: "my_api_key" });
const data = api.convertToCoordinates(location).then((value) => {
return value;
});
const [coords, setCoords] = useState();
async function getCoords() {
const jason = await data;
console.log(jason);
setCoords(jason);
}
getCoords();
const [viewport, setViewport] = useState({
width: 1000,
height: 400,
latitude: coords.coordinates.lat,
longitude: coords.coordinates.lng,
zoom: 16,
});
return (
<div>
<ReactMapGL
mapStyle="mapbox://styles/mapbox/outdoors-v11"
mapboxApiAccessToken="my_api_key"
{...viewport}
></ReactMapGL>
</div>
);
};
what3words API 返回一个承诺:
{
"country": "GB",
"square": {
"southwest": {
"lng": -0.12552,
"lat": 51.508328
},
"northeast": {
"lng": -0.125477,
"lat": 51.508355
}
},
"nearestPlace": "London",
"coordinates": {
"lng": -0.125499,
"lat": 51.508341
},
"words": "daring.lion.race",
"language": "en",
"map": "https://w3w.co/daring.lion.race"
}
我正在尝试将承诺中的坐标放入视口中,但它不起作用。我正在使用 ReactMapGL (http://visgl.github.io/react-map-gl/) 来显示 Mapbox 地图。
【问题讨论】:
标签: javascript reactjs typescript async-await next.js