问题描述
在这里,我有一个工作正常的代码,所以这里是简单的谷歌地方代码,显示我基于位置的地方,我在每一个标记上添加一个标签:
- CODE和DEMO
现在我想在标签显示打开时间内添加我的代码:
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);
}
//对于每个地方,get
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 open ='没有工作时间';
if (!! place.opening_hours.periods [1 ] .open.time)open = place.opening_hours.periods [1] .open.time;
//为每个地方创建一个标记。
var marker = new MarkerWithLabel({
map:map,
icon:image,
title:place.name,
position:place.geometry.location,
labelContent:open,
labelClass:labels,//标签的CSS类
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:
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);
}
//获取每个地方的图标,地名和位置
标记= [];
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 ='没有工作时间';
}
var otvoreno = place.openNow +< / br>+ place.name;
//为每个地方创建一个标记
var marker = new MarkerWithLabel({
map:map,
icon: image,
title:place.name,
position:place.geometry.location,
labelContent:open,
labelClass:labels,//标签的CSS类
labelStyle:{opacity:0.75},
labelAnchor:new google.maps.Point(22,0)
});
});
markers.push(marker);
bounds.extend(place.geometry.location);
}
map.fitBounds(bounds);
});
使用try-catch语句:
try {
open = place.opening_hours.periods [1] .open.time;
}
catch(e){
open ='没有工作时间';
}
...否则如果以下任何一项未定义,您将收到错误:
-
place.opening_hours
-
place.opening_hours.periods
-
place.opening_hours.periods [1]
-
place.opening_hours.periods [1] .open
但是,文档似乎是误导性的,它表明返回的结果是的。但事实并非如此,您需要针对每个地点的进一步请求才能(其中包含 opening_hours.periods
,如果有的话)
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
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:
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);
});
Use a try-catch-statement:
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
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)
这篇关于如何从谷歌地方API获得开放时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!