【问题标题】:How to get current location using react-native-maps如何使用 react-native-maps 获取当前位置
【发布时间】:2018-05-13 11:56:35
【问题描述】:

我正在尝试使用 react-native 地图在 android 设备上的当前用户地理位置呈现地图。

这是我目前得到的:

import React, { Component } from 'react';

import {
  View,
  StyleSheet,
  Dimensions,
} from 'react-native';

import MapView from 'react-native-maps';

const {width, height} = Dimensions.get('window')

const SCREEN_HEIGHT = height
const SCREEN_WIDTH = width
const ASPECT_RATIO = width / height
const LATITUDE_DELTA = 0.0922
const LONGITUDE_DELTA = LATITUDE_DELTA * ASPECT_RATIO

class MapComponent extends Component {
  constructor() {
    super()
    this.state = {
      initialPosition: {
        latitude: 0,
        longitude: 0,
        latitudeDelta: 0,
        longitudeDelta: 0,
      },
    }
  }

  componentDidMount() {
    navigator.geolocation.getCurrentPosition((position) => {
      var lat = parseFloat(position.coords.latitude)
      var long = parseFloat(position.coords.longitude)

      var initialRegion = {
        latitude: lat,
        longitude: long,
        latitudeDelta: LATITUDE_DELTA,
        longitudeDelta: LONGITUDE_DELTA,
      }

      this.setState({initialPosition: initialRegion})
    },
    (error) => alert(JSON.stringify(error)),
    {enableHighAccuracy: true, timeout: 20000, maximumAge: 1000});
  }


  renderScreen = () => {
      return (
        <View style={styles.container}>
          <MapView
            style={styles.map}
            initialRegion={this.state.initialPosition}/>
        </View>
      );
  }

  render() {
    return (
      this.renderScreen()
    );
  }
}

const styles = StyleSheet.create({
  container: {
    position: 'absolute',
    top: 0,
    left: 0,
    right: 0,
    bottom: 0,
    justifyContent: 'flex-end',
    alignItems: 'center',
  },
  map: {
    position: 'absolute',
    top: 0,
    left: 0,
    right: 0,
    bottom: 0,
  },
});

export default MapComponent;

按预期呈现地图,但不在当前设备地理位置中。 (它呈现在海洋中间)

AndroidManifest.xml 中的权限已设置为:

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

20 秒后,我收到一条警报,提示“位置请求超时”“代码 3”

我做错了什么?

【问题讨论】:

  • Geolocation.getCurrentPosition((info) => { let lat = info.coords.latitude; let long = info.coords.longitude; console.log(lat); console.log(long); var initialRegion = { latitude: lat, longitude: long, latitudeDelta: 0.0922, longitudeDelta: 0.0421, }; this.setState({ initialPosition: initialRegion }); });
  • 使用这个功能它会解决你的问题

标签: android react-native geolocation react-native-maps


【解决方案1】:

我不确定,但有一个看起来相关的issue ("Geolocation timed out, code 3")

对于其他在 android 上遇到此问题的人,请尝试删除 maximumAge: 2000 选项参数。此选项导致 地理定位超时或崩溃的应用程序。

【讨论】:

    【解决方案2】:

    它在我的工作完美。我所做的唯一更改是在StyleSheet

    container: {
        position: 'absolute',
        height : SCREEN_HEIGHT,
        width : SCREEN_WIDTH,
        justifyContent: 'flex-end',
        alignItems: 'center',
    }
    

    之后,它会呈现我的位置。您可以在此之后添加标记以显示您的位置。

    【讨论】:

      【解决方案3】:

      这对我有用。第三个选项是创建问题的原因。

      navigator.geolocation.getCurrentPosition() =>{
      
      },
       err => {
      
      },
      { enableHighAccuracy: false, timeout: 20000}, //this worked for me

      【讨论】:

        【解决方案4】:

        navigator.geolocation.getCurrentPosition() 已弃用。
        请改用@react-native-community/geolocation

        npm install @react-native-community/geolocation --save
        

        【讨论】:

        【解决方案5】:
        import React, { Component } from "react";
        // global.currentScreenIndex = 'Dashboard';
        //Import all required component
        import {
          View,
          Text,
          StyleSheet,
          TouchableOpacity,
          Image,
          Modal,
          TouchableHighlight,
          StatusBar,
          FlatList,
          ActivityIndicator,
          Button,
          NetInfo,
        } from "react-native";
        import MapView, { Marker } from "react-native-maps";
        import Geolocation from "@react-native-community/geolocation";
        
        const MyStatusBar = ({ backgroundColor, ...props }) => (
          <View style={[styles.statusBar, { backgroundColor }]}>
            <StatusBar translucent backgroundColor={backgroundColor} {...props} />
          </View>
        );
           
        
        class Dashboard extends Component {
          constructor() {
            super();
            this.state = {
              initialPosition: {
                latitude: 0,
                longitude: 0,
                latitudeDelta: 0,
                longitudeDelta: 0,
              },
            };
          }
        
          componentDidMount() {
            const { navigation } = this.props;
            Geolocation.getCurrentPosition((info) => {
              let lat = info.coords.latitude;
              let long = info.coords.longitude;
        
              console.log(lat);
              console.log(long);
              var initialRegion = {
                latitude: lat,
                longitude: long,
                latitudeDelta: 0.0922,
                longitudeDelta: 0.0421,
              };
              this.setState({ initialPosition: initialRegion });
            });
          }
        
          render() {
            const { modalVisible } = this.state;
            const { navigate } = this.props.navigation;
            // const token_data = token_value();
            // console.log("token_homes_2st"+token_data);
        
            return (
              <View style={styles.container}>
                <MapView
                  style={styles.map}
                  initialRegion={this.state.initialPosition}
                  showsUserLocation={true}
                />
              </View>
            );
          }
        }
        export default Dashboard;
        
        const styles = StyleSheet.create({
          container: {
            justifyContent: "center",
            fontWeight: "400",
            color: "#000",
            fontFamily:
              "Inter UI,-apple-system,BlinkMacSystemFont,Segoe UI,Roboto,Oxygen,Ubuntu,Cantarell,Fira Sans,Droid Sans,Helvetica Neue,sans-serif",
          },
        
          map: {
            height: 400,
            width: 400,
            justifyContent: "flex-end",
            alignItems: "center",
          },
        });
        

        【讨论】:

        • 使用此代码 GUYS 100% 工作正常,谢谢大家。
        【解决方案6】:

        1.npm install @react-native-community/geolocation --save
        2.从'@react-native-community/geolocation'导入地理位置;
        3.navigator.geolocation.getCurrentPosition => Geolocation.getCurrentPosition

        【讨论】:

        • 如果您能更详细地解释您描述的步骤如何帮助回答问题,那就太好了。
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-11-10
        • 2019-03-13
        相关资源
        最近更新 更多