有没有办法在移动设备上找到客户的位置?例如,如果用户打开应用程序,我如何才能找到他们的大致位置?

例如,如果一个用户来自旧金山CA,它将有一些类型标识符,让我知道用户来自旧金山CA.我不需要他们的确切位置,只是县或一般地区的原产地.

我有个推荐人:This SO Question

推荐答案

当然基于该线程中的一个答案,您可以像这样在React Native中实现-

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

export default class Test extends Component {
  constructor(props) {
    super(props);
    this.state = {
      countryName: '',
      regionName: ''
    };
  }

  componentDidMount() {
    var url = 'https://freegeoip.net/json/';
    fetch(url)
      .then((response) => response.json())
      .then((responseJson) => {
        //console.log(responseJson);
        this.setState({
          countryName: responseJson.country_name,
          regionName: responseJson.region_name
        });
      })
      .catch((error) => {
       //console.error(error);
      });
  }

  render() {
    return (
      <View style={styles.container}>
        <Text>Country: {this.state.countryName}</Text>
        <Text>Region: {this.state.regionName}</Text>
      </View>
    );
  }
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    justifyContent: 'center',
    alignItems: 'center',
    backgroundColor: '#F5FCFF',
  },
});

AppRegistry.registerComponent('Test', () => Test);

React-native相关问答推荐

ReactNative如何在填充编辑表单 timeshift 除按钮禁用状态

在 React Native 中使用 Flatlist scrollView 时的模式关闭

应用管理

找不到变量:firebase/react native(expo)上的 IDBIndex

如果没有启用远程调试器,React Native 代码将无法工作

react-native-snap-carousel 无法正确渲染

react native条件渲染

如何在 React-Native 中创建检测自动位置的map

给定 APK 文件,我如何检测应用程序是否使用 React Native?

React Native 错误:Animated.event now requires a second argument for options

如何实时从 react-native-camera 获取帧

为了回调 URL 的目的,在 Expo 中运行的 React Native 元素的 info.plist 在哪里?

React-Native:save user preferences

如何在react native中过滤对象数组?

如何在 React-Native 的alert中设置alert框标题?

在 React Native App 中禁用屏幕捕获/ScreenShot

React Native require(image) 返回数字

使用来自另一个页面的 fetch 调用函数返回结果值

React Native Image 不显示,我该如何解决?

占位符文本在(多行)TextInput for Android(React Native)中的位置