阻止 Google 距离矩阵服务使用区域代码

Prevent Google Distance Matrix Service from using AREA codes

我正在研究我的表单功能的一个核心部分,它可以找到两个邮政编码之间的距离。它确实准确地找到了距离,但不幸的是,这个 Google 距离矩阵 API 也在搜索现有的区号,这可能导致随机和不必要的里程 returns。

目前,我可以找到 64154 和 816 (19.1) 之间的里程数,但我想完全省略任何 AREA CODE 数据的 return。

这是returns说数据的函数:

 handleChange = (e) => {
    const name = e.target.name
    const value = e.target.value
    this.setState({
      [name]: value
  //call back function to make sure we get the last character from the user input
    }, () => {
      this.getMiles(this.state.origin, this.state.destination)
    })
  }

  getMiles = (origin, destination) => {
    const {google} = this.props
     let service = new google.maps.DistanceMatrixService()
        console.log(origin)
        console.log(destination)
         service.getDistanceMatrix({
          origins: [origin],
          destinations: [destination],
          travelMode: 'DRIVING',
          unitSystem: google.maps.UnitSystem.IMPERIAL,
          avoidHighways: false,
          avoidTolls: false
        }, (response, status) => {
          if (status !== 'OK') {
            alert('Error was: ' + status);
          } else {
            let originList = response.originAddresses;
            let destinationList = response.destinationAddresses;
            if (response.rows[0].elements[0].status === 'NOT_FOUND'){
              return false
            } else{
                this.setState({mileage: response.rows[0].elements[0].distance.text.replace("mi", " ")})
            }
        }
    });
  }

我在文档中找不到任何允许我过滤掉某些地址类型的内容。我什至尝试对字符长度设置不同的形式限制,这在大多数情况下都有效,但用户仍然可以输入三位数字和 "space" 一直到输入的末尾。不幸的是,这将绕过我的条件和 return 区域代码的数据,因为我正在寻找的字符长度将得到满足 "816 ".length === 5

嗯,我刚刚结束使用正则表达式模式来检查字符串是否包含 5 位数字。

这样做的目的是,它不会阻止 API 返回 AREA CODE 搜索,但它确实会通过不断将其设置为零来阻止我计算里程数:

let fiveNumberMatch = /^[0-9]{5}$/;

<h1>{!this.state.origin.match(fiveNumberMatch) || !this.state.destination.match(fiveNumberMatch) ? this.state.mileage = 0 : this.state.mileage}</h1>

暂时可用!