我目前正在设计一个项目,我需要获取用户键入的任何城市的纬度和经度,一旦输入,我有一个按钮,一旦点击,假设得到这个城市的纬度和经度,并把它在我设置的两个文本框中 . 我在我的项目中使用Google Places API和 Map .
关于我如何实现这一点的任何想法,因为我已经尝试这样做超过一个星期了 .
看看谷歌geocoding api .
我在jsbin上建了a simple example .
$("#city").on("change keyup", function() { var city = $(this).val() $.getJSON("https://maps.googleapis.com/maps/api/geocode/json?address="+encodeURIComponent(city), function(val) { if(val.results.length) { var location = val.results[0].geometry.location $("#lat").val(location.lat) $("#lon").val(location.lng) } }) })
你有一个完美的例子here
var place = autocomplete.getPlace(); if (place.geometry.viewport) map.fitBounds(place.geometry.viewport); else { map.setCenter(place.geometry.location); map.setZoom(17); //Or any zoom level }
看看the documentation
当然,你必须根据自己的需要进行调整 .只需使用 place.geometry 对象中返回的纬度和经度 .
place.geometry
2 回答
看看谷歌geocoding api .
我在jsbin上建了a simple example .
你有一个完美的例子here
看看the documentation
当然,你必须根据自己的需要进行调整 .
只需使用
place.geometry
对象中返回的纬度和经度 .