Get country / city without geographic location React-native API?

Is there a way to find the location of clients on a mobile device? For example, if a user opens an application, how can I find out what their approximate location is?

For example, if a user came from San Francisco, he would have a certain type of identifier to inform me that the user came from San Francisco. I would not need their exact location only in the county or common area of ​​origin.

Well, I had a link: This SO Question . Can we somehow implement the same in React-native?

+5
source share
1 answer

Sure. Based on one of the answers in this thread, you can implement in React Native like this -

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); 
+25
source

Source: https://habr.com/ru/post/1265590/


All Articles