React Native 应用程序在 npm 更新后在地图上显示不正确的起始位置

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

我有一个 React Native 应用程序,它显示以马尔默为起点的地图,以及一个在 Android 模拟器上显示用户当前位置的按钮。 运行 npm update 后,应用程序将华盛顿特区显示为起点,并且按钮不执行任何操作。 代码与 npm 更新之前相同。我已经验证了位置权限是正确的,因为每当我按下按钮时,我都会在终端中打印出:“您可以使用该位置”。 npm 更新后,什么会导致应用程序显示不正确的地图和按钮功能?

import MapboxGL from "@rnmapbox/maps"
import { PermissionsAndroid } from "react-native"
import Geolocation from "@react-native-community/geolocation"

 const [coordinates, setCoordinates] = useState([
    13.016573571131062, 55.597274504505585,
  ])
 
 const [currentLocation, setCurrentLocation] = useState(null)

const Permission = async () => {
    try {
      const granted = await PermissionsAndroid.request(
        PermissionsAndroid.PERMISSIONS.ACCESS_FINE_LOCATION,
        {
          title: "Ploteye App Location Permission",
          message: "Ploteye  needs access to your location ",
          buttonNeutral: "Ask Me Later",
          buttonNegative: "Cancel",
          buttonPositive: "OK",
        }
      )
      if (granted === PermissionsAndroid.RESULTS.GRANTED) {
        console.log("You can use the location")
        getCurrentLocation()
      } else {
        console.log("Location permission denied")
      }
    } catch (error) {
      console.log("error:", error)
    }
  }

  const getCurrentLocation = () => {
    Geolocation.getCurrentPosition(
      (position) => {
        const { latitude, longitude } = position.coords
        setCurrentLocation({ latitude, longitude })
        console.log("INSIDE getCURRENTLOCATION: ", latitude, longitude)
      },
      (error) => alert("Error", error.message),
      { enableHighAccuracy: true, maximumAge: 1000 }
    )
  }

<MapboxGL.Camera
            zoomLevel={currentLocation ? 18 : zoomLevel} 
            centerCoordinate={
              currentLocation
                ? [currentLocation.longitude, currentLocation.latitude]
                : coordinates
            } 
          />

请帮助我,如果我需要分享更多代码,请告诉我!

react-native geolocation android-emulator mapbox npm-update
1个回答
0
投票

显然,我的解决方案是将 package.json 文件中的这行代码:

"@rnmapbox/maps": "^10.0.15",
更改为:
"@rnmapbox/maps": "~10.0.15",

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