HOC函数内的地理编码功能

时间:2018-05-18 15:24:14

标签: javascript reactjs google-maps react-google-maps

我正在试图在地理编码的回调函数中执行一个setState但是不可能,所以如何执行

this.setState

在func内部或等待func直到完成并获得一些结果以便将该信息设置为新状态。

onMarkerPositionChanged: (e,reloadList,marker) => {
      var geocoder = new window.google.maps.Geocoder();
      var myLatlng = new window.google.maps.LatLng(e.latLng.lat(),e.latLng.lng());
      var dragEndPositions;
      var nextMarkers
      let sitiosNew =[]

      sessionStorage.id = marker.id
      geocoder.geocode({'latLng': myLatlng }, function(results, status) {
        if (status == window.google.maps.GeocoderStatus.OK) {
        if (results[0]) {
          dragEndPositions = results[0].formatted_address;
          console.log(dragEndPositions)
          nextMarkers = [{
            position: myLatlng,
            nombre: dragEndPositions,
            icon : icon,
            direccion: dragEndPositions,
            latitud: e.latLng.lat(),
            longitud: e.latLng.lng(),
            id: sessionStorage.id
          }]
          if(sessionStorage.auto){
            JSON.parse(sessionStorage.auto).forEach(nextMarker => {
              if (nextMarker.id==nextMarkers.id) {
                sitiosNew.push(nextMarkers)
              }
              sitiosNew.push(nextMarker)
            })
          }
        if(sessionStorage.auto){
          sessionStorage.removeItem("auto")
        }
        sessionStorage.auto = JSON.stringify(sitiosNew)
        this.setState({
            markers: sitiosNew
          })
        reloadList()
        }
        }
        })

提前致谢!

0 个答案:

没有答案
相关问题