为了账号安全,请及时绑定邮箱和手机立即绑定

如何使用谷歌地图获取一个地方的坐标

如何使用谷歌地图获取一个地方的坐标

跃然一笑 2022-09-23 16:41:27
我有一个地址,我想在地图上显示它,并得到拉特/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 /拉特。我如何使用地图JavaScript API获得它,或者我必须使用其他地图API,如地理编码API
查看完整描述

1 回答

?
万千封印

TA贡献1891条经验 获得超3个赞

从您自己的屏幕截图中可以看出,为了检索纬度/经度的计算值,您需要将这些属性视为函数。

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


查看完整回答
反对 回复 2022-09-23
  • 1 回答
  • 0 关注
  • 208 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号