Google商家信息自动填充边界并不总是有效

时间:2017-04-04 13:06:29

标签: javascript google-maps google-places-api

我正在使用Google Maps Places API for JavaScript,并将我的界限设置为柏林,德国

这是我的代码(我正在使用npm-module,但正如您在console.log中看到的那样,边界正确传递):

import React from 'react';
import ReactDOM from 'react-dom';
import Autocomplete from 'react-google-autocomplete';

document.addEventListener('DOMContentLoaded', function() {
  ReactDOM.render(<Autocomplete
        style={{width: '90%'}}
        onPlaceSelected={(place) => {
          console.log(place);
        }}
        types={[]}
        bounds={new window.google.maps.LatLngBounds(
                      new window.google.maps.LatLng(51.2, 13.41),
                      new window.google.maps.LatLng(52.5200, 10.40)
                    )}
      />, document.getElementById('mount'));
});

现在,当我试图在附近寻找一个地方时,它首先在印度给我一个地方,然后是柏林:

没有柏林这个词: enter image description here

用柏林一词: enter image description here

我的界限设置为柏林: enter image description here

  

所以现在我的问题是:这种行为是正常的还是我得到的东西   错误?我试图仅仅出于好奇而切换LatLng,但同样如此   结果

2 个答案:

答案 0 :(得分:2)

您应该将strictBounds设置为true,以将结果限制为仅限于界限。否则你支持那个区域,但结果可能来自外部。

来自API docs

  • bounds是一个google.maps.LatLngBounds对象,指定搜索地点的区域。结果偏向于但不限于这些界限中包含的位置。
  • strictBounds是一个布尔值,指定API是否必须仅返回严格位于给定边界定义的区域内的那些位置。即使API与用户输入匹配,API也不会返回此区域外的结果。

答案 1 :(得分:0)

使用这些界限:

new google.maps.LatLngBounds(
    new google.maps.LatLng(52.35, 13.1),
    new google.maps.LatLng(52.65, 13.8),
)
相关问题