使用边界放置搜索请求以提供不一致的结果

时间:2012-03-02 19:54:31

标签: javascript google-maps google-maps-api-3

根据此链接:http://code.google.com/apis/maps/documentation/javascript/places.html#place_search_requests

该方法可以采用

- 一个点和一个半径或

- 一个矩形边界

使用点+半径给出正确的结果,但最大半径只有50公里,我需要搜索到1000公里,所以我尝试使用边界。 当我的界限很小时,我得到与点+半径相同的正确结果,但是当我增加界限时,我得不到任何结果。

下面的代码会给出正确的结果,但是如果用注释掉的坐标替换var sw和var ne,它就不再有用了。

即。厨房位于艾尔和圭尔夫之间的边界内,但不在底特律和渥太华之间的边界。如果你看地图,这没有任何意义。

<script type="text/javascript">
  var map;
  var infowindow;

  function initialize() {
    var Cambridge = new google.maps.LatLng(43.346528, -80.417962);
    var sw = new google.maps.LatLng(43.292501,-80.543175);  // 41.914541,-83.282318  
    var ne = new google.maps.LatLng(43.605057,-80.156250);  // 45.580391,-76.283051
    var zoneBounds = new google.maps.LatLngBounds(sw,ne);

    map = new google.maps.Map(document.getElementById('map'), {
      mapTypeId: google.maps.MapTypeId.ROADMAP,
      center: Cambridge,
      zoom: 10
    });

    var request = {
      bounds: zoneBounds,
      //location: Cambridge,        //using location and
      //radius: 500000,             //radius works
      name: ['Kitchener, ON']


    };
    infowindow = new google.maps.InfoWindow();
    var service = new google.maps.places.PlacesService(map);
    service.search(request, callback);
  }

  function callback(results, status) {
    if (status == google.maps.places.PlacesServiceStatus.OK) {
      for (var i = 0; i < results.length; i++) {
        createMarker(results[i]);
      }
    }
  }

  function createMarker(place) {
    var placeLoc = place.geometry.location;
    var marker = new google.maps.Marker({
      map: map,
      position: place.geometry.location
    });

    google.maps.event.addListener(marker, 'click', function() {
      infowindow.setContent(place.name);
      infowindow.open(map, this);
    });
  }
  google.maps.event.addDomListener(window, 'load', initialize);
</script>

我应该能够使用PlacesServiceStatus来查找请求的状态,我认为这可以让我深入了解问题,但我不知道如何检索状态代码

非常感谢任何帮助

谢谢!

编辑:好的,我添加了警报(状态);并且状态显示它是“ZERO_RESULTS”

1 个答案:

答案 0 :(得分:0)

在回调方法中,您可以执行类似

的操作
function callback(results, status) {
    window.alert(results.length);

它会告诉你你有多少结果,你可以循环它们这样的东西......

        for (var i = 0; i < results.length; i++) {
            window.alert(results[i].formatted_address);
        }

我自己也尝过这个,是的,如果界限大于50公里那么结果就没用了。我能猜出答案;如果边界非常大,那么它可能会对Google服务器造成太大的影响。但是,如果有人知道的话,我不喜欢这个答案。帮助我们!

另一种方法是使用地方自动填充功能 - https://developers.google.com/maps/documentation/javascript/places#places_autocomplete

你可以使用地理位置,但我怀疑它会提供你正在寻找的响应,例如,如果你对“enfield”进行地理编码,它会显示世界上所有的enfields,如果你对地理编码'enf'它会显示你是一个完成的机场!