react-google-maps StandaloneSearchBox设置特定的国家/地区限制?

时间:2018-09-20 14:37:12

标签: react-google-maps

我正在尝试使用react-google-maps StandaloneSearchBox设置特定的国家/地区限制。

我尝试过componentRestrictions,但不确定如何使用。

在下面共享我的代码:

    export const AutoCompleteSearchBox = compose(
    withProps({
      googleMapURL:googleMapUrl,
      loadingElement: <div style={{ height: `100%` }} />,
      containerElement: <div style={{ height: `400px`, top:'3px' }} />,
    }),
    lifecycle({
       componentWillMount() {
         const refs = {}
         this.setState({
           types: ['(regions)'],
           componentRestrictions: {country: "bd"},
           onSearchBoxMounted:ref =>{ refs.searchBox = ref; },
           onPlacesChanged:()=>{ 
             const places = refs.searchBox.getPlaces(); 
             this.props.onPlacesChanged(places); 
           },
         })
         const options = {
          types: ['(regions)'],
          componentRestrictions:{ country: 'bd' }
          }
       },
     }),
     withScriptjs  
   )`(props =>
     <div data-standalone-searchbox="">
       <StandaloneSearchBox 
            ref={props.onSearchBoxMounted} 
            bounds={props.bounds} 
            onPlacesChanged={props.onPlacesChanged} 
            controlPosition={ window.google.maps.ControlPosition.TOP_LEFT}
        >
       <TextField 
            className={props.inputClass}
            placeholder={props.inputPlaceholder}
            label={props.inputLabel}
            name={props.inputName} 
            value={props.inputValue}
            onChange={props.inputOnChange}
            helperText={props.inputHelperText}
            error={props.inputError}
        />
       </StandaloneSearchBox>
     </div>
   );`

我该如何解决这个问题?

1 个答案:

答案 0 :(得分:1)

您不能为SearchBox结果添加此类限制,但可以指定偏向查询预测的区域。预测偏向于但不限于针对这些界限的查询。

如果只想显示特定的地方,则可以使用Google Place Autocomplete功能。为此,您不必为Google Maps使用其他React库。这是示例:

import React, { Component } from 'react';
import Script from 'react-load-script'

class LocationMap extends Component {

    handleScriptLoad() {
        const inputEl = document.getElementById('address-input');

        /*global google*/
        var options = {
            //types: ['address'],
            componentRestrictions: {country: 'by'}
        };
        this.autocomplete = new google.maps.places.Autocomplete(inputEl, options);
        this.autocomplete.addListener('place_changed', this.handlePlaceSelect.bind(this));
    }

    handlePlaceSelect() {
        console.log(this.autocomplete.getPlace());
    }

    render() {
        return (
            <section>
                <Script
                    url="https://maps.googleapis.com/maps/api/js?key=API_KEY&v=3.33&libraries=places&language=en&region=US"
                    onLoad={this.handleScriptLoad.bind(this)}
                />        

                <div className="form-group">
                    <label htmlFor="address-map">Enter address</label>
                    <input type="text"
                           autoComplete="new-password"
                           className="form-control"
                           id="address-input"
                           name="address"/>
                </div>
            </section>
        );
    }
}

export default LocationMap;

不要忘记添加react-load-script软件包:npm i react-load-script --save