javascript 如何从谷歌地点 api 获取营业时间

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20735016/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 19:09:56  来源:igfitidea点击:

How to get opening hours from google places api

javascriptgoogle-mapsgoogle-places-api

提问by MikiMrki

Here I have a code that works fine, so here is simple google places code that show me a places based on location and I add an label on every marker so:

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

http://jsbin.com/UqafeCI/4/edit- CODE and DEMO

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

Now I want to in label show opening time so I add to my code:

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

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

and now my code looks like this:

现在我的代码如下所示:

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);
  });

So how I can show open time of an object on label if time exist???

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

UPDATE:

更新:

I try to add request and service.getDetails function but again I cant get opening_time:

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

CODE:

代码:

 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);
  });

回答by Dr.Molle

Use a try-catch-statement:

使用 try-catch 语句:

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

...otherwise you will get an error when any of following is undefined:

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

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

But however, the documentation seems to be misleading, it suggests that the returned results are placeDetails. But that's not the fact , you need a further request for each place to request the placeDetails(which will contain the opening_hours.periods, when available)

但是,文档似乎具有误导性,它表明返回的结果是placeDetails。但这不是事实,您需要对每个地方进行进一步请求以请求 placeDetails(其中将包含opening_hours.periods, 当可用时)