簡體   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