0

I'm triying to execute a setState inside the callback function of geocode but isn't possible so how can I execute

this.setState

inside of the func or wait the func until finish and get some results in order to set that info in a new state.

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

Thanks in advance!

Erick Vivanco
  • 239
  • 4
  • 10

0 Answers0