【问题标题】:How to get user city name location by permission and than send that city name location to backend?如何通过许可获取用户城市名称位置并将该城市名称位置发送到后端?
【发布时间】:2023-01-05 23:40:35
【问题描述】:

如何通过许可获取用户城市名称位置并将该城市名称位置发送到后端?我怎样才能在我的代码中做这件事?在 React Native Expo 和 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({})

【问题讨论】:

    标签: javascript node.js reactjs react-native


    【解决方案1】:

    将 expo 模块 Location 与此方法一起使用 Location.reverseGeocodeAsync(location, options)

    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)
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-09-21
      • 1970-01-01
      • 2016-07-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-01-16
      相关资源
      最近更新 更多