gpt4 book ai didi

javascript - 使用 react-places-autocomplete 只希望城市和相关国家在建议中

转载 作者:行者123 更新时间:2023-11-30 06:24:36 28 4
gpt4 key购买 nike

在 react-places-autocomplete 的搜索字段中输入内容时,我只想获得建议中的城市和相关国家。我尝试使用 searchOptions Prop 并放在那里 {{type: ['(cities)', '(country)']}} 但没有用。为了使事情像这样工作,我应该在那里使用什么?有人遇到过同样的问题吗?有什么帮助吗?

以下是我的代码:

import React, { Component } from 'react';
import { render } from 'react-dom';
import Hello from './Hello';
import './style.css';
import PlacesAutocomplete, { geocodeByAddress, geocodeByPlaceId, getLatLng } from 'react-places-autocomplete';

const isObject = val => {
return typeof val === 'object' && val !== null;
};

const classnames = (...args) => {
const classes = [];
args.forEach(arg => {
if (typeof arg === 'string') {
classes.push(arg);
} else if (isObject(arg)) {
Object.keys(arg).forEach(key => {
if (arg[key]) {
classes.push(key);
}
});
} else {
throw new Error(
'`classnames` only accepts string or object as arguments'
);
}
});

return classes.join(' ');
};

class App extends Component {
constructor() {
super();
this.state = {
name: "React",
postAddress: "",
post_address_obj: {},
errorMessage: "",
latitude: null,
longitude: null,
isGeocoding: false
};
}

handlePostAddressChange = address => {
// console.log(address);
this.setState({
postAddress: address,
latitude: null,
longitude: null,
errorMessage: ""
});
};

render() {
return (
<div>
<div
id="post_elements_5"
className="div-capsule-margin location_margin"
>
<PlacesAutocomplete
value={this.state.postAddress}
onChange={this.handlePostAddressChange}
>
{({ getInputProps, suggestions, getSuggestionItemProps }) => {
return (
<div className="Demo__search-bar-container">
<div className="Demo__search-input-container">
<input
{...getInputProps({
placeholder: "Tag the location",
className: "Demo__search-input"
})}
/>
{this.state.postAddress.length > 0 && (
<button
className="Demo__clear-button"
onClick={this.handleCloseClick}
>
x
</button>
)}
</div>
{suggestions.length > 0 && (
<div className="Demo__autocomplete-container">
{suggestions.map(suggestion => {
const className = classnames("Demo__suggestion-item", {
"Demo__suggestion-item--active": suggestion.active
});

return (
/* eslint-disable react/jsx-key */
<div
{...getSuggestionItemProps(suggestion, {
className
})}
>
<strong>
{suggestion.formattedSuggestion.mainText}
</strong>{" "}
<small>
{suggestion.formattedSuggestion.secondaryText}
</small>
</div>
);
/* eslint-enable react/jsx-key */
})}
<div className="Demo__dropdown-footer">
<div>
<img
src="http://files.hostgator.co.in/hostgator254362/image/powered-by-google.png"
className="Demo__dropdown-footer-image"
/>
</div>
</div>
</div>
)}
</div>
);
}}
</PlacesAutocomplete>
</div>
</div>
);
}
}

render(<App />, document.getElementById('root'));

最佳答案

searchOptions 将用于 AutocompletionRequest ,它允许您指定 types of the response .

<PlacesAutocomplete
value={this.state.postAddress}
onChange={this.handlePostAddressChange}
searchOptions={{ types: ['locality', 'country'] }}
>

关于javascript - 使用 react-places-autocomplete 只希望城市和相关国家在建议中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51241664/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com