Mapbox getDirections错误:值必须是一个对象

时间:2018-11-25 16:16:01

标签: react-native mapbox directions

我正在尝试在React Native应用中的地图上添加路线。

我已遵循本教程:https://blog.mapbox.com/adding-directions-to-the-react-native-store-locator-kit-ade759bd824e

问题是,当我用mbDirections.getDirections请求路线时,我收到“错误:值必须是一个对象”。有谁知道接收和存储指示的正确方法?

谢谢!

这就是我要尝试的:

const Directions = require('@mapbox/mapbox-sdk/services/directions');
const mbDirections = Directions({ accessToken: 'my token' });

async fetchDirections (origin, destination) {

const originLatLng = {
  latitude: origin[1],
  longitude: origin[0],
};

const destLatLng = {
  latitude: destination[1],
  longitude: destination[0],
};

const requestOptions = {
  geometry: 'polyline',
};

let res = null;
try {
  res = await mbDirections.getDirections([
    originLatLng,
    destLatLng,
  ], requestOptions);
} catch (e) {
  console.log(e);
}

if (res !== null) {
  const directions = res.entity.routes[0];
  this.setState({ directions: directions });
}
}

1 个答案:

答案 0 :(得分:0)

嗨,尝试一下,让我知道

try {
  res = await mbDirections.getDirections([
      { latitude: origin[1], longitude: origin[0]},
      { latitude: destination[1], longitude: destination[0]},
    ], {  geometry: 'polyline'});
} catch (e) {
  console.log(e);
}