Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/rust/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 仅自动完成显示特定国家/地区或邮政编码的结果_Javascript_Html_Google Maps_Geolocation - Fatal编程技术网

Javascript 仅自动完成显示特定国家/地区或邮政编码的结果

Javascript 仅自动完成显示特定国家/地区或邮政编码的结果,javascript,html,google-maps,geolocation,Javascript,Html,Google Maps,Geolocation,如何使我的自动完成仅显示来自特定国家或邮政编码的结果 这就是我到目前为止所做的 var input = document.getElementById('searchTextField'); var autocomplete = new google.maps.places.Autocomplete(input); autocomplete.bindTo('bounds', map); 您有几种类型的过滤器: 视口偏移 区域偏置 如果喜欢视口偏移,则应指定

如何使我的自动完成仅显示来自特定国家或邮政编码的结果

这就是我到目前为止所做的

var input = document.getElementById('searchTextField');
        var autocomplete = new google.maps.places.Autocomplete(input);

        autocomplete.bindTo('bounds', map);

您有几种类型的过滤器:

  • 视口偏移
  • 区域偏置
如果喜欢视口偏移,则应指定位置(要检索位置信息的纬度/经度)和半径(返回位置结果的距离(以米为单位)。 如果您更喜欢地区偏差,则必须提供地区(国家代码,请参阅)

使用区域偏置的示例: maps.googleapis.com/maps/api/place/autocomplete/json?input=Vict&types=geocode®ion=CA&language=fr&sensor=true&key=AIzaSyAiFpFd85eMtfbvmVNEYuNds5TEF9FjIPI


您可以在google maps api官方文档中阅读更多内容:

您有几种类型的过滤器:

  • 视口偏移
  • 区域偏置
如果喜欢视口偏移,则应指定位置(要检索位置信息的纬度/经度)和半径(返回位置结果的距离(以米为单位)。 如果您更喜欢地区偏差,则必须提供地区(国家代码,请参阅)

使用区域偏置的示例: maps.googleapis.com/maps/api/place/autocomplete/json?input=Vict&types=geocode®ion=CA&language=fr&sensor=true&key=AIzaSyAiFpFd85eMtfbvmVNEYuNds5TEF9FjIPI

您可以在google maps api官方文档中阅读更多内容:

您可以截取该功能返回的结果,并根据需要使用它们

基本上,您可以在head元素上重新定义appendChild方法,然后监视googleautocomplete代码插入到domforjsonp中的javascript元素。随着javascript元素的添加,您将覆盖Google定义的JSONP回调,以便访问原始的自动完成数据,然后您可以按国家和显示限制这些数据

这是一个有点黑客行为,下面是(我正在使用jQuery,但这种黑客行为没有必要起作用):

您可以截取功能返回的结果,并根据需要使用它们

基本上,您可以在head元素上重新定义appendChild方法,然后监视googleautocomplete代码插入到domforjsonp中的javascript元素。随着javascript元素的添加,您将覆盖Google定义的JSONP回调,以便访问原始的自动完成数据,然后您可以按国家和显示限制这些数据

这是一个有点黑客行为,下面是(我正在使用jQuery,但这种黑客行为没有必要起作用):

//The head element, where the Google Autocomplete code will insert a tag 
//for a javascript file.
var head = $('head')[0];  
//The name of the method the Autocomplete code uses to insert the tag.
var method = 'appendChild';  
//The method we will be overriding.
var originalMethod = head[method];

head[method] = function () {
  if (arguments[0] && arguments[0].src && arguments[0].src.match(/GetPredictions/)) {  //Check that the element is a javascript tag being inserted by Google.
    var callbackMatchObject = (/callback=([^&]+)&|$/).exec(arguments[0].src);  //Regex to extract the name of the callback method that the JSONP will call.
    var searchTermMatchObject = (/\?1s([^&]+)&/).exec(arguments[0].src);  //Regex to extract the search term that was entered by the user.
    var searchTerm = unescape(searchTermMatchObject[1]);
    if (callbackMatchObject && searchTermMatchObject) {
      var names = callbackMatchObject[1].split('.');  //The JSONP callback method is in the form "abc.def" and each time has a different random name.
      var originalCallback = names[0] && names[1] && window[names[0]] && window[names[0]][names[1]];  //Store the original callback method.
      if (originalCallback) {
        var newCallback = function () {  //Define your own JSONP callback
          if (arguments[0] && arguments[0][3]) {
            var data = arguments[0][4];  //Your autocomplete results
            //SUCCESS! - Limit results here and do something with them, such as displaying them in an autocomplete dropdown.
          }
        }

        //Add copy all the attributes of the old callback function to the new callback function. This prevents the autocomplete functionality from throwing an error.
        for (name in originalCallback) {  
          newCallback[name] = originalCallback[name];
        }
        window[names[0]][names[1]] = newCallback;  //Override the JSONP callback
      }
    }

  //Insert the element into the dom, regardless of whether it was being inserted by Google.
  return originalMethod.apply(this, arguments);
};