反应本机mapbox getCenter不起作用

时间:2019-03-17 15:51:27

标签: javascript react-native react-native-mapbox-gl

我对这个库有问题。 我正在尝试通过(onPress)从地图上获取当前中心坐标,但是没有要显示的

agree = async () => {
  const center = await this._map.getCenter();
  console.warn(center);
}

地图代码。

<MapboxGL.MapView
  ref={(c) => this._map = c}
  style={{ flex: 1 }}
  zoomLevel={15}
  centerCoordinate={[-122.084, 37.422]}>
</MapboxGL.MapView>

我正在像这样使用onPress:

<TouchableOpacity
  style={styles.button}
  onPress={this.agree.bind(this)}
>
  <Text style={styles.buttonText}>NYC</Text>
</TouchableOpacity>

有帮助吗?

1 个答案:

答案 0 :(得分:0)

我已经完成了喜欢(使用导航器):

getCurrentLocation = () => {
        navigator.geolocation.getCurrentPosition((pos) => {
            console.log("lng:"+pos.coords.longitude + "lat:"+pos.coords.latitude); 
            //goto your current location 
            this._map.setCamera({
                centerCoordinate: [pos.coords.longitude, pos.coords.latitude],
                zoom: 17
            });
        });
    }

官方文档:link