【问题标题】:How to get opening hours from google places api如何从 google places api 获取营业时间
【发布时间】:2013-12-22 23:06:15
【问题描述】:

这里我有一个运行良好的代码,所以这里是简单的谷歌地点代码,它根据位置向我显示一个地点,我在每个标记上添加一个标签,所以:

http://jsbin.com/UqafeCI/4/edit - 代码和演示

现在我想在标签显示开放时间,所以我添加到我的代码中:

if (!!place.opening_hours.periods[1].open.time) open = place.opening_hours.periods[1].open.time;

现在我的代码如下所示:

google.maps.event.addListener(searchBox, 'places_changed', function() {
    var places = searchBox.getPlaces();

    for (var i = 0, marker; marker = markers[i]; i++) {
      marker.setMap(null);
    }

    // For each place, get the icon, place name, and location.
    markers = [];
    var bounds = new google.maps.LatLngBounds();
    for (var i = 0, place; place = places[i]; i++) {
      var image = {
        url: place.icon,
        size: new google.maps.Size(71, 71),
        origin: new google.maps.Point(0, 0),
        anchor: new google.maps.Point(17, 34),
        scaledSize: new google.maps.Size(25, 25)
      };

      //THIS CODE I ADD TO PREVIOUS CODE THAT WORK, so if there is open time to show in label
      var open = 'No work time';
     if (!!place.opening_hours.periods[1].open.time) open = place.opening_hours.periods[1].open.time;

      // Create a marker for each place.
     var marker = new MarkerWithLabel({
        map: map,
        icon: image,
        title: place.name,
        position: place.geometry.location,
       labelContent: open,
       labelClass: "labels", // the CSS class for the label
       labelStyle: {opacity: 0.75},
              labelAnchor: new google.maps.Point(22, 0)
      });

      markers.push(marker);

      bounds.extend(place.geometry.location);
    }

    map.fitBounds(bounds);
  });

如果时间存在,我如何在标签上显示对象的打开时间???

更新:

我尝试添加 request 和 service.getDetails 函数,但我再次无法获取 opening_time:

代码:

 google.maps.event.addListener(searchBox, 'places_changed', function() {
    var places = searchBox.getPlaces();


    for (var i = 0, marker; marker = markers[i]; i++) {
      marker.setMap(null);
    }

    // For each place, get the icon, place name, and location.
    markers = [];
    var bounds = new google.maps.LatLngBounds();
    for (var i = 0, place; place = places[i]; i++) {
      var image = {
        url: place.icon,
        size: new google.maps.Size(71, 71),
        origin: new google.maps.Point(0, 0),
        anchor: new google.maps.Point(17, 34),
        scaledSize: new google.maps.Size(25, 25)
      };
      var request =  {
          reference: place.reference
    };
      service.getDetails(request, function(place, status) {

      var open = null;
      try{
  open = place.opening_hours.periods[1].open.time;
}
catch(e){
  open='No work time';
}
      var otvoreno = place.openNow+"</br>"+place.name;

      // Create a marker for each place.
     var marker = new MarkerWithLabel({
        map: map,
        icon: image,
        title: place.name,
        position: place.geometry.location,
       labelContent: open,
       labelClass: "labels", // the CSS class for the label
       labelStyle: {opacity: 0.75},
              labelAnchor: new google.maps.Point(22, 0)
      });
    });
      markers.push(marker);

      bounds.extend(place.geometry.location);

    }

    map.fitBounds(bounds);
  });

【问题讨论】:

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


【解决方案1】:

使用 try-catch 语句:

try{
  open = place.opening_hours.periods[1].open.time;
}
catch(e){
  open='No work time';
}

...否则,当以下任何一个未定义时,您将收到错误:

  • place.opening_hours
  • place.opening_hours.periods
  • place.opening_hours.periods[1]
  • place.opening_hours.periods[1].open

但是,文档似乎具有误导性,它表明返回的结果是placeDetails。但事实并非如此,您需要对每个地点进一步请求request the placeDetails(其中将包含opening_hours.periods,如果可用)

【讨论】:

  • 现在还不是圣诞节 ;)
  • 但它是 2 天,所以这将是我的圣诞礼物 :D 谢谢!请使用 placeDetails 功能更新
  • 告诉我在哪里放置 getplace 请求
  • 如您所见,我更新了我的问题,但此代码再次不起作用。为什么?现在哪里出了问题?我看不见?对不起我的英语
  • 请求有限制,对于所有 Web 服务,基本限制为每秒 10 个请求。您需要越来越多地请求详细信息。固定示例:jsbin.com/ATaZEXE/1/edit
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-05-07
  • 2017-06-19
  • 2017-03-10
  • 2020-08-11
相关资源
最近更新 更多