【问题标题】:Google Geocode Autocomplete restrict to Maryland State谷歌地理编码自动完成限制在马里兰州
【发布时间】:2020-06-30 10:35:54
【问题描述】:

我正在尝试将 Google 自动完成结果限制在特定状态,使用 Cornad 的答案 - Google GeoCode Autocomplete limit State。它在加利福尼亚州工作,而不是与马里兰州的坐标合作。任何人都可以看看和帮助。完整代码在这里-https://jsfiddle.net/zuhpwjcq/ 及以下是工作和不工作的 sn-ps

工作代码-

var stateBounds={
        ca: ["32.812736", "-119.216815", "34.608128", "-117.039301"]
        // lat/long boundaries list for states/provinces.
    };

    function getStateBounds(state) {
        return new google.maps.LatLngBounds(
          new google.maps.LatLng(stateBounds[state][0], 
                                 stateBounds[state][1]), 
          new google.maps.LatLng(stateBounds[state][2], 
                                 stateBounds[state][3])
        ); 
    }

    new google.maps.places.Autocomplete(document.getElementById('search'), {
        types: ['address'],
        componentRestrictions: {country: 'us'},
        bounds: getStateBounds('ca') // get LatLngBounds for ca.
    });

不工作的代码 -

var stateBounds={
        md: ["39.0457549", "76.64127119999999"]
        // lat/long boundaries list for states/provinces.
    };

    function getStateBounds(state) {
        return new google.maps.LatLngBounds(
          new google.maps.LatLng(stateBounds[state][0], 
                                 stateBounds[state][1])
        ); 
    }

    new google.maps.places.Autocomplete(document.getElementById('search'), {
        types: ['address'],
        componentRestrictions: {country: 'us'},
        bounds: getStateBounds('md') // get LatLngBounds for ca.
    });

【问题讨论】:

  • 边界是由两个角定义的正方形。你只有一个点。
  • 是的,我刚刚意识到并开始使用位置和半径,但结果不准确。你能帮忙吗
  • 请提供一个minimal reproducible example 来证明您的问题。将自动完成限制为正方形不太可能与州的实际(多边形)形状完全对应(特别是对于形状像马里兰州的州)
  • fiddle 可视化马里兰州的边界(黑框)与边界(红色多边形)。
  • @geocodezip 根据您的解释,我只能使用“位置”作为界限来限制马里兰州。这是有效的代码 var myLatlng = new google.maps.LatLng(39.0457549,-76.64127119999999); service.getPlacePredictions({ input: value, types: type, componentRestrictions: { country: 'us' }, location: myLatlng, radius: 500, strictBounds: true }

标签: jquery google-maps-api-3 autocomplete google-geocoder


【解决方案1】:

根据@geocodezip 的解释,我只能使用“位置”作为边界限制在马里兰州。这是有效的代码

var myLatlng = new google.maps.LatLng(39.0457549,-76.64127119999999); service.getPlacePredictions({
            input: value,
            types: type,
            componentRestrictions: {
              country: 'us'
            },
            location: myLatlng,
            radius: 500,
            strictBounds: true
          }

【讨论】:

  • 这并不能回答您自己的问题。您询问如何将自动完成功能限制在马里兰州。相反,这会将自动完成功能限制在给定位置和 500 米的半径范围内。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-10-14
  • 1970-01-01
  • 2013-03-18
  • 2014-06-06
相关资源
最近更新 更多