<div id="allmap" style="width: 100%; height: 485px; vertical-align:top;">
</div>
<script type="text/javascript">
//拼接infowindow内容字串
var html = [];
var now = new Date();
var hours = now.getHours();
var year = (now.getYear() < 1900) ? (1900 + now.getYear()) : now.getYear();
var timeValue = hours >= 12 ? 2 : 1; //上下午
var nowTime = year + "/" + (now.getMonth() + 1) + "/" + now.getDate();当前日期
// 百度地图API功能
var map = new BMap.Map("allmap");
var myIcon = new BMap.Icon("../Images/hotel3.png", new BMap.Size(25, 24)); // 编写自定义函数,创建标注
function addMarker(point, ImageURL, linkUrl,strSXW,strYear) {
if (strSXW == timeValue && strYear == nowTime) {
map.centerAndZoom(point, 15);
}
myIcon = new BMap.Icon(ImageURL, new BMap.Size(25, 24));
var marker = new BMap.Marker(point, { icon: myIcon }); // 创建标注
map.addOverlay(marker);
var infoWin = new BMap.InfoWindow(html.join(""), { offset: new BMap.Size(0, -10) });
marker.addEventListener("mouseover", function () { this.openInfoWindow(infoWin); });
marker.addEventListener("mouseout", function () { this.openInfoWindow(‘‘); });
marker.addEventListener("click", function () { window.location.href = linkUrl; }); }
//地图事件设置函数:
function setMapEvent() {
map.enableDragging(); //启用地图拖拽事件,默认启用(可不写)
map.enableScrollWheelZoom(); //启用地图滚轮放大缩小
map.enableDoubleClickZoom(); //启用鼠标双击放大,默认启用(可不写)
map.enableKeyboard(); //启用键盘上下左右键移动地图
}
//地图控件添加函数:
function addMapControl() { //向地图中添加缩放控件
var ctrl_nav = new BMap.NavigationControl({ anchor: BMAP_ANCHOR_TOP_LEFT, type: BMAP_NAVIGATION_CONTROL_LARGE });
map.addControl(ctrl_nav); //向地图中添加缩略图控件
var ctrl_ove = new BMap.OverviewMapControl({ anchor: BMAP_ANCHOR_BOTTOM_RIGHT, isOpen: 1 });
map.addControl(ctrl_ove); //向地图中添加比例尺控件
var ctrl_sca = new BMap.ScaleControl({ anchor: BMAP_ANCHOR_BOTTOM_LEFT });
map.addControl(ctrl_sca);
}
function InitMapInfo() { //result为空时初始化地图,避免地图显示空白
var point = new BMap.Point("116.403857", "39.915144"); // 创建点坐标
map.centerAndZoom(point, 15); // 初始化地图,设置中心点坐标和地图级别。
setMapEvent();
addMapControl();
}
function InitMap(result) { //result不为空时初始化地图
setMapEvent();
addMapControl();
var str = new Array();
str = result.split("###");
for (var i = 0; i < str.length; i++) {
var pinfo = new Array();
pinfo = str[i].split("、");
html = [];
html.push(‘<table border="0" cellpadding="2" cellspacing="2" width="350px" >‘);
html.push(‘ <tr>‘);
html.push(‘ <td align="right" valign="top" style="width:100px">工程名称:</td>‘);
html.push(‘ <td>‘ + pinfo[0] + ‘</td>‘);
html.push(‘ </tr>‘); html.push(‘ <tr>‘);
html.push(‘ <td align="right" valign="top">质量监督注册号:</td>‘);
html.push(‘ <td>‘ + pinfo[1] + ‘</td>‘);
html.push(‘ </tr>‘);
html.push(‘ <tr>‘);
html.push(‘ <td align="right" valign="top">建设地点:</td>‘);
html.push(‘ <td>‘ + pinfo[2] + ‘</td>‘);
html.push(‘ </tr>‘);
html.push(‘ <tr>‘);
html.push(‘ <td align="right" valign="top">工程类别:</td>‘);
html.push(‘ <td>‘ + pinfo[8] + ‘</td>‘);
html.push(‘ </tr>‘);
html.push(‘ <tr>‘);
html.push(‘ <td align="right" valign="top">施工单位:</td>‘);
html.push(‘ <td>‘ + pinfo[5] + ‘</td>‘);
html.push(‘ </tr>‘);
html.push(‘ <tr>‘);
html.push(‘ <td align="right" valign="top">负责人:</td>‘);
html.push(‘ <td>‘ + pinfo[6] + ‘</td>‘);
html.push(‘ </tr>‘);
html.push(‘ <tr>‘);
html.push(‘ <td align="right" valign="top">联系电话:</td>‘);
html.push(‘ <td>‘ + pinfo[7] + ‘</td>‘);
html.push(‘ </tr>‘);
html.push(‘ <tr>‘);
html.push(‘ <td align="right" valign="top">监督小组:</td>‘);
html.push(‘ <td>‘ + pinfo[9] + ‘</td>‘);
html.push(‘ </tr>‘);
html.push(‘ <tr>‘);
html.push(‘ <td align="right" valign="top">监督组员:</td>‘);
html.push(‘ <td>‘ + pinfo[15].replaceAll(‘_‘, ‘、‘) + ‘</td>‘);
html.push(‘ </tr>‘);
html.push(‘</table>‘);
var point = new BMap.Point("116.403857", "39.915144");
if (pinfo[3] != "0" && pinfo[4] != "0") {
point = new BMap.Point(pinfo[3], pinfo[4]);
}
var imageUrl = "../Images/hotel3.png";
if (pinfo[10] == timeValue && pinfo[11] == nowTime) {
imageUrl = "../Images/hotel2.png";
}
var linkUrl = ‘../Officer/PlanProject.aspx?ProjectID=‘ + pinfo[13];
addMarker(point, imageUrl, linkUrl, pinfo[10], pinfo[11]);
}
}
</script>
原文:http://www.cnblogs.com/fqs123456/p/3796040.html