简体   繁体   English

返回警告“未定义”的javascript对象

[英]returning javascript object alerting “undefined”

I apologize ahead of time because I feel like this is such a simple question, but I'm new so I don't quite understand how to do it yet! 我提前道歉,因为我觉得这是一个简单的问题,但是我是新手,所以我还不太了解该怎么做!

I'm geocoding an address and I return the coordinates: 我正在对地址进行地理编码,然后返回坐标:

//Geocoder
var geocoder = new google.maps.Geocoder();

//Submit listener and alert the coordinates
submit.addEventListener('click', function() {
    alert(geocodeAddress(geocoder, map)));
});

//Geocodes an address and returns the lat lng
function geocodeAddress(geocoder, resultsMap) {
    geocoder.geocode({address: address.value}, function(addressLatLng, status) {

        //Checks if status returned was ok
        if (status === google.maps.GeocoderStatus.OK) {

            //adds pin to map and centers map on pin
            resultsMap.setCenter(addressLatLng[0].geometry.location);
            var marker = new google.maps.Marker({
                map: resultsMap,
                position: addressLatLng[0].geometry.location
            });
            //alert(addressLatLng[0].geometry.location);
            return(addressLatLng[0].geometry.location);
        } else {
            alert('No address was found, please try again!');
        }
    });
}

If I alert them inside the function, it alerts them correctly (ie {20.12345, 20.12345}. If I alert them on the submit button, it just says "undefined." How would I correctly return those coordinates? (I ultimately have to do something with them, not just alert them) Thanks! 如果我在函数内部向它们发出警报,它将正确地向它们发出警报(即{20.12345,20.12345}。如果在提交按钮上向它们发出警报,它只会显示“未定义。”如何正确返回这些坐标?(最终我必须这样做)与他们保持联系,而不仅仅是提醒他们)谢谢!

This should work 这应该工作

//Geocoder
var geocoder = new google.maps.Geocoder();

//Submit listener and alert the coordinates
submit.addEventListener('click', function() { //This is callback
    geocodeAddress(geocoder, map, function(loc){
      alert(loc || 'No address was found, please try again!');   
    });
});

//Geocodes an address and returns the lat lng
function geocodeAddress(geocoder, resultsMap, cb) {
    geocoder.geocode({address: address.value}, function(addressLatLng, status) {

        //Checks if status returned was ok
        if (status === google.maps.GeocoderStatus.OK) {

            //adds pin to map and centers map on pin
            resultsMap.setCenter(addressLatLng[0].geometry.location);
            var marker = new google.maps.Marker({
                map: resultsMap,
                position: addressLatLng[0].geometry.location
            });
             //call cb with location details
            cb(addressLatLng[0].geometry.location);
        } else {
            //call the callback with empty value
            cb('');
        }
    });
}

Hope this helps! 希望这可以帮助!

I believe this is an async problem. 我相信这是一个异步问题。 You need to return a Promise from your function, and then call the alert when the promise is fulfilled. 您需要从函数中返回一个Promise ,然后在实现Promise时调用警报。

The reason alert is showing undefined is because the function hasn't finished yet, so it hasn't returned by the time the alert is shown. 警报显示为未定义的原因是因为该函数尚未完成,因此在显示警报时尚未返回。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM