Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/codeigniter/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript正在等待谷歌地理编码器_Javascript_Google Maps - Fatal编程技术网

Javascript正在等待谷歌地理编码器

Javascript正在等待谷歌地理编码器,javascript,google-maps,Javascript,Google Maps,我有这个代码,问题是它警告“未定义”。如何发出警报(添加)等待地理编码器?(我不想在函数codeAdd中使用警报(添加);) <script> function codeAdd(address){ var geocoder = new google.maps.Geocoder(); var address = "new york"; geocoder.geocode( { 'address': address}, function(results, status) { i

我有这个代码,问题是它警告“未定义”。如何发出
警报(添加)等待地理编码器?(我不想在
函数codeAdd
中使用
警报(添加);

<script>

function codeAdd(address){
var geocoder = new google.maps.Geocoder();
var address = "new york";

geocoder.geocode( { 'address': address}, function(results, status) {

  if (status == google.maps.GeocoderStatus.OK) {
    var latitude = results[0].geometry.location.lat();
    var longitude = results[0].geometry.location.lng();
    return latitude;
  } 
}); }

var add = codeAdd("new york");
alert (add);



</script>
</body>
</html>

功能代码添加(地址){
var geocoder=new google.maps.geocoder();
var address=“纽约”;
geocoder.geocode({'address':address},函数(结果,状态){
if(status==google.maps.GeocoderStatus.OK){
var latitude=results[0]。geometry.location.lat();
var longitude=results[0]。geometry.location.lng();
返回纬度;
} 
}); }
var add=代码添加(“纽约”);
警报(添加);

您遇到的问题是geoencode是异步的,因此它会在返回结果之前继续发送警报

<script>

function codeAdd(address){
var geocoder = new google.maps.Geocoder();
var address = "new york";

geocoder.geocode( { 'address': address}, function(results, status) {

  if (status == google.maps.GeocoderStatus.OK) {
    var latitude = results[0].geometry.location.lat();
    var longitude = results[0].geometry.location.lng();
    return latitude;
  } 
}); }

var add = codeAdd("new york");
alert (add);



</script>
</body>
</html>
您需要将警报放在异步回调中。换句话说,一旦完成查找,需要执行警报(取决于返回的数据)

<script>

function codeAdd(address){
var geocoder = new google.maps.Geocoder();
var address = "new york";

geocoder.geocode( { 'address': address}, function(results, status) {

  if (status == google.maps.GeocoderStatus.OK) {
    var latitude = results[0].geometry.location.lat();
    var longitude = results[0].geometry.location.lng();
    return latitude;
  } 
}); }

var add = codeAdd("new york");
alert (add);



</script>
</body>
</html>
// some calling code
function onLoad(){
  codeAdd("new york");
}



// the function
function codeAdd(address){
var geocoder = new google.maps.Geocoder();
var address = "new york";

geocoder.geocode( { 'address': address}, function(results, status) {

  if (status == google.maps.GeocoderStatus.OK) {
    var latitude = results[0].geometry.location.lat();
    var longitude = results[0].geometry.location.lng();

    // we've got the details, show the alert
    alert (latitude);
  } 
}); 
}

由于不使用codeAdd函数返回任何内容,因此无需执行以下操作:

<script>

function codeAdd(address){
var geocoder = new google.maps.Geocoder();
var address = "new york";

geocoder.geocode( { 'address': address}, function(results, status) {

  if (status == google.maps.GeocoderStatus.OK) {
    var latitude = results[0].geometry.location.lat();
    var longitude = results[0].geometry.location.lng();
    return latitude;
  } 
}); }

var add = codeAdd("new york");
alert (add);



</script>
</body>
</html>
 var add = codeAdd("new york");
但是正义

<script>

function codeAdd(address){
var geocoder = new google.maps.Geocoder();
var address = "new york";

geocoder.geocode( { 'address': address}, function(results, status) {

  if (status == google.maps.GeocoderStatus.OK) {
    var latitude = results[0].geometry.location.lat();
    var longitude = results[0].geometry.location.lng();
    return latitude;
  } 
}); }

var add = codeAdd("new york");
alert (add);



</script>
</body>
</html>
codeAdd("new york");
您可以使用geocode的回调函数:

<script>

function codeAdd(address){
var geocoder = new google.maps.Geocoder();
var address = "new york";

geocoder.geocode( { 'address': address}, function(results, status) {

  if (status == google.maps.GeocoderStatus.OK) {
    var latitude = results[0].geometry.location.lat();
    var longitude = results[0].geometry.location.lng();
    return latitude;
  } 
}); }

var add = codeAdd("new york");
alert (add);



</script>
</body>
</html>
geocoder.geocode({ 
    'address': address
}, function (results, status) {
    alert(results);
    //here the code to execute once geocode is loaded
});