---
title: "google map 自定义矩形框"
author: "feirou520"
date: "2010-09-12T21:30:00Z"
tags: ["Google", "HTML", "prototype"]
categories: ["Web前端"]
slug: "google-map"
draft: false
lastmod: "2010-09-12T21:30:00Z"
id: 761604
url: "https://feirou520.iteye.com/blog/761604"
---

转自:[http://www.playgoogle.com/post/3.html](http://www.playgoogle.com/post/3.html)  
![](http://www.playgoogle.com/upload/googleMap2.gif "GoogleMap 上添加自定义图层覆盖（继承自GOverlay） - kangzye - kangzye的博客")

/\*Rectangle类，添加自定义的DIV图层 ====================================================================  
\*author Karry@derbysoft.com  
\*继承自GOverlay();  
\*矩形是在地图上勾勒经纬边界的简单叠加层，其边界已指定  
\*粗细和颜色，并（可选）可以使用半透明背景颜色.  
\*/  
function Rectangle(point, opt\_boldWeight, opt\_color, opt\_weigth, opt\_height, opt\_bgColor) {  
    this.point_ = point;  
    this.boldWeight_ = opt\_boldWeight || 1;  
    this.color_ = opt\_color || "#888888";  
    this.weight_ = opt\_weigth || "50px";  
    this.height_ = opt\_height || "30px";  
    this.bgColor_ = opt\_bgColor || "#FFF";  
}  
Rectangle.prototype = new GOverlay();  
// 创建表示此矩形的 DIV.  
Rectangle.prototype.initialize = function(map) {  
    // 创建表示我们的矩形的 DIV  
    var div = document.createElement("div");  
    div.style.border = this.boldWeight_ + "px solid " + this.color\_;  
    div.style.position = "absolute";  
    div.style.background.style = "#FFF";  
    div.style.width = this.weight\_;  
    div.style.height = this.height\_;  
    div.style.background = this.bgColor\_;  
      // 与地图信息窗口相同，在地图的最上方  
    map.getPane(G\_MAP\_FLOAT\_PANE).appendChild(div);  
    $(div).html("&lt;input type=\\"button\\" value=\\"添加\\" /&gt;")  
    this.map_ = map;  
    this.div_ = div;  
}

//从地图面板删除 DIV  
Rectangle.prototype.remove = function() {  
    this.div\_.parentNode.removeChild(this.div\_);  
}

// 将我们的数据复制到新的矩形  
Rectangle.prototype.copy = function() {  
    return new Rectangle(this.point\_, this.boldWeight\_, this.color\_, this.weight\_, this.height\_, this.bgColor\_);  
}

// 基于当前投影和缩放级别重新绘制矩形  
Rectangle.prototype.redraw = function(force) {  
    // We only need to redraw if the coordinate system has changed  
    if (!force) return;  
      // 现在基于边界的 DIV 坐标放置 DIV  
    this.div\_.style.left = (this.point\_.x - this.boldWeight\_) + "px";  
    this.div\_.style.top = (this.point\_.y - this.boldWeight_ - 20) + "px";  
}  
/*  
\*Rectangle类结束============================================================================  
\*/

 

使用方式：

 map.addOverlay(new Rectangle(new GPoint(31.22395859822874,121.47857666015625)));