location - 获取世博会地点的纬度和经度以在 map 上获取

标签 location android-mapview expo

我在世博会的位置文档中找到了此代码:

state = {
location: null,
errorMessage: null,
};

componentWillMount() {
    if (Platform.OS === 'android' && !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) {
    text = JSON.stringify(this.state.location);
    }

   return (
     <View style={styles.container}>
     <Text style={styles.paragraph}>{text}</Text>
       </View>
      );
      }
     }

它可以返回我的纬度、经度、准确度时间戳和其他内容,对我来说效果很好。

有人可以告诉我如何获取坐标并将其渲染在 map 上吗?我想使用经纬度状态并将其从我的位置访问的值传递到该状态以进一步将其放入 map View 的初始区域

谁能帮忙?

最佳答案

我知道这个答案有点晚了,但以防万一有人遇到它,这就是我所做的:

向状态添加了区域属性:

state = {
    location: null,
    region: null,
    errorMessage: null,
};

在 getLocations 中,我将从 getCurrentPositionAsync() 调用返回的位置对象转换为区域:

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({});

        const region = {
            latitude: location.coords.latitude,
            longitude: location.coords.longitude,
            latitudeDelta: 1.0,
            longitudeDelta: 1.0
        }

        this.setState({ location, region });
    };

最后在渲染方法中,我使用 state 的区域属性来设置 map View 中的区域:

render() {
        return (
            <MapView
                initialRegion={this.state.region}
            />
        );
    }

获取位置的异步调用完成后, map 将跳转到当前位置。

关于location - 获取世博会地点的纬度和经度以在 map 上获取,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53358630/

相关文章:

javascript - 如何从另一个网站提取数据以及 ip 、位置和国家/地区代码

android - 谷歌地图不适用于模拟器和设备

iOS7 : method drawMapRect is deprecated

javascript - WARN ViewPropTypes 将从 React Native 中移除。迁移到从 'deprecated-react-native-prop-types' 导出的 ViewPropTypes

javascript - 正则表达式:无效的组说明符名称 - native react

javascript - 如何将我的 expo 应用程序发布到 Apple 应用商店?

multithreading - WP7 App 从服务和更新 UI 更新可观察集合

android - 使用位置预配置 Android 模拟器?

iOS/信标 : Reset didEnterRegion possible?

android - android 中的 MyMapOverlay 类型未定义方法 getBaseContext()?为什么?