[英]Auto Complete for city names not working
I am trying to get city names in auto complete text box using google places api. 我正在尝试使用Google Places API在自动完成文本框中获取城市名称。 I don't know what is wrong with my code i am not getting city names in my autocomplete text box. 我不知道我的代码有什么问题,我没有在自动填充文本框中输入城市名称。
Here is my code: 这是我的代码:
<script type="text/javascript" src="http://maps.googleapis.com/maps/api/js?libraries=places&language=en"></script> <script type="text/javascript"> google.maps.event.addDomListener(window, 'load', function () { var options = { types: ['(cities)'], componentRestrictions: { country: "us" } }; var input = document.getElementById('City'); var places = new google.maps.places.Autocomplete(input, options); }); </script>
<div> @Html.EditorFor(model => model.City, new { AllowHtmlAttribute = new { @class = "form-control" } }) @Html.ValidationMessageFor(model => model.City) </div>
You need to use Apı key for your googleapis.com reference. 您需要使用Apı键作为googleapis.com参考。
You should be seeing the following error at your console: 您应该在控制台上看到以下错误:
Google Maps API error: MissingKeyMapError Google Maps API错误:MissingKeyMapError
Go to https://developers.google.com/maps/documentation/javascript/get-api-key 前往https://developers.google.com/maps/documentation/javascript/get-api-key
Use your script reference like this: 像这样使用脚本引用:
<script type="text/javascript" src="http://maps.googleapis.com/maps/api/js?key={YOUR_APIKEY};libraries=places&language=en"></script>
I tried and it's working fine. 我试过了,一切正常。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.