Jquery 谷歌地理编码自动完成限制到马里兰州

Jquery 谷歌地理编码自动完成限制到马里兰州,jquery,google-maps-api-3,autocomplete,google-geocoder,Jquery,Google Maps Api 3,Autocomplete,Google Geocoder,我试图将谷歌的自动完成结果限制在一个特定的状态,使用Cornad的答案-。它在加利福尼亚州工作,那里没有马里兰州的协调机构。谁能帮我看看吗。完整的代码在这里-下面是工作和不工作的代码片段 工作代码- var stateBounds={ ca: ["32.812736", "-119.216815", "34.608128", "-117.039301"] // lat/long bou

我试图将谷歌的自动完成结果限制在一个特定的状态,使用Cornad的答案-。它在加利福尼亚州工作,那里没有马里兰州的协调机构。谁能帮我看看吗。完整的代码在这里-下面是工作和不工作的代码片段

工作代码-

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.
    });
非工作代码-

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.
    });

根据@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
          }

边界是由两个角定义的正方形。你只有一个点。是的,我刚刚意识到这一点,并开始使用位置和半径,但结果并不准确。请提供一份能说明您的问题的报告。将“自动完成”限制为正方形不太可能完全对应于该州的实际(多边形)形状(特别是对于形状类似马里兰的州),从而可视化马里兰州的边界(黑框)与边界(红色多边形)。@geocodezip I能够使用“位置”将其限制为仅马里兰州根据你的解释,这是不恰当的。下面是运行var mylatng=new google.maps.LatLng(39.0457549,-76.6412711999999)的代码;getPlacePredictions({input:value,types:type,componentRestrictions:{country:'us'},位置:myLatlng,半径:500,strictBounds:true}这并不能回答您自己的问题。您询问如何将自动完成限制在马里兰州。相反,这将限制自动完成到给定位置和500米半径。