简体   繁体   English

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

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

I have a address, i want to display it on the map and also get the lat/lng我有一个地址,我想在 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>

I got this:我懂了:

在此处输入图像描述

the map works fine but i can't get the lng/lat. map 工作正常,但我无法获得 lng/lat。

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

As you can see from your own screenshot, in order to retrieve the calculated values of lat/long, you need to treat those attributes like functions.正如您从自己的屏幕截图中看到的那样,为了检索 lat/long 的计算值,您需要将这些属性视为函数。

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

暂无
暂无

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

相关问题 如何通过JavaScript与Google Maps API获取地点的时区偏移量? - How to get the timezone offset of a place using JavaScript with the Google Maps API? Google Geocode Javascript API-如何获取坐标? - Google Geocode Javascript API - How to get coordinates? 谷歌地图API v3如何获取所有形状的坐标 - google map API v3 how to get coordinates of all shapes 谷歌地图 API:如何在使用 javascript 悬停到地点/标记时显示悬停卡 - Google map API: How to show hover card when hover to a place/marker by using javascript 如何使用 @react-google-maps/api 中的 getCenter 获取当前地图中心坐标? - How can I get the current map center coordinates using getCenter in @react-google-maps/api? 如何显示谷歌地图并通过JavaScript代码获取设备上的坐标? - how to show google map and get coordinates on device through javascript code? 如何使用Google Maps JavaScript API v3获取谷歌地图中所查看区域中心的坐标 - how to get coordinates of the center of the viewed area in google maps using Google Maps JavaScript API v3 使用位置获取Google Map Place ID - Get Google Map Place id using Place 如何使用 JavaScript 和 REST API 从嵌入的 esri 地图中获取多边形的坐标? - How to get coordinates of a polygon from an embedded esri map using JavaScript and REST API? 如何使用坐标数组在谷歌地图 API 中绘制多边形? - how to draw a polygon in google map API using array of coordinates?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM