results [0] .formatted_address返回undefined或null

时间:2015-12-28 21:31:51

标签: javascript google-maps geocode

我试图通过一个函数得到地址,我只需要给latLong,但我有一个真正的问题,保存de值返回或返回简单



df %>%
   group_by(date) %>%
   mutate(value = ifelse( ( (value[element == 'TMIN'] >= value[element == 'TMAX']) & element=='TMIN'), NA, value)) %>%
   ungroup




1 个答案:

答案 0 :(得分:1)

Javascript是异步的,如果以这种方式编写,返回的地址将始终为空。这是您当前代码的执行顺序:

function getadress(latLng){

  var adress =""; // (1) Sets address as empty.

  geocoder.geocode( {'latLng': latLng}, // (2) Launches the geocoding request.
      function(results, status) { // (4) later, when the result gets back, populates the address.
          adress = results[0].formatted_address;
      });
  });

  return adress; // (3) Immediately returns an empty address.
}

你应该如何构建它:

function getadress(latLng){

  var adress =""; // (1) Sets address as empty.

  geocoder.geocode( {'latLng': latLng}, // (2) Launches the geocoding request.
      function(results, status) { // (3) later, when the result gets back, populates the address.
          adress = results[0].formatted_address;
          nextStep(address);
      });
  });
}

function nextStep(address){ // (4) should log the address
   console.log(address);
}