简体   繁体   English

谷歌地理编码 function 返回未定义

[英]Google geocode function returning undefined

function initMap() {
  const map = new google.maps.Map(document.getElementById("map"), {
  zoom: 15,
  center: {lat: 24.149950, lng: 120.638610},
  mapId: '63d22d3ae6cf15ff'
  });

  console.log(getCoordinates("Bouverie Street"));
}

// geocoder
function getCoordinates(address) {
    const geocoder = new google.maps.Geocoder();
  geocoder.geocode({address: address}, (results, status) => {
    if (status === 'OK') {
        return results[0].geometry.location;
    } else {
        alert("Geocode error: " + status);
        console.log(("Geocode error: " + status));
    }
  });
}

On line 9 I'm trying to log the return object from getCoordinates().在第 9 行,我试图记录 getCoordinates() 的返回 object。 However it shows up as undefined for some reason.但是,由于某种原因,它显示为未定义。 I think the function works as intended as, if I added "console.log(results);"我认为 function 按预期工作,如果我添加“console.log(results);” above the return statement, it logs the result object as intended.在 return 语句上方,它按预期记录结果 object。

if (status === 'OK') {
        return results[0].geometry.location;
    }

What am I doing wrong?我究竟做错了什么? Thanks in advance.提前致谢。

There has occurred an asynchronous issue.发生了异步问题。 To get rid of that(in this case, you're printing latitude and longitude) you can pass a callback parameter when you're calling getCoordinates function.为了摆脱这种情况(在这种情况下,您正在打印纬度和经度),您可以在调用getCoordinates function 时传递一个回调参数。

Here I'm going to use below script for the example:在这里,我将使用以下脚本作为示例:

<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false&callback=initMap" defer></script>

So replace this with your own which will be like this:所以用你自己的替换它,如下所示:

<script type="text/javascript" src="http://maps.google.com/maps/api/js?key=YOUR_API_KEY&callback=initMap" defer></script>

So here I'm going to pass a callback parameter to getCoordinates function which will print coordinates passed from getCoordinates in this way:所以在这里我将传递一个回调参数给getCoordinates function ,它将以这种方式打印从getCoordinates传递的坐标:

 function initMap() { const map = new google.maps.Map(document.getElementById("map"), { zoom: 15, center: { lat: 24.149950, lng: 120.638610 }, mapId: '63d22d3ae6cf15ff' }); getCoordinates("Bouverie Street", printLocation); } function printLocation(location) { console.log("location"); console.log(location.lat()); console.log(location.lng()); } // geocoder function getCoordinates(address, myCallback) { const geocoder = new google.maps.Geocoder(); geocoder.geocode({ address: address }, (results, status) => { if (status === 'OK') { myCallback(results[0].geometry.location); } else { console.warn = () => {}; // stop printing warnings console.log(("Geocode error: " + status)); } }); }
 #map { height: 400px; width: 100%; }
 <script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false&callback=initMap" defer></script> <div id="map"></div>

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

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