从json GPS API反应本地动态更新状态

时间:2019-08-21 09:24:30

标签: react-native react-native-maps

我有google map,我需要使用GPS纬度和经度坐标从自定义API动态更新标记。我编写了componentWillMount()函数,该函数在其中获取日期,但是由于某种原因数据没有传递到经度/纬度的当前状态。我是React Native的新手,我已经构建了两个功能齐全的应用程序,但我仍在努力应对状态。

我尝试了很多硬编码的示例,这些示例对我都有效,但是来自API的响应并将其设置为地图上的图钉却没有效果。

class MarkerTypes extends React.Component {
          constructor(props) {
            super(props);

             this.state = {

               isLoading: true,
               jsondata: [],
               error: null,

我完成功能的代码的第二部分。这就是API返回的内容

{"lat": "45.23846817", "lon": "19.80212021"}
fetchData() {
    fetch('API')
      .then((response) => response.json())
      .then(data =>

        this.setState({
        jsondata: data,
        isLoading: false,
      })
    )
  }

  componentDidMount() {
    this.fetchData();
    console.log(this.jsondata)
  }

在API位置虎钳中进行更改后,我希望指出并更新标记的第三部分。我的响应控制台日志看起来不错,但以后无法获取标记传递的值,一直说不确定。

<MapView.Marker
  testID="API GPS"
  coordinate={this.state.jsondata}
  image={this.Marker}
/>

似乎无法在线找到解决方案。与我的问题相关的所有解决方案似乎都在使用地理位置或对部分代码进行硬编码,这对我在此处遇到的问题完全没有帮助。

1 个答案:

答案 0 :(得分:0)

标记将坐标作为LatLng表示您需要将协调器值作为

传递
{
   latitude:value, 
   longitude:value
}

在您的情况下,它作为{lat:value, lon:value}

传递

从api获得响应后,将数据存储在对象中

this.setState({
    jsondata: {latitude:data.lat, longitude:data.lon},
    isLoading: false,
})
相关问题