<!DOCTYPE html
PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN”
“http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd“>
<html xmlns=”http://www.w3.org/1999/xhtml“>
<head>
<meta http-equiv=”content-type” content=”text/html; charset=utf-8″/>
<title>Google Maps JavaScript API Example</title>
<style type=”text/css”>
*{margin:0;padding:0;}
.market{background:#f30;color:#fff;border:3px solid #fcc;padding:10px;}
</style>
<script src=”http://maps.google.com/maps?file=api&v=2&key=abcdefg” type=”text/javascript”></script>
<script type=”text/javascript”>
google.maps.FocusMarker = function(latlng, opt){
this.latlng = latlng;
this.innerHtml = opt.innerHtml || ”;
this.className = opt.className || ”;
this.css = opt.css || {};
this.id = opt.id || ”;
}
google.maps.FocusMarker.prototype = new google.maps.Overlay();
google.maps.FocusMarker.prototype.initialize = function(map){
// 创建用于表示该矩形区域的 DIV 元素
var div = document.createElement(“div”);
div.id = this.id || ”;
div.style.width = this.css.width || ‘auto’;
div.className = this.className;
div.style.border = this.css.border || “none”;
div.style.color = this.css.color || “#ffffff”;
div.style.backgroundColor = this.css.backgroundColor || “”;
div.style.position = this.css.position || “absolute”;
div.style.textAlign= this.css.textAlign || “center”;
div.style.padding= this.css.padding || “0px 0px 0px 0px”;
div.style.fontSize = this.css.fontSize || “12px”;
div.style.height = this.css.height || “60px”;
div.style.cursor = this.css.cursor || “pointer”;
div.style.whiteSpace= this.css.whiteSpace || “nowrap”;
var c = map.fromLatLngToDivPixel(this.latlng);
div.style.left = c.x+”px”;
div.style.top = c.y+”px”;
div.innerHTML = this.innerHtml;
// 我们希望将覆盖物紧贴于地图之上,因此我们把它放置在 Z 序最小的 G_MAP_MAP_PANE 层,
// 事实上,这也是地图本身的 Z 顺序,即在标注的影子之下
map.getPane(G_MAP_MAP_PANE).appendChild(div);
this.map = map;
this.container = div;
}
google.maps.FocusMarker.prototype.remove = function()
{
this.container.parentNode.removeChild(this.container);
}
google.maps.FocusMarker.prototype.redraw = function(force)
{
// 只有当坐标系改变时,我们才需要重绘
if (!force) return;
// 根据当前显示区域的经纬度坐标,计算 DIV 元素的左上角和右下角的像素坐标
var c = this.map.fromLatLngToDivPixel(this.latlng);
// 根据计算得到的坐标放置我们的 DIV 元素
this.container.style.left = c.x + “px”;
this.container.style.top = c.y + “px”;
// this.div_.style.width = “auto”;
}
function load() {
if (GBrowserIsCompatible()) {
var map = new GMap2(document.getElementById(“map”));
map.addControl(new GSmallMapControl());
map.addControl(new GScaleControl());
map.setCenter(new GLatLng(39.917, 116.397), 14);
map.addOverlay(new google.maps.FocusMarker(new GLatLng(39.917,116.397),{
innerHtml : ‘<div class=”market”>自定义信息内容</div>’
}));
}
}
</script>
</head>
<body onload=”load()” onunload=”GUnload()”>
<div id=”map” style=”width: 500px; height: 300px”></div>
</body>
</html>