繁体   English   中英

如何使用谷歌获取地点坐标 map javascript api

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

我有一个地址,我想在 map 上显示它并获取 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>

我懂了:

在此处输入图像描述

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

How can i get that using the Maps JavaScript API or i have to use other map API like Geocoding API

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

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

暂无
暂无

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

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