我想把初始区域的经度和纬度的值,上面得到的经度和纬度,但我一直得到错误。我该怎么办?我不知道怎么做。
"纬度:37岁经度:127年,"这部分总是出错。
我做
const[洛杉矶,setLa] = useState("加载…")setLa(纬度)…
initialRegion = {{纬度:数量(la),
然后错误说纬度:& lt; & lt;"Nan"在祝辞. .
我认为纬度是字符串。
如何将字符串转换为数字?
import { StatusBar } from "expo-status-bar";
import React from "react";
import * as Location from "expo-location";
import { View, Text, StyleSheet, ScrollView, Dimensions} from "react-native";
import { backgroundColor } from "react-native/Libraries/Components/View/ReactNativeStyleAttributes";
import { useEffect, useState } from "react";
import MapView, { PROVIDER_GOOGLE, Marker }from "react-native-maps";
const { width: SCREEN_WIDTH } = Dimensions.get("window");
export default function App() {
const [city, setCity] = useState("Loading...");
const [la,setLa]=useState("Loading...");
const [location, setLocation] = useState(null);
const [ok, setOk] = useState(true);
const ask = async () => {
const { granted } = await Location.requestForegroundPermissionsAsync();
if (!granted) {
setOk(false);
}
const {
coords: { latitude, longitude },
} = await Location.getCurrentPositionAsync({ accuracy: 5 });
const location = await Location.reverseGeocodeAsync(
{ latitude, longitude },
{ useGoogleMaps: false }
);
setCity(location[0].district)
};
useEffect(() => {
ask();
}, []);
return (
<>
<View style={{flex:0.16,backgroundColor:'white'}}>
<Text style={{fontSize:40,marginTop:55,marginLeft:30}}>{city}</Text>
</View>
<View style={{ flex: 1 }}>
<MapView
style={{ flex: 1 }}
provider={PROVIDER_GOOGLE}
initialRegion={{
latitude:37,
longitude: 127,
latitudeDelta: 0.00922,
longitudeDelta: 0.00321,
}}>
</MapView>
</View>
<StatusBar style="black" />
</>
);
}
const styles = StyleSheet.create({
}
);
当location不为空时显示MapView
<View style={{ flex: 1 }}>
yourlatitudelongitudestate && <MapView
style={{ flex: 1, marginTop: 40 }}
initialRegion={{
latitude: latitude,
longitude: longitude ,
latitudeDelta: 0.00922,
longitudeDelta: 0.00321,
}}
/>
</View>