繁体   English   中英

如何使用Google API的placekicker的place_changed事件?

[英]how to use place_changed event of placekicker of google api?

我已经快做完了,但是我面临的一个问题是我的问题:-

I have successfully implement the placekicker with uk country only. Here is my code:-
Input location here:<input id="searchTextField" type="text" size="50">
<script src='https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js'></script>
<script src="https://maps.googleapis.com/maps/api/js?sensor=false&libraries=places"></script>
<script>
var pac_input = document.getElementById('searchTextField');
$(function() {
  var options = {
    componentRestrictions: {
      country: 'uk' //PUT YOUR COUNTRY CODE HERE!!
    }
  };
  var autocomplete = new google.maps.places.Autocomplete(pac_input, options);
});

当我运行上面的代码时,它将仅向我显示英国结果,即是正确的,但是当我在上面的代码中添加了更多功能时,它将显示整个国家的记录。 添加的代码在这里:

<script type="text/javascript">
    google.maps.event.addDomListener(window, 'load', function () {
        var places = new google.maps.places.Autocomplete(document.getElementById('searchTextField'));
        google.maps.event.addListener(places, 'place_changed', function () {
            var place = places.getPlace();
            var address = place.formatted_address;
            var latitude = place.geometry.location.lat();
            var longitude = place.geometry.location.lng();
            var mesg = "Address: " + address;
            mesg += "\nLatitude: " + latitude;
            mesg += "\nLongitude: " + longitude;
            alert(mesg);
        });
    });
</script>

当我添加此代码时,它将给出我已从Placepicker选择的每个地方的纬度和经度,现在的问题是它将提供整个国家/地区的数据,但是当我删除此代码时,它会给我英国的结果。 请帮我。 我做错了。 提前致谢 :)

尝试传递自动完成功能中的选项

var options = {
    componentRestrictions: {
      country: 'uk' //PUT YOUR COUNTRY CODE HERE!!
    }
  };
    google.maps.event.addDomListener(window, 'load', function () {
        var places = new google.maps.places.Autocomplete(document.getElementById('searchTextField'), options );  //// options added
        google.maps.event.addListener(places, 'place_changed', function () {
            var place = places.getPlace();
            var address = place.formatted_address;
            var latitude = place.geometry.location.lat();
            var longitude = place.geometry.location.lng();
            var mesg = "Address: " + address;
            mesg += "\nLatitude: " + latitude;
            mesg += "\nLongitude: " + longitude;
            alert(mesg);
        });
    });

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM