gpt4 book ai didi

javascript - 谷歌地图API : How to show hover card when hover to a place/marker by using javascript

转载 作者:行者123 更新时间:2023-12-02 23:09:14 26 4
gpt4 key购买 nike

我是 google map API 的新手。在我的 javascript 项目中,当我有地点的纬度/经度并将鼠标悬停在它们或标记上(如下图所示)时,我需要显示带有图像、地点名称和地点地址的地点悬停卡,这些地点从谷歌地图 API 返回p>

将鼠标悬停 Place hover

        map = new google.maps.Map(mapContainer, {
center: {lat: -34.397, lng: 150.644},
zoom: 13
});
let infowindow = new google.maps.InfoWindow();
const request = {
placeId: 'ChIJN1t_tDeuEmsRUsoyG83frY4',
fields: ['name', 'formatted_address', 'place_id', 'geometry']
};
const service = new google.maps.places.PlacesService(map);

navigator.geolocation.getCurrentPosition(function (position) {
const currentPos = {
lat: position.coords.latitude,
lng: position.coords.longitude
};

marker = new google.maps.Marker({
position: currentPos,
map: map
});

service.getDetails(request, function(place, status) {
if (status === google.maps.places.PlacesServiceStatus.OK) {
marker.addListener('click', function() {
infowindow.open(map, marker);
// I want to show the info window here with image from google map
console.log(place);
});
}
});
map.setCenter(currentPos);
}

最佳答案

要获取所查询地点的图像,您可以使用 photo 字段,该字段是一个包含 PlacePhoto 对象的数组。您还可以在信息窗口的内容中显示该地点的名称评级user_ ratings_total。看看Google's documentation了解更多信息。

此外,如果您想在将鼠标悬停在标记上时显示信息窗口,则需要在事件监听器中使用“鼠标悬停”而不是“单击”。

尝试以下 jsfiddle 查看可用于指导的工作示例:https://jsfiddle.net/rnLm3wd4/

完整代码如下。

<!DOCTYPE html>
<html>

<head>
<title>Map</title>
<meta name="viewport" content="initial-scale=1.0">
<meta charset="utf-8">
<style>
#map {
height: 100%;
}

html,
body {
height: 100%;
margin: 0;
padding: 0;
}

.infowindow-container {
width: 330px;
}

.inner {
display: inline-block;
position: absolute;
top: 0;
padding: 10px;
}
</style>
</head>

<body>
<div id="map"></div>
<script>
let map;
let marker;

function initMap() {
map = new google.maps.Map(document.getElementById("map"), {
zoom: 13
});
let infowindow = new google.maps.InfoWindow();
const request = {
placeId: 'ChIJN1t_tDeuEmsRUsoyG83frY4',
fields: ['name', 'formatted_address', 'place_id', 'geometry', 'photo', 'rating', 'user_ratings_total']
};
const service = new google.maps.places.PlacesService(map);
service.getDetails(request, function(place, status) {
if (status === google.maps.places.PlacesServiceStatus.OK) {
map.setCenter(place.geometry.location)
marker = new google.maps.Marker({
position: place.geometry.location,
map: map
});
marker.addListener('mouseover', function() {
infowindow.open(map, marker);
infowindow.setContent("<div class='infowindow-container'>" +
"<img src='" + place.photos[0].getUrl({ maxWidth: 200, maxHeight: 150 }) +
"'></img><div class='inner'><h4>" + place.name +
"</h4><p>Rating: " + place.rating + "</p><p>Total reviews: " + place.user_ratings_total + "</p></div></div>");

});
marker.addListener("mouseout", function() {
infowindow.close();
});
}
});
}
</script>
<script src="https://maps.googleapis.com/maps/api/js?key=&callback=initMap&libraries=places" async defer></script>
</body>

</html>

希望这有帮助!

关于javascript - 谷歌地图API : How to show hover card when hover to a place/marker by using javascript,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57450457/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com