如何防止谷歌的地理编码警报状态消息

How to prevent Google's geocode alert status message?

本文关键字:状态 消息 编码 何防止 谷歌      更新时间:2023-09-26
function initMap() {
      var this_input;
     /* var options = {
  types: ['(cities)'],
  componentRestrictions: {country: "my"}
 };*/
      $("input[name=location]").focusin(function(){
   // alert($(this).attr("id"));
     this_input = $(this).attr("id");
  var input = /** @type {!HTMLInputElement} */(
      document.getElementById(this_input));
  var autocomplete = new google.maps.places.Autocomplete(input);
   var infowindow = new google.maps.InfoWindow();
  var geocoder = new google.maps.Geocoder();
  autocomplete.addListener('place_changed', function() {
    infowindow.close();
    var place = autocomplete.getPlace();
    if (!place.geometry) {
      window.alert("Autocomplete's returned place contains no geometry");
      return;
    }
    var address = '';
    if (place.address_components) {
      address = [
        (place.address_components[0] && place.address_components[0].short_name || ''),
        (place.address_components[1] && place.address_components[1].short_name || ''),
        (place.address_components[2] && place.address_components[2].short_name || '')
      ].join(' ');
    }
    infowindow.setContent('<div><strong>' + place.name + '</strong><br>' + address);
  });
   document.getElementById('submit').addEventListener('click', function() {
   geocodeAddress(geocoder);
  });

  });//end of input[name=location]
}

ABove 是我的脚本,用于在带有name ='location'的任何focusin文本字段上显示自动完成。这工作正常。只是,我已经删除了一堆geocoder代码,提醒输入位置的状态,即:ZERO_RESULTS。

但它会不断发出警报、超出限制的查询ZERO_RESULT消息。我不知道这些消息是从哪里提示的,因此我该如何阻止它?

您需要从地理编码地址函数开始。

你的函数可能会喜欢这个

  function geocodeAddress(geo) {
    geo.geocode({address:search}, function (results, status)
      { 
        // If that was successful
        if (status == google.maps.GeocoderStatus.OK) {
            ...
        }
        // ====== Decode the error status ======
        else {
            // Make alert when error occurs
            window.alert(status); // <-- delete this line or modify it                
        }
      }
    );
  }

您必须在地理编码处理程序中使用错误状态发出警报。因此,您应该修改处理程序代码。