- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试在 React 中重新创建这个 Google 自动完成示例:https://developers.google.com/maps/documentation/javascript/examples/places-autocomplete-addressform
我正在使用本教程生成正确加载 Google API 所需的高阶组件:https://www.fullstackreact.com/articles/how-to-write-a-google-maps-react-component/
自动完成功能似乎运行良好。但是,当我选择建议并尝试运行 fillInAddress()
时,这是我收到的错误消息:
Uncaught TypeError: Cannot read property 'getPlace' of undefined
这是我的代码/components/GoogleApiComponent.jsx
import React from 'react';
import {GoogleApiWrapper} from 'google-maps-react';
import GoogleAutoComplete from '../components/GoogleAutoComplete';
export class Container extends React.Component {
render() {
if (!this.props.loaded) {
return <div>Loading...</div>
}
return (
<div>
<GoogleAutoComplete
/>
</div>
)
}
}
export default GoogleApiWrapper({
apiKey: 'somekey'
})(Container)
../components/GoogleAutoComplete.jsx
import React from 'react';
export default class GoogleAutoComplete extends React.Component {
static propTypes = {
}
constructor(props) {
super(props);
}
componentDidMount() {
this.initAutocomplete();
}
initAutocomplete() {
this.autocomplete = new google.maps.places.Autocomplete((this.refs.autoCompletePlaces), {types: ['geocode']});
this.autocomplete.addListener('place_changed', this.fillInAddress);
}
geolocate() {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(function(position) {
const geolocation = {
lat: position.coords.latitude,
lng: position.coords.longitude
};
});
}
}
fillInAddress() {
const componentForm = {
street_number: 'short_name',
route: 'long_name',
locality: 'long_name',
administrative_area_level_1: 'short_name',
country: 'long_name',
postal_code: 'short_name'
};
// Get the place details from the autocomplete object.
const place = this.autocomplete.getPlace();
for (let component in componentForm) {
this.refs.component.value = '';
this.refs.component.disabled = false;
}
// Get each component of the address from the place details
// and fill the corresponding field on the form.
for (let i = 0; i < place.address_components.length; i++) {
const addressType = place.address_components[i].types[0];
if (componentForm[addressType]) {
const val = place.address_components[i][componentForm[addressType]];
this.refs.addressType.value = val;
}
}
}
render() {
return (
<div>
<div>
<input
placeholder="Enter your address"
onFocus={this.geolocate}
ref="autoCompletePlaces"
/>
</div>
<table ref="address">
<tbody>
<tr>
<td>Street address</td>
<td>
<input
ref="street_number"
disabled="true"/>
</td>
<td>
<input
ref="route"
disabled="true"/>
</td>
</tr>
<tr>
<td>City</td>
<td>
<input
ref="locality"
disabled="true"/>
</td>
</tr>
<tr>
<td>State</td>
<td>
<input
ref="administrative_area_level_1"
disabled="true"/>
</td>
<td>Zip code</td>
<td>
<input
ref="postal_code"
disabled="true"/>
</td>
</tr>
<tr>
<td>Country</td>
<td>
<input
ref="country"
disabled="true"/>
</td>
</tr>
</tbody>
</table>
</div>
);
}
}
最佳答案
在 discord 的人们的帮助下,我解决了这个问题。 ,还有这个 stackoverflow question 。根据该答案,关键点是“在 place_changed-callback 内,关键字 this 指向已触发事件的对象。”。我做了这个改变并且它起作用了。
这是关键行:this.place = this.autocomplete.getPlace();
为了完整性,我还重新考虑了表单填写this.place.address_components.forEach((组件,索引) => {
const addressType = this.place.address_components[index].types[0];
if (componentForm[addressType]) {
const val = this.place.address_components[index][componentForm[addressType]];
this.refs[addressType].value = val;
}
})
完整代码如下:
import React from 'react';
export default class GoogleAutoComplete extends React.Component {
static propTypes = {
}
constructor(props) {
super(props);
this.fillInAddress = this.fillInAddress.bind(this);
}
componentDidMount() {
this.initAutocomplete();
}
initAutocomplete() {
const google = window.google;
this.autocomplete = new google.maps.places.Autocomplete((this.refs.autoCompletePlaces), {types: ['geocode']});
this.autocomplete.addListener('place_changed', this.fillInAddress);
}
geolocate() {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(function(position) {
const geolocation = {
lat: position.coords.latitude,
lng: position.coords.longitude
};
});
}
}
fillInAddress() {
const componentForm = {
street_number: 'short_name',
route: 'long_name',
locality: 'long_name',
administrative_area_level_1: 'short_name',
country: 'long_name',
postal_code: 'short_name'
};
// Get the place details from the autocomplete object.
this.place = this.autocomplete.getPlace();
/*this.setState({placeResult: this.place.address_components})*/
for (let component in this.componentForm) {
this.refs.component.value = '';
this.refs.component.disabled = false;
}
// Get each component of the address from the place details
// and fill the corresponding field on the form.
this.place.address_components.forEach((component, index) => {
const addressType = this.place.address_components[index].types[0];
if (componentForm[addressType]) {
const val = this.place.address_components[index][componentForm[addressType]];
this.refs[addressType].value = val;
}
})
}
render() {
return (
<div>
<div>
<input
placeholder="Enter your address"
onFocus={this.geolocate}
ref="autoCompletePlaces"
className="form-control"
type="text"
/>
</div>
<table ref="address">
<tbody>
<tr>
<td>Street address</td>
<td>
<input
ref="street_number"
disabled="true"
value=''
/>
</td>
<td>
<input
ref="route"
disabled="true"/>
</td>
</tr>
<tr>
<td>City</td>
<td>
<input
ref="locality"
disabled="true"/>
</td>
</tr>
<tr>
<td>State</td>
<td>
<input
ref="administrative_area_level_1"
disabled="true"/>
</td>
<td>Zip code</td>
<td>
<input
ref="postal_code"
disabled="true"/>
</td>
</tr>
<tr>
<td>Country</td>
<td>
<input
ref="country"
disabled="true"/>
</td>
</tr>
</tbody>
</table>
</div>
);
}
}
关于javascript - getPlace 函数无法在 React 中与 Google Autocomplete 配合使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42406213/
我在点击和输入更改事件时动态添加了一个文本输入,我需要获取 google api getPlace() 值。 按回车键时效果很好,但是当我从鼠标中选择mouseclick时,place_changed
我正在尝试开发基于 google place API 的应用程序。当我尝试使用时 Place place=PlacePicker.getPlace(data,this); 有一个错误显示 getPla
我正在使用 Google map 自动完成功能,一切正常。 但我想知道是否可以从自定义字符串运行 .getPlace() 函数,无论是坐标还是地址。例如,我不想使用输入字段并单击位置来选择它,而是想手
基于 Google API 的应用程序。当我尝试使用 Place place = PlacePicker.getPlace(this, data) 出现错误显示: cannot resolve met
我是 angularJs/coffeescript 的新手,正在尝试将其与谷歌地图集成。 目标是使用 getPlace() 自动完成后获取 place_id,并将其传递给服务方法将其存储在安全的地方,
调用 autocomplete.getPlace() 时,返回的 PlaceResult 对象现在缺少 formatted_phone_number、international_phone_numbe
我希望在手动触发此事件时能够从 autocomplete.getPlace() 返回数据。 基本上,我有一个函数可以获取用户在 Lat/Lng 中的位置,之后我想获取用户的 getPlace(),因为
我正在尝试在 React 中重新创建这个 Google 自动完成示例:https://developers.google.com/maps/documentation/javascript/examp
setAutocomplete() { this.originPlaceId = null; this.destinationPlaceId = null; this.trav
我想将来自 google the places api 的经度/纬度结果传递到我的 MVC 应用程序操作路由值。我不确定如何将它获取到我的路由值或如何将 javascript 返回到 html 值。现
我有一个使用谷歌地图自动完成库的地址搜索框: var autocompleter = new google.maps.places.Autocomplete(item); 有一个奇怪的情况是地址只返回
我也试过触发事件 google.maps.event.trigger(autocomplete, 'place_changed'); 但是在我手动更改城市之前,autocomplete.getPlac
我是一名优秀的程序员,十分优秀!