可能的未处理的承诺拒绝(id:1)Mapbox本机反应

时间:2020-05-05 01:28:04

标签: react-native mapbox

我正在尝试获取Mapbox上的当前位置,但是在方法返回时却得到了未处理的诺言(TypeError:null不是对象)。我相信这是由于尝试在iOS用户允许权限之前获取信息而引起的,但不确定如何使功能等待用户权限。它在const currentCoords = [location.coords.longitude, location.coords.latitude]

处失败
return (
      <View style={styles.page}>
        <View style={styles.container}>
          <mapbox.MapView style={styles.map} />
          <mapbox.UserLocation
            onUpdate={(location) => {
              const currentCoords = [
                location.coords.longitude,
                location.coords.latitude,
              ];
            }}
          />
        </View>
      </View>
    );

1 个答案:

答案 0 :(得分:1)

您可能会因为null is not an objectlocation为空而得到location.coords

您无需添加等待权限,只需添加一个空检查以确保返回的值不为空。

  <mapbox.UserLocation
    onUpdate={(location) => {
      if(!location || !location.coords) return;

      const currentCoords = [
        location.coords.longitude,
        location.coords.latitude,
      ];
    }}
  />