下面我有代码 谷歌自动完成. 两个输入地址的文本框(来源地,目的地)。
// Google Map Autocomplete Code
function initAutocomplete() {
new google.maps.places.Autocomplete(
document.getElementById("origin_address"),
{ types: ["geocode"], componentRestrictions: { country: "IN" } }
);
new google.maps.places.Autocomplete(
document.getElementById("destination_address"),
{ types: ["geocode"], componentRestrictions: { country: "IN" } }
);
}
使用这段代码,我得到了大部分的地址,但是当我按公司名称搜索时,搜索不成功。例如:Ucodice Technologies ,Techlet solution等......。按公司名称搜索时,我无法使用自动完成。为什么会发生这种情况,我不知道。有谁知道这个问题吗?请让我知道,因为我也无法找到我的公司名称。
types: ["geocode"]
告诉自动完成只返回地址,如果你想在搜索结果中返回公司名称,请删除。
从 文件:
地理编码 指示 Place Autocomplete 服务只返回地理编码结果,而不是业务结果。一般来说,您使用此请求来解除指定位置可能不确定的结果。
new google.maps.places.Autocomplete(
document.getElementById("origin_address"),
{ componentRestrictions: { country: "IN" } }
);
代码片段。
// This example requires the Places library. Include the libraries=places
// parameter when you first load the API. For example:
// <script src="https://maps.googleapis.com/maps/api/js?key=YOUR_API_KEY&libraries=places">
// Google Map Autocomplete Code
function initAutocomplete() {
new google.maps.places.Autocomplete(
document.getElementById("origin_address"), {
componentRestrictions: {
country: "IN"
}
}
);
new google.maps.places.Autocomplete(
document.getElementById("destination_address"), {
componentRestrictions: {
country: "IN"
}
}
);
}
/* Always set the map height explicitly to define the size of the div
* element that contains the map. */
#map {
height: 100%;
}
/* Optional: Makes the sample page fill the window. */
html,
body {
height: 100%;
margin: 0;
padding: 0;
}
.pac-card {
margin: 10px 10px 0 0;
border-radius: 2px 0 0 2px;
box-sizing: border-box;
-moz-box-sizing: border-box;
outline: none;
box-shadow: 0 2px 6px rgba(0, 0, 0, 0.3);
background-color: #fff;
font-family: Roboto;
}
#pac-container {
padding-bottom: 12px;
margin-right: 12px;
}
.pac-controls {
display: inline-block;
padding: 5px 11px;
}
.pac-controls label {
font-family: Roboto;
font-size: 13px;
font-weight: 300;
}
.autocomplete-input {
background-color: #fff;
font-family: Roboto;
font-size: 15px;
font-weight: 300;
margin-left: 12px;
padding: 0 11px 0 13px;
text-overflow: ellipsis;
width: 400px;
}
<div class="pac-card" id="pac-card">
<div id="pac-container">
<input id="origin_address" class="autocomplete-input" type="text" placeholder="Enter a location" value="Ucodice Technologies">
<br/>
<input id="destination_address" class="autocomplete-input" type="text" placeholder="Enter a location" value="Techlet solution">
</div>
</div>
<div id="map"></div>
<div id="infowindow-content">
<img src="" width="16" height="16" id="place-icon">
<span id="place-name" class="title"></span><br>
<span id="place-address"></span>
</div>
<!-- Replace the value of the key parameter with your own API key. -->
<script src="https://maps.googleapis.com/maps/api/js?key=AIzaSyCkUOdZ5y7hMm0yrcCQoCvLwzdM6M8s5qk&libraries=places&callback=initAutocomplete" async defer></script>