Google Places API - 雷达按名称搜索

Google Places API - radarSearch search by name

本文关键字:搜索 雷达按 Places API Google      更新时间:2023-09-26

我正在使用Google Places API并使用radarSearch列出场所。但是,当我使用 name 参数搜索时,我遇到了问题。

它返回了几个地方,但我想得到确切的名称。

在此示例中,我希望返回"最佳购买",但它返回与我正在搜索的单词无关的地点。

            var map;
  var infoWindow;
  var service;
  function initialize() {
    map = new google.maps.Map(document.getElementById('map'), {
      mapTypeId: google.maps.MapTypeId.ROADMAP,
      center: new google.maps.LatLng(33.7490, -84.3880),
      zoom: 11,
      styles: [
        {
          stylers: [
            { visibility: 'simplified' }
          ]
        },
        {
          elementType: 'labels',
          stylers: [
            { visibility: 'on' }
          ]
        }
      ]
    });
    infoWindow = new google.maps.InfoWindow();
    service = new google.maps.places.PlacesService(map);
    performSearch();
  }
  function performSearch() {
    var request = {
      location: new google.maps.LatLng(33.7490, -84.3880),
      radius: 50000,
      name: "Best Buy"
    };
    service.radarSearch(request, callback);
  }
  function callback(results, status) {
    if (status != google.maps.places.PlacesServiceStatus.OK) {
      alert(status);
      return;
    }
    for (var i = 0, result; result = results[i]; i++) {
      createMarker(result);
    }
  }
  function createMarker(place) {
    var marker = new google.maps.Marker({
      map: map,
      position: place.geometry.location,
      icon: 'http://www.google.com/intl/en_us/mapfiles/ms/micons/yellow-dot.png'
    });
    google.maps.event.addListener(marker, 'click', function() {
      service.getDetails(place, function(result, status) {
        if (status != google.maps.places.PlacesServiceStatus.OK) {
          alert(status);
          return;
        }
        infoWindow.setContent(result.name);
        infoWindow.open(map, marker);
      });
    });
  }
  initialize();

根据Google Places Search API(https://developers.google.com/places/web-service/search),在name参数上:

name — 要与地名匹配的一个或多个术语, 用空格字符分隔。结果将仅限于那些 包含传递的名称值。请注意,一个地方可能有 与其关联的其他名称,超出其列出的名称。接口 将尝试将传递的名称值与所有这些名称进行匹配。如 结果,可能会在列出的名称返回的结果中返回位置 与搜索词不匹配,但与其关联的名称不匹配。

我认为你应该通过在它前面使用'来逃避你的空格字符:

var request = {
  location: new google.maps.LatLng(33.7490, -84.3880),
  radius: 50000,
  name: "Best' Buy"
};

注意:在你的小提琴中,我只得到了"百思买..."/"百思买"/"..百思买..."结果,我不明白你的问题

使用此更改更新小提琴(以及小的 jsfiddle 改进): https://jsfiddle.net/fautepdf/16/