React Native - Google 反向地理编码在生产中不起作用

问题描述 投票:0回答:1

我正在使用 Google 的地理编码 API 从地址获取纬度/经度。

当我在 Expo 中运行应用程序时,这有效,但当我为 iOS 编译时,请求失败。

这真的很难调试,因为我必须重建应用程序并将所做的每个更改提交给 TestFlight。当我运行

expo run:ios
并在模拟器中运行开发版本时它就可以工作。

这是我的代码

    const fetchCoordinates = async () => {
      try {

        const apiKey = Util.getPlatformAPIKey();
        const googleApiUrl = `https://maps.googleapis.com/maps/api/geocode/json?address=${encodeURIComponent(address)}&key=${apiKey}`;

        const response = await fetch(googleApiUrl, {
          mode: "no-cors",
          redirect: "follow",
          method: "GET",
        });

        if (!response.ok) {
          return;
        } else {
          setStatus("Response from geocoding API ok");\
        }

        const data = await response.json();

        if (data.results.length > 0) {
          const {lat, lng} = data.results[0].geometry.location;
          return {latitude: lat, longitude: lng};
        } else {
          return undefined;
        }
      } catch (error) {
        console.error("Error fetching coordinates:", error);
      }
    };

似乎只是返回未定义。 Google Cloud Console 未显示任何错误。

iOS 是否会阻止对 googleapis.com 的请求?我已将 NSAllowsArbitraryLoads 设置为 true

"NSAppTransportSecurity": {
    "NSAllowsArbitraryLoads": true
}
ios react-native google-maps google-api expo
1个回答
0
投票

问题在于

console.dir
位于 try 块中,这会在生产中引发错误(但不在开发/模拟器中)

© www.soinside.com 2019 - 2024. All rights reserved.