NodeJS 如何通过权限获取用户所在城市名称位置,然后将该城市名称位置发送到后台?

2uluyalo  于 2023-01-08  发布在  Node.js
关注(0)|答案(1)|浏览(170)

如何获得用户的城市名称位置的权限,然后发送到后端的城市名称位置?我怎么能在我的代码做这件事?在react本地博览会和Nodejs
后端:

router.post('/updateCity', (req, res) => {
  const city = req.body.city;
  const username = req.body.username;

  User.findOneAndUpdate({ username: username }, { city: city }, (err, user) => {
    if (err) {
      return res.status(500).json({
        error: err
      });
    }
    res.status(200).json({
      message: 'City updated successfully'
    });
  });
});

前端:

import { StyleSheet, Text, View } from 'react-native'
import React from 'react'

const App = () => {
  return (
    <View>
      <Text>App</Text>
    </View>
  )
}

export default App

const styles = StyleSheet.create({})
bqjvbblv

bqjvbblv1#

使用expo模块Location和此方法Location.reverseGeocodeAsync(位置,选项)

location = await Location.getCurrentPositionAsync({ accuracy: 1 });
if (location) {
  const geocodedAddress = await Location.reverseGeocodeAsync(
    {
      latitude: location.coords.latitude,
      longitude: location.coords.longitude,
    }
  );
  console.log(geocodedAddress.city)
}

相关问题