如何使用 google 地图 javascript api 获取地点坐标

How to get coordinates of a place using google map javascript api

我有一个地址,我想把它显示在地图上,同时也得到lat/lng

   <body>
    <div id="floating-panel">
          <input id="address" type="textbox" value="Sydney, NSW">
          <input id="submit" type="button" value="Geocode">
        </div>
        <div id="map"></div>
        <script>
          function initMap() {
            var map = new google.maps.Map(document.getElementById('map'), {
              zoom: 8,
              center: {lat: -34.397, lng: 150.644}
            });
            var geocoder = new google.maps.Geocoder();

            document.getElementById('submit').addEventListener('click', function() {
              geocodeAddress(geocoder, map);
            });
          }

          function geocodeAddress(geocoder, resultsMap) {
            var address = document.getElementById('address').value;
            geocoder.geocode({'address': address}, function(results, status) {
              if (status === 'OK') {
                resultsMap.setCenter(results[0].geometry.location);
                //////////////////////////////////////////
                console.log(results[0].geometry.location);
                //////////////////////////////////////////
                var marker = new google.maps.Marker({
                  map: resultsMap,
                  position: results[0].geometry.location
                });
              } else {
                alert('Geocode was not successful for the following reason: ' + status);
              }
            });
          }
        </script>
        <script async defer
        src="https://maps.googleapis.com/maps/api/js?key=YOUR_API_KEY&callback=initMap">
        </script>
      </body>

我知道了:

地图工作正常,但我无法获得 lng/lat。

如何使用地图 JavaScript API 或者我必须使用其他地图 API 像地理编码 API

正如您从自己的屏幕截图中看到的那样,为了检索 lat/long 的计算值,您需要将这些属性视为函数。

let latVal = results[0].geometry.location.lat();
let lngVal = results[0].geometry.location.lng();