Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/361.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/google-maps/4.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_Reverse Geocoding - Fatal编程技术网

Javascript 回调函数不工作

Javascript 回调函数不工作,javascript,google-maps,reverse-geocoding,Javascript,Google Maps,Reverse Geocoding,我创建了一个脚本,可以根据鼠标单击的区域在地图上放置一个标记。放置该标记后,当鼠标悬停在标记上时,将显示一个信息框,其中包含放置标记的地址。但是,信息框没有显示,因为当我调用“info_text”时,它返回未定义的。我意识到我需要添加一个回调函数,但我认为我没有正确地实现它。有人能帮我修改代码吗?谢谢 我的代码: var geocoder; function initialize() { geocoder = new google.maps.Geocoder(); var even

我创建了一个脚本,可以根据鼠标单击的区域在地图上放置一个标记。放置该标记后,当鼠标悬停在标记上时,将显示一个信息框,其中包含放置标记的地址。但是,信息框没有显示,因为当我调用“info_text”时,它返回未定义的。我意识到我需要添加一个回调函数,但我认为我没有正确地实现它。有人能帮我修改代码吗?谢谢

我的代码:

var geocoder;

function initialize() 
{

  geocoder = new google.maps.Geocoder();
  var event = google.maps.event.addListener;

  // intializing and creating the map.
  var mapOptions = {
    zoom: 4,
    center: new google.maps.LatLng(-25.363882, 131.044922),
    mapTypeId: google.maps.MapTypeId.TERRAIN
  };

  var map = new google.maps.Map(document.getElementById('map'),
      mapOptions);

   event(map,'click',function(e){
    var marker = placeMarker_and_attachMessage(e.latLng,map,count);
    count = count + 1;
  });

} // end of initalize.

function placeMarker_and_attachMessage(position,map) 
{
    var event = google.maps.event.addListener;
    var message = ['This', 'is', 'the', 'secret', 'message'];

    var marker = new google.maps.Marker({
        position: position,
        map: map
    }); 

    var pos = info_text(position);
    alert('pos is: ' + pos);

    var infowindow = new google.maps.InfoWindow({
            content: 'hello'
        });

    event(marker,'mouseover',function(){
        infowindow.open(map,this);
    });

    event(marker,'mouseout',function(){
        infowindow.close();
    });

} // end of function.

function info_text(position)
{
    var lat = parseFloat(position.lat());
    var lng = parseFloat(position.lng());
    alert('lat,lng is: ' + (lat,lng));
    var latlng = new google.maps.LatLng(lat,lng);
    alert('just before geocode');
    geocoder.geocode({'latLng': latlng}, function(results, status) 
    {
        if (status == google.maps.GeocoderStatus.OK)
        {
            alert('in info_text If statement');
            if(results[1])
            {
                alert('in inner if statement');
                var finalResult = myCallback(results[1].formatted_address);
                 return finalResult;
            }
            else
            {
                alert('no results found');
            }
        }
        else
        {
            alert('could not pinpoint location');
        }

    });
}

function myCallback(loc)
{
    alert('i have a location!');
    return loc;
}

google.maps.event.addDomListener(window, 'load', initialize); 

您的函数
info\u text
不返回任何内容,因此此行:

var pos = info_text(position);
其中,
pos
未定义

您需要包括:

return something;
在函数中的某个点(就像您在下面做的那样)

已添加 函数
myCallback
返回一个值,但在使用该值的位置:

myCallback(results[1].formatted_address);

它不会将返回值存储在任何位置-它只是被丢弃。

您好。您对如何在异步代码中使用回调有一点误解。我可以推荐一些吗?结果是您无法以您尝试的方式从回调返回值。@Jivings:谢谢您的回复。如果my way不起作用,返回地址的最佳方法是什么?您需要在回调函数中执行所需的操作。我将myCallback(…)设置为一个变量,然后在内部if语句中返回它。所以,虽然“未定义”pos不会出现,但当我将鼠标悬停在标记上时,不会出现任何信息框。。。有什么建议是因为内容-->'hello'?您没有在发布的代码中将
myCallback()
设置为变量,所以我有点不知所措。我假设
geocoder.geocode
是一个AJAX调用,所以这个答案不会解决用户的问题。
return finalResult不起作用。正如Jivings所建议的,您需要了解Ajax和回调是如何工作的。我只是在解释代码中的一些基本错误,关于返回值。