地理编码限制问题

Geocoding limit problems

本文关键字:问题 编码      更新时间:2023-11-22


我有一个页面,用户必须在地图上选择一个地方。他仍然可以搜索它。
在移动标记时,我想让他知道"他在哪里",这个值也存储在一个隐藏的输入中,就像纬度和经度一样,这样以后可以用来在数据库中插入值
一切都很好,只是在我插入地理编码功能后(可能是巧合),搜索框无论如何都不起作用。它会显示结果,但当您单击时,不会发生任何事情。我试着删除地理编码,但仍然不起作用
我也得到了OVER_QUERY_LIMIT。这是我的代码:

function initialize() {

    var geocoder = new google.maps.Geocoder(); 
  var myLatlng = new google.maps.LatLng(-1.456688, -48.477586);
  var mapOptions = {
    zoom: 14,
    minZoom: 13,
    center: myLatlng,
    mapTypeId: google.maps.MapTypeId.ROADMAP,
    mapTypeControl: false
  };

  var map = new google.maps.Map(document.getElementById('map'), mapOptions);
  var marker = new google.maps.Marker({
    position: myLatlng,
    map: map,
    draggable: true,
    title: 'Hello World!'
  });
  google.maps.event.addListener(marker, 'position_changed', function() {
    var lat = this.getPosition().lat();
    var lng = this.getPosition().lng();
    var latlng = new google.maps.LatLng(lat, lng);
    document.getElementById('latitude').value = lat;
    document.getElementById('longitude').value = lng;

geocoder.geocode({'latLng': latlng}, function(results, status) {
  if (status == google.maps.GeocoderStatus.OK) {
    if (results[0]) {
       document.getElementById('enderec').value = results[0].formatted_address;
       $('.onde-esta').html(results[0].formatted_address);
    } else {
      alert("Sem resultados");
    }
  } else {
    alert("Geocoder falhou: " + status);
  }
});
  });

  // Create an Autocomplete and link it to the UI element.
  var input = /** @type {HTMLInputElement} */ (
    document.getElementById('pac-input'));
    map.controls[google.maps.ControlPosition.TOP_LEFT].push(input);

  var searchBox = new google.maps.places.Autocomplete(
    /** @type {HTMLInputElement} */
    (input), {
      types: ['geocode']
    });
  // Listen for the event fired when the user selects an item from the
  // pick list. Retrieve the matching places for that item.
  google.maps.event.addListener(searchBox, 'place_changed', function() {
        var place = this.getPlace();
       document.getElementById('enderecoo').value = place.formatted_address;
    //when place has been found
    if (place.geometry) {
      marker.setOptions({
        title: place.name,
        position: place.geometry.location
      });
      if (place.geometry.viewport) {
        marker.getMap().fitBounds(place.geometry.viewport);
      } else {
        marker.getMap().setCenter(place.geometry.location);
      }
    }
    else {
      marker.setOptions({
        title: null
      });
      alert('Lugar não encontrado');
    }
  });
  // Bias the SearchBox results towards places that are within the bounds of the
  // current map's viewport.
  google.maps.event.addListener(map, 'bounds_changed', function() {
    var bounds = map.getBounds();
    searchBox.setBounds(bounds);
  });
}
google.maps.event.addDomListener(window, 'load', initialize);

是因为每次用户移动标记时都会对其进行地理编码吗?有没有什么可以让它只在用户放下标记时进行地理编码?提前感谢,Mateus

是的。我认为你对地理编码api的点击太多或太快了。只有当使用掉标记时才尝试使用它。
google.maps.event.addListener(Marker, "dragend", function(event) { 
     /*
         User stopped dragging.
     */ 
    });