简体   繁体   English

Google Maps API place.url返回“未定义”吗?

[英]Google Maps API places.url returns “undefined”?

I'm having trouble adding some of Google Places API data types to an info window that will display data when a user clicks on a marker on the map. 我在将某些Google Places API数据类型添加到信息窗口时遇到麻烦,该信息窗口会在用户单击地图上的标记时显示数据。 I have included the following data types place.name , place.rating , place.vacinity and these will successfully display the name, rating, and address of the establishment in the info window. 我已经包含了以下数据类型place.nameplace.ratingplace.vacinity ,它们将在信息窗口中成功显示企业的名称,等级和地址。 However when I try to add place.url to return the "URL of the official Google page for this place", it returns undefined . 但是,当我尝试添加place.url以返回“此地方的官方Google页面的URL”时,它会返回undefined These establishments have the URL if I search them via Google so I know that there are URLs however it just cannot access them for some reason? 如果我通过Google搜索这些场所,则它们具有URL,所以我知道有URL,但是由于某种原因它无法访问它们?

This is the Javascript code snippet that creates the marker and adds the info window to the listener that will be called when clicked. 这是Javascript代码段,用于创建标记并将信息窗口添加到单击时将被调用的侦听器。

    function createMarker(place) {

   infowindow = new google.maps.InfoWindow();
  var placeLoc = place.geometry.location;

  var reportmarker = new google.maps.Marker({
    map: map,
    position: place.geometry.location
  });

  markers.push(reportmarker);
  google.maps.event.addListener(reportmarker, 'click', function() {
    infowindow.setContent("<p>" + place.name + "<br />" + place.rating + "<br />" + place.vicinity + "<br />" + place.url+ "<br />" +"</p>");
    infowindow.open(map, this);
  });
}

In your Jsfiddle, you're using nearbySearch() which responses data without url information. 在您的Jsfiddle中,您正在使用nearbySearch()来响应没有URL信息的数据。 You have to use getDetails() to get url. 您必须使用getDetails()来获取URL。

It isn't very clear from the documentation, but only the PlaceDetails result includes the url. 从文档中还不清楚,但是只有PlaceDetails结果包含URL。

 var map; var infowindow; function initMap() { var pyrmont = { lat: -33.867, lng: 151.195 }; map = new google.maps.Map(document.getElementById('map'), { center: pyrmont, zoom: 15 }); infowindow = new google.maps.InfoWindow(); var service = new google.maps.places.PlacesService(map); service.nearbySearch({ location: pyrmont, radius: 500, type: ['store'] }, callback); } function callback(results, status) { if (status === google.maps.places.PlacesServiceStatus.OK) { var service = new google.maps.places.PlacesService(map); for (var i = 0; i < results.length; i++) { var request = { placeId: results[i].place_id } service.getDetails(request, createMarker) } } else console.log("nearbySearch:"+status); } function createMarker(place, status) { if (status === google.maps.places.PlacesServiceStatus.OK) { 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 + ": " + place.rating + ": " + place.url); infowindow.open(map, this); }); } else console.log("placeDetails:"+status); } $(function() { initMap() }) 
 #map { height: 100vh; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <script src="https://maps.googleapis.com/maps/api/js?libraries=places"></script> <div id="map"></div> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM