如何在react native中的输入字段中获取地址输入的经度和纬度



我正在显示谷歌地图。在两个坐标之间,一个是原点,另一个是目的地。我想将输入值转换为经度和纬度。目前我设置了静态值,但我需要获得输入文本中Inter值的经度和纬度

const [coordinates] = useState([
{
latitude: 51.5115,
longitude: 10.1160,
},
{
latitude:     51.521515,
longitude: -0.127636,
},
]);
return(
<View>

<MapView

style={styles.map}
initialRegion={{
latitude: coordinates[0].latitude,
longitude: coordinates[0].longitude,
latitudeDelta: 2.0922,
longitudeDelta: 2.0421,
}}>
<MapViewDirections
origin={coordinates[0]}
destination={coordinates[1]}
apikey={"Google Api key"} 
strokeWidth={4}
optimizeWaypoints={true}
strokeColor="red"
/>
<Marker coordinate={coordinates[0]} />
<Marker coordinate={coordinates[1]} />
</MapView>
<TextInput placeholder = "Enter Orgin " />
<TextInput   placeholder = "Enter Destination"  />  
</View>
)

};

您可以使用Expo访问位置,如下代码片段所示。

import * as Location from "expo-location";

export const hasLocationServiceEnable = async () =>
await Location.hasServicesEnabledAsync();
const getUserLocation = async () => {

let result = null;
const isLocationAccessEnabled = await hasLocationServiceEnable();
/** Discard request coordinates when user location disabled. This required to avoid
* continuously request location in contrary to the user preference
*/

if (!isLocationAccessEnabled) {
return result;
} else {
// Request location access permission 
let { status } = await Location.requestForegroundPermissionsAsync();

if (status !== "granted") {

return result;
} else {
try {
let { coords } = await Location.getCurrentPositionAsync({});

result = coords || null;
} catch (error) {
console.error(error)

}
return result;
}
}
};
export default getUserLocation;


最新更新