【问题标题】:Initialize React Google Maps StandaloneSearchBox with geocode使用地理编码初始化 React Google Maps StandaloneSearchBox
【发布时间】:2018-05-19 04:56:56
【问题描述】:

谁能告诉我如何使用类型初始化 React Google Maps 的 StandaloneSearchBox 组件:['geocode'](就像在原始的 google.maps.places.Autocomplete 中一样,所以我可以限制自动完成对输入的建议?

【问题讨论】:

  • 我有同样的疑问,请提出任何解决方案!

标签: reactjs react-google-maps


【解决方案1】:

在亚历山大的建议下,我能够做这样的事情。将地址传递到地址搜索并使用地理编码进行附近搜索。希望这会有所帮助。

import React from 'react';
import { compose, withProps, lifecycle } from 'recompose';
import { withScriptjs } from 'react-google-maps';
import StandaloneSearchBox from 'react-google-maps/lib/components/places/StandaloneSearchBox';
import get from 'lodash/get';
import head from 'lodash/head';
import { Search } from '../components';

const GOOGLE_API_KEY = '123';

const AddressSearchbox = compose(
  withProps(props => {
    return {
      ...props,
      googleMapURL: `https://maps.googleapis.com/maps/api/js?key=${GOOGLE_API_KEY}&v=3.exp&libraries=geometry,drawing,places`,
      loadingElement: <div style={{ height: `100%` }} />,
      containerElement: <div style={{ height: `400px` }} />,
    };
  }),
  withScriptjs,
  lifecycle({
    componentDidMount() {
      const refs = {};

      this.setState({
        places: [],
        searchText: '',
        error: null,
        onSearchBoxMounted: ref => {
          refs.searchBox = ref;
        },
        onPlacesChanged: () => {
          const places = refs.searchBox.getPlaces();
          this.setState({
            places,
            searchText: '',
          });
        },
      });

      ***
      const geocoder = new google.maps.Geocoder();
      geocoder.geocode({ address: this.props.placeName }, (results, status) => {
      if (status === google.maps.DirectionsStatus.OK) {
        const lngs = results[0].geometry.bounds.j;
        const lats = results[0].geometry.bounds.l;
        this.setState({
          boundSearch: new google.maps.LatLngBounds(
            new google.maps.LatLng(lats.l, lngs.l),
            new google.maps.LatLng(lats.j, lngs.j)
          ),
        });
      } else {
        this.setState({
          error: status,
        });
      }
      ***
      });
    },
  })
)(props => {
  return (
    <div data-standalone-searchbox="">
      <StandaloneSearchBox
        ref={props.onSearchBoxMounted}
        onPlacesChanged={props.onPlacesChanged}
        bounds={props.boundSearch}
      >
        <Search searchText={props.searchText} />
      </StandaloneSearchBox>
      <div>{get(head(props.places), 'formatted_address')}</div>
    </div>
  );
});

export default AddressSearchbox;

<AddressSearchbox address="cleveland, oh" />

【讨论】:

    【解决方案2】:

    StandaloneSearchBox 是 google.maps.places.SearchBox 的包装器。
    所以搜索框的谷歌文档说:

    SearchBox 构造函数有两个参数:

    • 文本类型的 HTML 输入元素。这是输入字段 SearchBox 服务将监视并将其结果附加到。
    • 一个选项 参数,可以包含 bounds 属性:bounds 是一个 google.maps.LatLngBounds 对象指定要搜索的区域 结果偏向于,但不限于, 包含在这些范围内的地方。

    来源:https://developers.google.com/maps/documentation/javascript/places-autocomplete#places_searchbox

    所以你可以这样调用这个组件:

    <StandaloneSearchBox
          ref={props.onSearchBoxMounted}
          bounds={props.bounds}
          onPlacesChanged={props.onPlacesChanged}
          defaultBounds={new google.maps.LatLngBounds(
                new google.maps.LatLng(40.712216, -74.22655),
                new google.maps.LatLng(40.773941, -74.12544)
          )}
    

    其中defaultBoundsLatLngBounds class,它表示地理坐标中的一个矩形。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-04-05
      • 1970-01-01
      • 2021-08-24
      • 1970-01-01
      • 2010-11-21
      • 2013-12-18
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多