<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Control_Custom.aspx.cs"
Inherits="Samples.Controls.Control_Custom" %>
<!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 runat="server">
<title>自定义控件示例</title>
<style type="text/css">
#maps
{
height: 450px;
}
</style>
<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false&language=zh-CN"></script>
<script type="text/javascript" language="javascript">
var map;
var chicago = new google.maps.LatLng(41.850033, -87.6500523);
/** * The HomeControl adds a control to the map that simply * returns the user to Chicago. This constructor takes * the control DIV as an argument. */
//创建控件,显示文本为Home,Title 为 click to set the map to Home,点击控件时将地图中心设置为芝加哥
function HomeControl(controlDiv, map) {
controlDiv.style.padding = "5px";
var controlUI = document.createElement("DIV");
controlUI.style.backgroundColor = "white";
controlUI.style.borderStyle = "solid";
controlUI.style.borderWidth = "2px";
controlUI.style.cursor = "pointer";
controlUI.style.textAlign = "center";
controlUI.title = "click to set the map to Home";
controlDiv.appendChild(controlUI);
var controlText = document.createElement("DIV");
controlText.style.fontFamily = "Arial,sans-serif";
controlText.style.fontSize = "12px";
controlText.style.paddingLeft = "4px";
controlText.style.paddingRight = "4px";
controlText.innerHTML = "Home";
controlUI.appendChild(controlText);
google.maps.event.addDomListener(controlUI, 'click', function() {
map.setCenter(chicago); //绑定控件的Click事件,点击时设置地图中心为芝加哥
});
}
//地图初始化函数
function initialize() {
var mapDiv = document.getElementById("maps");
var Options = {
zoom: 12, center: chicago, mapTypeId: google.maps.MapTypeId.ROADMAP
}
map = new google.maps.Map(mapDiv, Options); //创建地图实例
var homeControlDiv = document.createElement("DIV"); //创建控件容器
var homeControl = new HomeControl(homeControlDiv, map); //创建控件并放置到容器中
homeControlDiv.index = 1; //设置容器的index属性
map.controls[google.maps.ControlPosition.TOP_RIGHT].push(homeControlDiv); //将装有自定义控件的容器放置在地图顶部右侧
}
google.maps.event.addDomListener(window, "load", initialize); //将地图初始化函数绑定到window的load事件
</script>
</head>
<body>
<form id="form1" runat="server">
<div id="maps">
</div>
</form>
</body>
</html>
引用请说明出处