我有一个 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
}
/>
请帮助我,如果我需要分享更多代码,请告诉我!
显然,我的解决方案是将 package.json 文件中的这行代码:
"@rnmapbox/maps": "^10.0.15",
更改为:"@rnmapbox/maps": "~10.0.15",
。