使用Javascript从Google Places搜索api获取纬度和经度
Posted mracale
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了使用Javascript从Google Places搜索api获取纬度和经度相关的知识,希望对你有一定的参考价值。
如何使用谷歌地图搜索框api从搜索到的位置获取经度和纬度.
我使用与谷歌演示相同的代码 – https://developers.google.com/maps/documentation/javascript/examples/places-searchbox
搜索代码:
<script type="text/javascript"> //自动搜索 function initMap() var input = document.getElementById(‘address_search‘); var places = new google.maps.places.Autocomplete(input); google.maps.event.addListener(places, ‘place_changed‘, function () var place = places.getPlace(); var address = place.formatted_address; var latitude = place.geometry.location.A; var longitude = place.geometry.location.F; var mesg = "Address: " + address; mesg += "nLatitude: " + latitude; mesg += "nLongitude: " + longitude; console.log(place); GetLatlong(); ); //根据google places搜索api获取经纬度 function GetLatlong() var geocoder = new google.maps.Geocoder(); var address = document.getElementById(‘address_search‘).value; geocoder.geocode( ‘address‘: address , function (results, status) if (status == google.maps.GeocoderStatus.OK) var latitude = results[0].geometry.location.lat(); var longitude = results[0].geometry.location.lng(); alert(latitude+","+longitude); ) </script> <script async defer src="https://maps.googleapis.com/maps/api/js?key=xxxxx&libraries=places&callback=initMap"> </script>
以上是关于使用Javascript从Google Places搜索api获取纬度和经度的主要内容,如果未能解决你的问题,请参考以下文章
从google places api保存和刷新place_id的最佳做法是什么?
有没有像 google Place Search API 这样的 Apple API?
当我尝试将 google place 自动完成 API 与 Axios 一起使用时出现 CORS 错误
更改 Google Place Picker Appbar 文本颜色