【问题标题】:Setting map region based on current location根据当前位置设置地图区域
【发布时间】:2020-02-20 20:05:18
【问题描述】:

我非常感谢您在这个问题上的帮助。

基本上,我试图通过 var1 和 var2 将我当前的纬度和经度传递到初始区域的坐标中。但是,在运行此代码时,会出现一条错误消息,指出 Warning: Failed prop type: The prop initialRegion.latitude ismarked as required in MapView, but its value is undefined.

我已经将 var1 和 var2 的值打印到控制台,可以看出它们是数字,绝对不是未定义的。有人可以向我解释发生了什么并推荐解决方案吗?

import React, { Component } from 'react';
import { Platform, Text, View, StyleSheet,Dimensions } from 'react-native';
import  MapView, { Marker } from 'react-native-maps';
import Constants from 'expo-constants';
import * as Location from 'expo-location';
import * as Permissions from 'expo-permissions';

export default class App extends Component {
  constructor(props) {
    super(props);
    this.state = 
    {
      region : 
      {
        latitude: 1.290270,
        longitude: 103.851959,
        latitudeDelta: 0.02,
        longitudeDelta: 0.02,
      },
      location: null,
      errorMessage : null,

    }
  }
  componentWillMount() {
    if (Platform.OS === 'ios' && !Constants.isDevice) {
      this.setState({
        errorMessage: 'Oops, this will not work on Sketch in an Android emulator. Try it on your device!',
      });
    } else {
      this._getLocationAsync();
    }
  }

  _getLocationAsync = async () => {
    let { status } = await Permissions.askAsync(Permissions.LOCATION);
    if (status !== 'granted') {
      this.setState({
        errorMessage: 'Permission to access location was denied',
      });
    }

    let location = await Location.getCurrentPositionAsync({});
    this.setState({ location });
  };

  render() {
    let text = 'Waiting..';
    if (this.state.errorMessage) {
      text = this.state.errorMessage;
    } else if (this.state.location) {
      var1 = (this.state.location.coords.latitude);
      var2 = (this.state.location.coords.longitude);
      console.log(var1);
      console.log(var2 +" "+typeof(var2));

    }

    return (
      <View style={styles.container}>
        <Text style={styles.paragraph}>{this.var1, this.var2}</Text>
        <MapView 
        style={styles.mapStyle}  
        initialRegion={{latitude: this.var1,longitude: this.var2, latitudeDelta:0.02, longitudeDelta: 0.02}}
        >
            <Marker
              coordinate={{latitude: 1.290270, longitude: 103.851959}}
              title={'Singapore'}
              description={'Sg505'}
            />
        </MapView> 
      </View>
    );
  }
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    paddingTop: Constants.statusBarHeight,
    backgroundColor: '#ecf0f1',
  },
  paragraph: {
    margin: 24,
    fontSize: 18,
    textAlign: 'center',
  },
  mapStyle: {
    width: Dimensions.get('window').width,
    height: Dimensions.get('window').height,
  },
});

【问题讨论】:

    标签: javascript android react-native mobile expo


    【解决方案1】:

    您尚未定义这些 var1var2 变量。您可以将它们定义为类变量(因此可以使用this.var1 访问它们),但是直接在render 函数上定义它们更有意义。

    此外,在您打印它们的值的地方,Text 组件需要一个字符串子项,您可以在其中传递两个字符串。所以,最好用var1 + var2`${var1}${var2}` 加入这两个字符串

      render() {
        let var1;
        let var2;
        let text = 'Waiting..';
        if (this.state.errorMessage) {
          text = this.state.errorMessage;
        } else if (this.state.location) {
          var1 = (this.state.location.coords.latitude);
          var2 = (this.state.location.coords.longitude);
          console.log(var1);
          console.log(var2 +" "+typeof(var2));
    
        }
    
        return (
          <View style={styles.container}>
            <Text style={styles.paragraph}>{`${var1} ${var2}`}</Text>
            <MapView 
            style={styles.mapStyle}  
            initialRegion={{latitude: var1, longitude: var2, latitudeDelta:0.02, longitudeDelta: 0.02}}
            >
                <Marker
                  coordinate={{latitude: 1.290270, longitude: 103.851959}}
                  title={'Singapore'}
                  description={'Sg505'}
                />
            </MapView> 
          </View>
        );
      }
    

    【讨论】:

    • 非常感谢!!它允许我设置区域但是它仍然显示警告说该值未定义尽管地图正在加载并且在正确的区域?还有为什么当我用 var1 和 var 2 替换标记“坐标={{纬度:1.290270,经度:103.851959}}”中的值时,我收到错误警告:道具类型失败:道具initialRegion.latitude被标记在MapView 中要求,但它的值为undefined
    • @YeoBryan 我刚刚注意到,当location 状态变量为真时,您才设置var1var2。由于它以null 开头,如果不更新,则不会设置var1var2。如果region 状态适用于地图的当前区域,您可以使用它来定义var1var2,至少在locationnull 时。
    猜你喜欢
    • 2019-12-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-12
    • 1970-01-01
    • 2016-03-07
    相关资源
    最近更新 更多