【问题标题】:Google Maps JavaScript Places API - URL undefinedGoogle Maps JavaScript Places API - URL 未定义
【发布时间】:2016-09-26 15:01:55
【问题描述】:

我正在将 Google Maps API 集成到我正在开发的网站中。在大多数情况下,它按预期执行。但是,当调用 nearSearch 服务时,我很难获得返回的 url(到该位置的谷歌地图页面)。根据我对 Google 文档的理解,此调用返回具有各种属性的 PlaceResult 对象,包括 url。我能够正确访问其他两个属性,名称和附近,但 url 属性返回为未定义。问题可能是什么?谢谢。

相关代码sn-p:

var keyword =  document.getElementById("searchBox").value;
var requestOptions = {
location: { lat: 37.3011339, lng: -89.5770238},
radius: '5000',
keyword: keyword
};

placesService = new google.maps.places.PlacesService(hotelMap);
placesService.nearbySearch(requestOptions, findCallback);

});

}; // end initiallize

function findCallback(results, status) {

var resultsList = "";


  if (status == google.maps.places.PlacesServiceStatus.OK) {

    for (var i = 0; i < results.length; i++) {

       alert(results[i].url); // this returns undefined           
     resultsList += "<li>" + results[i].name + ": " + results[i].vicinity + " - <a href='" + results[i].url + "'>View Details</a></li>";

    }

    document.getElementById("searchList").innerHTML = resultsList;



  } 
}

供参考:

https://developers.google.com/maps/documentation/javascript/reference#PlaceResult

【问题讨论】:

  • 在更多地点测试?您正在查看的地方实际上可能没有谷歌页面?或者,使用网站对象并仅在可用时显示。

标签: javascript google-maps


【解决方案1】:

nearbySearch 查询中返回的PlaceResult 中似乎没有该属性。从该查询返回的结果中对 placeIds 的详细信息请求,返回具有该属性可用的结果。

根据我对文档的阅读,应该返回“有限”PlaceResult 的唯一查询是 radarSearch,但这似乎不是它的工作方式。

proof of concept fiddle

代码 sn-p:

var geocoder;
var hotelMap;
var infowindow = new google.maps.InfoWindow();

function initialize() {
  hotelMap = new google.maps.Map(
    document.getElementById("map_canvas"), {
      center: new google.maps.LatLng(37.4419, -122.1419),
      zoom: 13,
      mapTypeId: google.maps.MapTypeId.ROADMAP
    });
  var keyword = document.getElementById("searchBox").value;
  var requestOptions = {
    location: {
      lat: 37.3011339,
      lng: -89.5770238
    },
    radius: '5000',
    keyword: keyword
  };

  placesService = new google.maps.places.PlacesService(hotelMap);
  placesService.nearbySearch(requestOptions, findCallback);

}; // end initiallize

function findCallback(results, status) {
  var resultsList = "";
  if (status == google.maps.places.PlacesServiceStatus.OK) {
    var bounds = new google.maps.LatLngBounds();
    for (var i = 0; i < results.length; i++) {
      console.log(results[i].url);
      console.log(results[i]);
      resultsList += "<li>" + results[i].name + ": " + results[i].vicinity + " - <a href='" + results[i].url + "'>View Details</a></li>";
      var marker = new google.maps.Marker({
        position: results[i].geometry.location,
        map: hotelMap,
        title: results[i].name,
        placeId: results[i].place_id
      });
      bounds.extend(marker.getPosition());
      google.maps.event.addListener(marker, 'click', (function(marker) {
        return function(evt) {
          var service = new google.maps.places.PlacesService(hotelMap);
          service.getDetails({
            placeId: this.placeId
          }, function(place, status) {
            if (status === google.maps.places.PlacesServiceStatus.OK) {
              var content = '<div><strong>' + place.name + '</strong><br>' +
                'Place ID: ' + place.place_id + '<br>' +
                place.formatted_address + '<br>';
              if (place.url) {
                content += '<a href=' + place.url + ' target="_blank">[link]</a>';
              }
              content += '</div>';
              infowindow.setContent(content);
              infowindow.setPosition(place.geometry.location);
              infowindow.open(hotelMap, marker);
            }
          });
        }
      })(marker));
    }
    hotelMap.fitBounds(bounds);
  }
  document.getElementById("searchList").innerHTML = resultsList;
}
google.maps.event.addDomListener(window, "load", initialize);
html,
body,
#map_canvas {
  height: 100%;
  width: 100%;
  margin: 0px;
  padding: 0px
}
<script src="https://maps.googleapis.com/maps/api/js?libraries=places"></script>
<input id="searchBox" value="coffee" />
<div id="map_canvas"></div>
<div id="searchList"></div>

【讨论】:

  • 非常感谢您的透彻见解。我希望不是这样(即,附近搜索返回了带有 url 属性的 PlacesResult),但显然我们对此无能为力。再次感谢您的发现。
猜你喜欢
  • 2020-05-11
  • 1970-01-01
  • 2013-11-12
  • 2015-02-02
  • 1970-01-01
  • 2022-01-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多