地方详情“ place.opening_hours”未返回所有标记的数据

时间:2018-07-31 16:20:12

标签: google-maps-api-3 google-places-api undefined-symbol

我遇到一个问题,我从Google Maps JavaScript API向Places Libray发出了“地方详情”请求,但是响应并未在所有标签(仅其中一部分)的open_hours对象下返回数据。我已经检查过了,这些标记确实从我的文本搜索中标记了实际地点,并返回了其他字段的数据,并且我在Google Maps Native App上检查了这些地点的open_hours返回“ undefined”,并且它们确实显示了营业时间和营业时间。这是一些代码,可以更好地理解我的意思:

map.ts:

//Request for all places based on query search
var request = {
  location: this.myLocation,
  radius: '400',
  query: "McDonalds"
};

// Callback function for places
function callback(results, status) {
if (status == google.maps.places.PlacesServiceStatus.OK) {
  for (let i = 0; i < results.length; i++) {
    let placeLoc = results[i].geometry.location;
    scopeObj.addMarker(results[i], placeLoc);
  }
}
};
let service = new google.maps.places.PlacesService(this.map);
service.textSearch(request, callback);

//Request for place details of each place, (this would loop through all place ids stored in an array
var request = {
  placeId: details.place_id,
  fields: ['name', 'formatted_address', 'formatted_phone_number', 'opening_hours',]
};

function callback(place, status) {
if (status == google.maps.places.PlacesServiceStatus.OK) {
  console.log(place.opening_hourse); 
/* Returns Object object if data is found, 
   returns undefined otherwise, question is why 
   if status returns OK for all places that have 
   been marked with markers on the map? */
}
}

let service = new google.maps.places.PlacesService(this.map);
service.getDetails(request, callback);

1 个答案:

答案 0 :(得分:0)

使用我自己的API实施,我从各个地方的响应中收到open_hours信息。我已经省略了所有其他内容,但这是我已完成的“地点详细信息”请求,可以在许多企业中重复进行,但仍会获得信息。

   {
   "html_attributions" : [],
   "result" : { 
         OMITTED
      "name" : "McDonald's",
      "opening_hours" : {
         "open_now" : true,
         "periods" : [
            {
               "close" : {
                  "day" : 0,
                  "time" : "1700"
               },
               "open" : {
                  "day" : 0,
                  "time" : "0800"
               }
            },
            {
               "close" : {
                  "day" : 1,
                  "time" : "2100"
               },
               "open" : {
                  "day" : 1,
                  "time" : "0800"
               }
            },
            {
               "close" : {
                  "day" : 2,
                  "time" : "2100"
               },
               "open" : {
                  "day" : 2,
                  "time" : "0800"
               }
            },
            {
               "close" : {
                  "day" : 3,
                  "time" : "2100"
               },
               "open" : {
                  "day" : 3,
                  "time" : "0800"
               }
            },
            {
               "close" : {
                  "day" : 4,
                  "time" : "2100"
               },
               "open" : {
                  "day" : 4,
                  "time" : "0800"
               }
            },
            {
               "close" : {
                  "day" : 5,
                  "time" : "2100"
               },
               "open" : {
                  "day" : 5,
                  "time" : "0800"
               }
            },
            {
               "close" : {
                  "day" : 6,
                  "time" : "1700"
               },
               "open" : {
                  "day" : 6,
                  "time" : "0800"
               }
            }
         ],
         "weekday_text" : [
            "Monday: 8:00 AM – 9:00 PM",
            "Tuesday: 8:00 AM – 9:00 PM",
            "Wednesday: 8:00 AM – 9:00 PM",
            "Thursday: 8:00 AM – 9:00 PM",
            "Friday: 8:00 AM – 9:00 PM",
            "Saturday: 8:00 AM – 5:00 PM",
            "Sunday: 8:00 AM – 5:00 PM"
         ]
      },
    OMITTED
   "status" : "OK"
}

我能看到的与您的实现和我的唯一区别是,您指定了请求中想要的字段。

您可以尝试检索所有字段,而不是像完成操作那样专门请求字段:

var request = {
  placeId: details.place_id,
};

字段是可选参数,可能会导致您出现问题。