Alright so this code was originally working but all of a sudden after tooling around with the code this error popped up:
好的,这段代码最初是可以工作的,但是在处理完代码之后,突然出现了这个错误:
Unhandled Promise Rejection: Error: Missing parameter. You must specify placeId
It would just not go away even AFTER returning the code to its original state. I'm trying to make it so that if you click on the marker a InfoWindow would pop up which originally worked but now just gives me the error every time I click on a marker.
即使在将代码恢复到其原始状态之后,它也不会消失。我正在尝试这样做,这样如果你点击标记,InfoWindow就会弹出来,这最初是有效的,但现在每次我点击标记时都会出现错误。
Search Algorithm
搜索算法
const service = new google.maps.places.PlacesService(map);
service.textSearch(request, (results, status) => {
if (status === google.maps.places.PlacesServiceStatus.OK) {
results.forEach((place) => {
if (!place.name.includes("ديوان")) {
const marker = new google.maps.Marker({
map,
position: place.geometry.location,
});
const infoWindow = new google.maps.InfoWindow();
MarkerPlaceID.set(marker, place.placeId);
marker.addListener("click", () => {
handleMarkerClick(marker);
resultRowCLick(marker);
});
google.maps.event.addListener(map, 'click', function() {
infoWindows.forEach(function(infoWindow) {
infoWindow.close();
});
});
markers.push(marker);
addNewResultRow(place.name, place.formatted_address, marker);
}
});
console.log("Nearby search results:", results);
} else {
console.error("Error performing nearby search:", status);
}
});
handlerMarkerClick function
HandlerMarkerClick函数
function handleMarkerClick(marker) {
infoWindows.forEach((infoWindow) => {
infoWindow.close();
});
let infoWindow = new google.maps.InfoWindow();
window.addEventListener('keydown', (event) => {
if (event.keyCode === 27) {
infoWindow.close();
}
});
const request = {
placeId: marker.placeId,
fields: [
"name",
"formatted_address",
"photos",
"formatted_phone_number",
"website",
"opening_hours",
"rating",
"reviews",
],
};
const service = new google.maps.places.PlacesService(map);
service.getDetails(request, (place, status) => {
if (status === google.maps.places.PlacesServiceStatus.OK) {
let content = '<div style="max-width: 250px;">'; // Set max-width for the info window
if (place.photos && place.photos.length > 0) {
const photoUrl = place.photos[0].getUrl();
content += '<div class="image-container">';
content += `<img src="${photoUrl}" alt="${place.name}" style="max-width: 250px; max-height: 150px;">`;
content += '</div>';
}
content += `<h2>${place.name}</h2>`;
content += `<p>Address: ${place.formatted_address}</p>`;
if (place.website) {
content += `<p>Website: <a href="${place.website}" target="_blank">${place.website}</a></p>`;
}
if (place.formatted_phone_number) {
content += `<p>Phone: ${place.formatted_phone_number}</p>`;
}
if (place.opening_hours) {
content += `<div class="times">`
content += `<br><p>Opening Hours:</p>`;
content += `<p>${place.opening_hours.weekday_text.map(line => ` ${line}`).join("<br>")}</p>`;
content += `</div>`
}
if (place.rating) {
content += '<div class="rating-container">';
content += '<p>';
content += `rating: ${place.rating}`;
for (let i = 0; i < place.rating; i++) {
content += ' <i class="fa fa-star"></i>';
}
content+= "</p>"
content += '</div>';
}
if (place.reviews && place.reviews.length > 0) {
content += '<p>Reviews:</p>';
content += '<ul>';
place.reviews.forEach((review) => {
if (review.text && review.text.trim().length > 0) {
content += `<li class="review-item">${review.text}</li>`;
}
else {
}
});
content += '</ul>';
}
content += '</div>';
infoWindow.setContent(content);
infoWindow.open(map, marker);
infoWindows.push(infoWindow);
} else {
console.error("Error fetching Place Details:", status);
}
});
}
I can't seem to figure out why the code does not work though I have found a few things:
placeId
is undefined I printed it out. It was working before but now it just became undefined.
我似乎不明白为什么代码不能工作,尽管我发现了一些东西:placeID是未定义的,我把它打印出来了。它以前是有效的,但现在它变得不确定了。
更多回答
It seems like the marker
parameter being passed in to handleMarkerClick
does not have a placeId
property. Perhaps MarkerPlaceID.set(…)
is not doing what you expect - it’s unclear to me what that does.
传递给handleMarkerClick的标记参数似乎没有placeID属性。或许MarkerPlaceID.set(…)并不是在做你所期望的事情--我不清楚这会起什么作用。
@James the thing is I am setting the marker id but the problem is not with MarkerPlaceID
its with the place.placeId
it self when I set the markers placeId to place.placeId
it was undefined after debug I do not know how its possible for place.placeId
to be undefined which is what made me take this different approach
@James问题是我正在设置标记ID,但问题不在于MarkerPlaceID它与位置有关。PlaceID当我将标记PlaceID设置为PlaceID时,它自己。PlaceID在调试后是未定义的。我不知道Place.PlaceID如何可能是未定义的,这就是我采取这种不同方法的原因
Please provide a minimal reproducible example that demonstrates your issue (preferably a StackSnippet in the question itself). What is MarkerPlaceID
?
请提供一个最小的可重现的示例来说明您的问题(最好是问题本身中的StackSnipket)。什么是MarkerPlaceID?
"I do not know how its possible for place.placeId to be undefined" it probably isn't undefined. But marker.placeId is. You haven't shown any code where you are setting marker.placeId.
“我不知道place.placeID怎么可能是未定义的”,它很可能不是未定义的。但marker.placeID才是。您还没有显示设置marker.placeID的任何代码。
MarkerPlaceID is just a map in Java script, @James as for the the marker I have tried to console log both marker.placeId and place.placeId so they are both undefined
MarkerPlaceID只是Java脚本中的一个地图,@James至于我尝试通过控制台记录marker.placeID和place.placeID的标记,因此它们都是未定义的
我是一名优秀的程序员,十分优秀!