仅在少数标记上信息框的值为 null
Value for infobox null on only a few markers
我试图通过 Google 个位置为每个标记获取多个值,它正在工作,但由于某种原因,一半的标记给出了空值的详细信息,我不确定为什么。
function createMarker(place) {
var placeLoc = place.geometry.location;
var marker = new google.maps.Marker({
map: map,
position: place.geometry.location
});
var request = { reference: place.reference };
service.getDetails(request, function(details, status) {
google.maps.event.addListener(marker, 'click', function() {
infowindow.setContent(details.name + "<br />" + details.formatted_address +"<br />" + details.website + "<br />" + details.rating + "<br />" + details.formatted_phone_number);
infowindow.open(map, this);
});
});
}
这是一个有效的 fiddle:http://jsfiddle.net/g6mjkLpx/
您可能运行进入了查询限制。您应该只在单击标记时请求详细信息(并且为了避免信息窗口中的 "undefined" 文本,您应该在将其添加到信息窗口之前检查该字段是否存在):
function createMarker(place) {
var placeLoc = place.geometry.location;
var marker = new google.maps.Marker({
map: map,
position: place.geometry.location
});
google.maps.event.addListener(marker, 'click', function (e) {
var request = {
reference: place.reference
};
service.getDetails(request, function (details, status) {
if (status == google.maps.places.PlacesServiceStatus.OK) {
var infoContent = "";
if (details.name) infoContent += details.name + "<br />";
if (details.formatted_address) infoContent += details.formatted_address + "<br />";
if (details.website) infoContent += details.website + "<br />";
if (details.rating) infoContent += details.rating + "<br />";
if (details.formatted_phone_number) infoContent += details.formatted_phone_number + "<br>";
infowindow.setContent(infoContent);
} else {
infowindow.setContent("request failed, status=" + status);
}
infowindow.open(map, marker);
});
});
}
代码片段:
var pyrmont = new google.maps.LatLng(-33.8665433, 151.1956316);
var map = new google.maps.Map(document.getElementById('map'), {
center: pyrmont,
zoom: 15
});
var request = {
location: pyrmont,
radius: 500,
types: ['store']
};
var infowindow = new google.maps.InfoWindow();
var service = new google.maps.places.PlacesService(map);
service.nearbySearch(request, callback);
function callback(results, status) {
if (status == google.maps.places.PlacesServiceStatus.OK) {
for (var i = 0; i < results.length; i++) {
createMarker(results[i]);
}
}
}
function createMarker(place) {
var placeLoc = place.geometry.location;
var marker = new google.maps.Marker({
map: map,
position: place.geometry.location
});
google.maps.event.addListener(marker, 'click', function(e) {
var request = {
reference: place.reference
};
service.getDetails(request, function(details, status) {
if (status == google.maps.places.PlacesServiceStatus.OK) {
var infoContent = "";
if (details.name) infoContent += details.name + "<br />";
if (details.formatted_address) infoContent += details.formatted_address + "<br />";
if (details.website) infoContent += details.website + "<br />";
if (details.rating) infoContent += details.rating + "<br />";
if (details.formatted_phone_number) infoContent += details.formatted_phone_number + "<br>";
infowindow.setContent(infoContent);
} else {
infowindow.setContent("request failed, status=" + status);
}
infowindow.open(map, marker);
});
});
}
google.maps.event.addDomListener(window, 'load', initialize);
#map {
display: block;
width: 800px;
height: 400px;
}
<script src="http://maps.googleapis.com/maps/api/js?libraries=places"></script>
<div id="map"></div>
我试图通过 Google 个位置为每个标记获取多个值,它正在工作,但由于某种原因,一半的标记给出了空值的详细信息,我不确定为什么。
function createMarker(place) {
var placeLoc = place.geometry.location;
var marker = new google.maps.Marker({
map: map,
position: place.geometry.location
});
var request = { reference: place.reference };
service.getDetails(request, function(details, status) {
google.maps.event.addListener(marker, 'click', function() {
infowindow.setContent(details.name + "<br />" + details.formatted_address +"<br />" + details.website + "<br />" + details.rating + "<br />" + details.formatted_phone_number);
infowindow.open(map, this);
});
});
}
这是一个有效的 fiddle:http://jsfiddle.net/g6mjkLpx/
您可能运行进入了查询限制。您应该只在单击标记时请求详细信息(并且为了避免信息窗口中的 "undefined" 文本,您应该在将其添加到信息窗口之前检查该字段是否存在):
function createMarker(place) {
var placeLoc = place.geometry.location;
var marker = new google.maps.Marker({
map: map,
position: place.geometry.location
});
google.maps.event.addListener(marker, 'click', function (e) {
var request = {
reference: place.reference
};
service.getDetails(request, function (details, status) {
if (status == google.maps.places.PlacesServiceStatus.OK) {
var infoContent = "";
if (details.name) infoContent += details.name + "<br />";
if (details.formatted_address) infoContent += details.formatted_address + "<br />";
if (details.website) infoContent += details.website + "<br />";
if (details.rating) infoContent += details.rating + "<br />";
if (details.formatted_phone_number) infoContent += details.formatted_phone_number + "<br>";
infowindow.setContent(infoContent);
} else {
infowindow.setContent("request failed, status=" + status);
}
infowindow.open(map, marker);
});
});
}
代码片段:
var pyrmont = new google.maps.LatLng(-33.8665433, 151.1956316);
var map = new google.maps.Map(document.getElementById('map'), {
center: pyrmont,
zoom: 15
});
var request = {
location: pyrmont,
radius: 500,
types: ['store']
};
var infowindow = new google.maps.InfoWindow();
var service = new google.maps.places.PlacesService(map);
service.nearbySearch(request, callback);
function callback(results, status) {
if (status == google.maps.places.PlacesServiceStatus.OK) {
for (var i = 0; i < results.length; i++) {
createMarker(results[i]);
}
}
}
function createMarker(place) {
var placeLoc = place.geometry.location;
var marker = new google.maps.Marker({
map: map,
position: place.geometry.location
});
google.maps.event.addListener(marker, 'click', function(e) {
var request = {
reference: place.reference
};
service.getDetails(request, function(details, status) {
if (status == google.maps.places.PlacesServiceStatus.OK) {
var infoContent = "";
if (details.name) infoContent += details.name + "<br />";
if (details.formatted_address) infoContent += details.formatted_address + "<br />";
if (details.website) infoContent += details.website + "<br />";
if (details.rating) infoContent += details.rating + "<br />";
if (details.formatted_phone_number) infoContent += details.formatted_phone_number + "<br>";
infowindow.setContent(infoContent);
} else {
infowindow.setContent("request failed, status=" + status);
}
infowindow.open(map, marker);
});
});
}
google.maps.event.addDomListener(window, 'load', initialize);
#map {
display: block;
width: 800px;
height: 400px;
}
<script src="http://maps.googleapis.com/maps/api/js?libraries=places"></script>
<div id="map"></div>