【发布时间】:2019-03-15 10:43:54
【问题描述】:
我正在使用 React.JS,我需要在搜索框中输入 3 个字符后调用 google-place-api 自动完成功能。他们的文档中有偏移量,但它不起作用。 这是我的代码:
renderAutoComplete() {
let attr = this;
const { google, map } = this.props;
var autocompleParam = {
offset: 4
}
const aref = this.refs.autocomplete;
const node = ReactDOM.findDOMNode(aref);
var start_location = document.getElementById('start_location')
var from_address = new google.maps.places.Autocomplete(
start_location, autocompleParam, {
});
from_address.setComponentRestrictions({ 'country': ['uk'] });
google.maps.event.addListener(from_address, 'place_changed', function (e) {
document.getElementById('start_location').style.borderColor = '';
var place = from_address.getPlace();
attr.props.onChangeLocationSearch(place.place_id);
attr.props.onGetLocationInfo(e);
});
var to_address = new google.maps.places.Autocomplete(
(document.getElementById('end_location')), {
});
to_address.setComponentRestrictions({ 'country': ['uk'] });
google.maps.event.addListener(to_address, 'place_changed', function (e) {
var place = to_address.getPlace();
document.getElementById('end_location').style.borderColor = '';
attr.props.onChangeEndLocationSearch(place.place_id);
attr.props.onReturnLocation(e);
});
}
【问题讨论】:
-
看起来类似于之前回答的问题:stackoverflow.com/a/64006859/1759519
标签: reactjs google-maps autocomplete google-places-api