我正在尝试从 Autocomplete Google Maps API 获取纬度和经度而不显示地图。在我的脚本中,自动补全效果很好,但我无法获得纬度和经度。
<script type="text/javascript">
function initialize() {
var options = {
types: ['(cities)']
};
var input = document.getElementById('searchTextField');
var autocomplete = new google.maps.places.Autocomplete(input, options);
}
google.maps.event.addDomListener(window, 'load', initialize);
var geocoder = new google.maps.Geocoder();
var address = autocomplete;
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);
}
});
</script>