直接调用方法,传入指定参数就可以用了
// 自定义信息弹窗
/* lat传入google地图经纬度对象,或准确的数值
var obj = {
lat: 29.915168,//latLng.lat()
lng: 116.403875 ,//latLng.lng()
};
el则是传入存放弹窗的html标签
*/
function diyPop(lat, el) {
class Popup extends google.maps.OverlayView {
constructor(position, content) {
super();
this.position = position;
content.classList.add("popup-bubble");
const bubbleAnchor = document.createElement("div");
bubbleAnchor.classList.add("popup-bubble-anchor");
bubbleAnchor.appendChild(content);
this.containerDiv = document.createElement("div");
this.containerDiv.classList.add("popup-container");
this.containerDiv.appendChild(bubbleAnchor);
Popup.preventMapHitsAndGesturesFrom(this.containerDiv);
}
onAdd() {
this.getPanes().floatPane.appendChild(this.containerDiv);
}
onRemove() {
if (this.containerDiv.parentElement) {
this.containerDiv.parentElement.removeChild(this.containerDiv);
}
}
draw() {
const divPosition = this.getProjection().fromLatLngToDivPixel(
this.position
);
const display =
Math.abs(divPosition.x) < 4000 && Math.abs(divPosition.y) < 4000 ? "block" : "none";
if (display === "block") {
this.containerDiv.style.left = divPosition.x + "px";
this.containerDiv.style.top = divPosition.y + "px";
}
if (this.containerDiv.style.display !== display) {
this.containerDiv.style.display = display;
}
}
}
return new Popup(new google.maps.LatLng(lat), el).setMap(map);
}