---
title: "基于googleMap API v3 的PopupMarker"
author: "y1d2y3xyz"
date: "2011-01-27T09:46:25Z"
tags: [".net", "Google", "IE", "Opera", "prototype"]
categories: ["Web前端"]
slug: "googlemap-api-v3-popupmarker"
draft: false
lastmod: "2011-01-27T09:47:39Z"
id: 897939
url: "https://lovebeyond.iteye.com/blog/897939"
---

\[code="java"] PopupMarker.prototype = new google.maps.OverlayView(); function PopupMarker(opts) { this.ICON\_WIDTH = 28;//图标的宽 this.ICON\_HEIGHT = 28;//图标的高 this.map_ = opts.map;//地图对象 this.latlng_ = opts.position; this.icon_ = opts.icon; this.text_ = opts.text || ""; this.setMap(this.map\_); this.showpop = opts.showpop || false; var agt = navigator.userAgent.toLowerCase(); var is\_ie_ = ((agt.indexOf("msie") !== -1) && (agt.indexOf("opera") === -1)); var yPos = 0; this.popupImgSrc_ = "images/1280.png"; this.popupTbl = {}; this.popupTbl.leftTop = { "left": 0, "top": yPos, "width": 19, "height": 7 }; this.popupTbl.leftTopFill = { "left": 16, "top": 3, "width": 4, "height": 4 }; this.popupTbl.rightTop = { "left": 19, "top": yPos, "width": 10, "height": 7 }; this.popupTbl.rightTopImg = { "left": -125, "top": 0, "width": 10, "height": 7 }; this.popupTbl.centerTopFill = { "left": 19, "top": yPos, "width": 0, "height": 7 }; yPos += this.popupTbl.leftTop.height; this.popupTbl.leftBody = { "left": 11, "top": yPos, "width": 8, "height": 0 }; this.popupTbl.centerBodyFill = { "left": 19, "top": yPos, "width": 40, "height": 15 }; this.popupTbl.rightBody = { "left": 19, "top": yPos, "width": 9, "height": 0 }; this.popupTbl.leftBottom = { "left": 0, "top": yPos, "width": 20, "height": 21 }; this.popupTbl.leftBottomImg = { "left": 0, "top": -13, "width": 20, "height": 21 }; this.popupTbl.leftBottomFill = { "left": 16, "top": 0, "width": 4, "height": 6 }; this.popupTbl.rightBottom = { "left": 19, "top": yPos, "width": 10, "height": 7 }; this.popupTbl.rightBottomImg = { "left": -125, "top": -13, "width": 10, "height": 7 }; this.popupTbl.centerBottomFill = { "left": 19, "top": (yPos + (is\_ie_ ? -1: 0)), "width": 0, "height": (6 + (is\_ie_ ? 1: 0)) }; }; PopupMarker.prototype.onAdd = function() { this.container_ = document.createElement("div"); this.iconContainer = document.createElement("div"); this.iconContainer.style.width = this.ICON\_WIDTH + "px"; this.iconContainer.style.height = this.ICON\_HEIGHT + "px"; this.iconContainer.innerHTML = "![](%22%20+%20this.icon_%20+%20%22)"; var panes = this.getPanes(); panes.floatShadow.appendChild(this.iconContainer); this.iconContainer.style.position = "absolute"; panes.floatPane.appendChild(this.container\_); this.container\_.style.position = "absolute"; if (!this.showpop) this.container\_.style.visibility = "hidden"; this.makeNormalPopup\_(); }; PopupMarker.prototype.draw = function() { this.redrawNormalPopup\_(this.text\_); }; PopupMarker.prototype.onRemove = function() { this.container\_.parentNode.removeChild(this.container\_); this.iconContainer.parentNode.removeChild(this.iconContainer); }; PopupMarker.prototype.makeNormalPopup_ = function() { this.leftTop_ = this.makeImgDiv\_(this.popupImgSrc\_, this.popupTbl.leftTop); this.leftTop\_.appendChild(this.fillDiv\_(this.popupTbl.leftTopFill)); this.container\_.appendChild(this.leftTop\_); this.leftBody_ = this.fillDiv\_(this.popupTbl.leftBody); this.leftBody\_.style.borderWidth = "0 0 0 1px"; this.leftBody\_.style.borderStyle = "none none none solid"; this.leftBody\_.style.borderColor = "#000000"; this.container\_.appendChild(this.leftBody\_); this.leftBottom_ = this.makeImgDiv\_(this.popupImgSrc\_, this.popupTbl.leftBottomImg); this.leftBottom\_.style.left = this.popupTbl.leftBottom.left + "px"; this.leftBottom\_.style.top = this.popupTbl.leftBottom.top + "px"; this.leftBottom\_.style.width = this.popupTbl.leftBottom.width + "px"; this.leftBottom\_.style.height = this.popupTbl.leftBottom.height + "px"; this.leftBottom\_.appendChild(this.fillDiv\_(this.popupTbl.leftBottomFill)); this.container\_.appendChild(this.leftBottom\_); this.bodyContainer_ = document.createElement("div"); this.bodyContainer\_.style.position = "absolute"; this.bodyContainer\_.style.backgroundColor = "#FFF"; this.bodyContainer\_.style.overflow = "hidden"; this.bodyContainer\_.style.left = this.popupTbl.centerBodyFill.left + "px"; this.bodyContainer\_.style.top = this.popupTbl.centerBodyFill.top + "px"; this.bodyContainer\_.style.width = this.popupTbl.centerBodyFill.width + "px"; this.bodyContainer\_.style.height = this.popupTbl.centerBodyFill.height + "px"; this.container\_.appendChild(this.bodyContainer\_); this.rightTop_ = this.makeImgDiv\_(this.popupImgSrc\_, this.popupTbl.rightTopImg); this.rightTop\_.style.left = this.popupTbl.rightTop.left + "px"; this.rightTop\_.style.top = this.popupTbl.rightTop.top + "px"; this.rightTop\_.style.width = this.popupTbl.rightTop.width + "px"; this.rightTop\_.style.height = this.popupTbl.rightTop.height + "px"; this.container\_.appendChild(this.rightTop\_); this.rightBottom_ = this.makeImgDiv\_(this.popupImgSrc\_, this.popupTbl.rightBottomImg); this.rightBottom\_.style.left = this.popupTbl.rightBottom.left + "px"; this.rightBottom\_.style.top = this.popupTbl.rightBottom.top + "px"; this.rightBottom\_.style.width = this.popupTbl.rightBottom.width + "px"; this.rightBottom\_.style.height = this.popupTbl.rightBottom.height + "px"; this.container\_.appendChild(this.rightBottom\_); this.rightBody_ = this.fillDiv\_(this.popupTbl.rightBody); this.rightBody\_.style.borderWidth = "0 1px 0 0"; this.rightBody\_.style.borderStyle = "none solid none none"; this.rightBody\_.style.borderColor = "#000000"; this.container\_.appendChild(this.rightBody\_); this.centerBottom_ = this.fillDiv\_(this.popupTbl.centerBottomFill); this.centerBottom\_.style.borderWidth = "0 0 1px 0"; this.centerBottom\_.style.borderStyle = "none none solid none"; this.centerBottom\_.style.borderColor = "#000000"; this.container\_.appendChild(this.centerBottom\_); this.centerTop_ = this.fillDiv\_(this.popupTbl.centerTopFill); this.centerTop\_.style.borderColor = "#000000"; this.centerTop\_.style.borderWidth = "1px 0 0 0"; this.centerTop\_.style.borderStyle = "solid none none none"; this.container\_.appendChild(this.centerTop\_); }; PopupMarker.prototype.redrawNormalPopup_ = function(text) { if (this.beforeNormalPopupText_ !== text) { try { while (this.bodyContainer\_.firstChild) { this.bodyContainer\_.removeChild(this.bodyContainer\_.firstChild); } } catch(e) {} this.bodyContainer\_.innerHTML = text; if (this.isIE\_() === false && this.bodyContainer\_.hasChildNodes) { if (this.bodyContainer\_.firstChild.nodeType === 1) { this.bodyContainer\_.firstChild.style.margin = 0; } } var offsetBorder = this.isIE\_() ? 2: 0; var cSize = this.getHtmlSize\_(text); var rightX = this.popupTbl.leftTop.width + cSize.width; this.leftBottom\_.style.top = (cSize.height + this.popupTbl.leftBody.top) + "px"; this.leftBody\_.style.height = cSize.height + "px"; this.bodyContainer\_.style.width = cSize.width + "px"; this.bodyContainer\_.style.height = cSize.height + "px"; this.bodyContainer\_.style.top = this.popupTbl.leftBody.top; this.rightTop\_.style.left = rightX + "px"; this.rightBottom\_.style.left = this.rightTop\_.style.left; this.rightBottom\_.style.top = this.leftBottom\_.style.top; this.rightBody\_.style.left = rightX + "px"; this.rightBody\_.style.height = this.leftBody\_.style.height; this.centerBottom\_.style.top = this.leftBottom\_.style.top; this.centerBottom\_.style.width = cSize.width + "px"; this.centerTop\_.style.width = cSize.width + "px"; this.size_ = { "width": (rightX + this.popupTbl.rightTop.width), "height": (cSize.height + this.popupTbl.leftTop.height + this.popupTbl.leftBottom.height) }; this.container\_.style.width = this.size\_.width + "px"; this.container\_.style.height = this.size\_.height + "px"; } this.setPosition(this.latlng\_); this.beforeNormalPopupText_ = text; }; PopupMarker.prototype.setPosition = function(latlng) { var pxPos = this.getProjection().fromLatLngToDivPixel(latlng); this.container\_.style.left = pxPos.x + "px"; this.container\_.style.top = (pxPos.y - this.size\_.height) + "px"; this.iconContainer.style.left = (pxPos.x - this.ICON\_WIDTH / 2) + "px"; this.iconContainer.style.top = (pxPos.y - this.ICON\_HEIGHT) + "px"; }; PopupMarker.prototype.isNull = function(value) { if (!value && value !== 0 || value === undefined || value === "" || value === null || typeof value === "undefined") { return true; } return false; }; PopupMarker.prototype.getHtmlSize_ = function(html) { var mapContainer = this.map\_.getDiv(); var onlineHTMLsize_ = function(text) { var dummyTextNode = document.createElement("span"); dummyTextNode.innerHTML = text; dummyTextNode.style.display = "inline"; mapContainer.appendChild(dummyTextNode); var size = {}; size.width = dummyTextNode.offsetWidth; size.height = dummyTextNode.offsetHeight; mapContainer.removeChild(dummyTextNode); return size; }; var ret; var lines = html.split(/\\n/i); var totalSize = new google.maps.Size(1, 1); for (var i = 0; i &lt; lines.length; i++) { ret = onlineHTMLsize\_(lines\[i]); totalSize.width += ret.width; totalSize.height += ret.height; } return totalSize; }; PopupMarker.prototype.makeImgDiv_ = function(imgSrc, params) { var imgDiv = document.createElement("div"); imgDiv.style.position = "absolute"; imgDiv.style.overflow = "hidden"; if (params.width) { imgDiv.style.width = params.width + "px"; } if (params.height) { imgDiv.style.height = params.height + "px"; } var img = null; if (this.isIE\_() === false) { img = new Image(); img.src = imgSrc; } else { img = document.createElement("div"); if (params.width) { img.style.width = params.width + "px"; } if (params.height) { img.style.height = params.height + "px"; } } img.style.position = "relative"; img.style.left = params.left + "px"; img.style.top = params.top + "px"; img.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + imgSrc + "')"; imgDiv.appendChild(img); return imgDiv; }; PopupMarker.prototype.fillDiv_ = function(params) { var bgDiv = document.createElement("div"); bgDiv.style.position = "absolute"; bgDiv.style.backgroundColor = "#FFF"; bgDiv.style.fontSize = "1px"; bgDiv.style.lineHeight = "1px"; bgDiv.style.overflow = "hidden"; bgDiv.style.left = params.left + "px"; bgDiv.style.top = params.top + "px"; bgDiv.style.width = params.width + "px"; bgDiv.style.height = params.height + "px"; return bgDiv; }; PopupMarker.prototype.isIE_ = function() { return (navigator.userAgent.toLowerCase().indexOf('msie') !== -1) ? true: false; }; PopupMarker.prototype.hide = function() { if (this.container\_) { this.container\_.style.visibility = "hidden"; } }; PopupMarker.prototype.show = function() { if (this.container\_) { this.container\_.style.visibility = "visible"; } }; PopupMarker.prototype.toggle = function() { if (this.container\_) { if (this.container\_.style.visibility == "hidden") { this.show(); } else { this.hide(); } } }; PopupMarker.prototype.update = function(obj) { if ((typeof obj.icon) != "undefined") { this.iconContainer.innerHTML = "![](%22%20+%20obj.icon%20+%20%22)"; } if ((typeof obj.position) != "undefined") { this.latlng_ = obj.position; this.setPosition(this.latlng\_); } if ((typeof obj.text) != "undefined") { this.text_ = obj.text; this.redrawNormalPopup\_(obj.text); } }; PopupMarker.prototype.setZIndex = function(index) { this.container\_.style.zIndex = index; this.iconContainer.style.zIndex = index; }; PopupMarker.prototype.latlng = function() { return this.latlng\_; } PopupMarker.prototype.hideMarker = function(){ this.container\_.style.visibility = "hidden"; this.iconContainer.style.visibility = "hidden"; }; PopupMarker.prototype.showMarker = function(){ this.container\_.style.visibility = "visible"; this.iconContainer.style.visibility = "visible"; }; //效果见http://www.gpsoo.net 或者 http://www.goocar.net \[/code]