var lastSetBackObj = null;
//创建和初始化地图函数:
function initMap() {
window.map = new BMap.Map("container");
map.centerAndZoom(new BMap.Point(114.288564, 30.608811), 15);
var data = MyMap.GetAllMap().value;
if (data != "" && data != null && typeof (data) == "object" && data.Rows.length > 0) {
addMarker(data); //向地图中添加marker
}
map.addControl(new BMap.NavigationControl());
map.addControl(new BMap.ScaleControl());
map.addControl(new BMap.OverviewMapControl());
map.enableScrollWheelZoom(); // 启用滚轮放大缩小。
map.enableKeyboard(); // 启用键盘操作。
}
initMap(); //创建和初始化地图
//显示所有数据
function ShowAll() {
addMarker(MyMap.GetAllMap().value);
}
function searchMap() {
try {
var tShow = document.getElementById("keyword").value;
if (trim(tShow) != "" && trim(tShow) != null) {
document.getElementById("show1").checked = "checked";
document.getElementById("show2").checked = "";
addMarker(MyMap.GetNowMap(tShow).value);
}
else {
document.getElementById("show1").checked = "";
document.getElementById("show2").checked = "checked";
}
}
catch (e)
{ }
}
//去空格
function trim(str) {
return str.replace(/(^\s*)|(\s*$)/g, "");
}
//创建marker
function addMarker(data) {
map.clearOverlays();
for (var i = 0; i < data.Rows.length; i++) {
var json = data.Rows[i];
var p0 = json.mLng;
var p1 = json.mLat;
var point = new BMap.Point(p0, p1);
var marker = new BMap.Marker(point);
var label = new BMap.Label(json.mName, { "offset": new BMap.Size(9, -20) });
marker.setLabel(label);
map.addOverlay(marker);
if (json.mFlag == "1") {
marker.setAnimation(BMAP_ANIMATION_BOUNCE); //跳动的动画
}
label.setStyle({
borderColor: "#808080",
color: "#333",
cursor: "pointer"
});
(function() {
var _json = json;
var _iw = createInfoWindow(_json, i);
var _marker = marker;
_marker.addEventListener("click", function() {
this.openInfoWindow(_iw);
});
_iw.addEventListener("open", function() {
_marker.getLabel().hide();
})
_iw.addEventListener("close", function() {
_marker.getLabel().show();
})
label.addEventListener("click", function() {
_marker.openInfoWindow(_iw);
})
if (!!json.isOpen) {
label.hide();
_marker.openInfoWindow(_iw);
}
})()
}
}
//创建InfoWindow
function createInfoWindow(json, tIndex) {
var tWarm = "";
var tState = "";
if (json.mFlag == "1") {
tWarm = "报警";
tState = "red";
}
else {
tWarm = "正常";
tState = "green";
}
var sContent =
"<h4 style='margin:0 0 5px 0;padding:0.2em 0'>" + json.mName + "</h4>" +
"<a href='http://www.baidu.com'><img border='0' style='float:right;margin:4px' id='imgDemo" + tIndex + "' src='MapImages/123.jpg' width='139' height='104' title='点击查看详情'/></a>" +
"<p style='margin:0;line-height:1.5;font-size:12px;text-indent:2em;width:350px'>" + json.mContent + "</p>" +
"<p style='margin:0;line-height:1.5;font-size:12px;text-indent:2em;color:" + tState + "'>状态:" + tWarm + "</p>";
var iw = new BMap.InfoWindow(sContent);
return iw;
}
//*********************************************地点查询*************************************************
//查询所有地点
function searchAll() {
var local = new BMap.LocalSearch(map, {
renderOptions: { map: map, panel: "results" }
});
local.search(trim(document.getElementById("Text1").value));
}
//*********************************************公交查询*************************************************
//查询公交线路
function searchBus() {
var transit = new BMap.TransitRoute("武汉市");
transit.setSearchCompleteCallback(function(results) {
if (transit.getStatus() == BMAP_STATUS_SUCCESS) {
for (var index = 0; index < results.getNumPlans(); index++) {
var showElement = document.createElement("div");
var callDLFunc = drawLine(map, results, index, showElement);
showElement.style.lineHeight = "20px";
showElement.onclick = callDLFunc;
showElement.innerHTML = (index + 1) + ". " + results.getPlan(index).getDescription();
document.getElementById("results").appendChild(showElement);
if (index == 0) { showElement.onclick(); }
}
}
})
transit.search("五里新村", "武汉图书馆");
}
//绘制线路
function drawLine(aMap, results, index, obj) {
return function() {
var opacity = 0.45;
var planObj = results.getPlan(index);
var bounds = new Array();
var addMarkerFun = function(point, imgType, index, title) {
var url;
var width;
var height
var myIcon;
// imgType:1的场合,为起点和终点的图;2的场合为过程的图形
if (imgType == 1) {
url = "http://openapi.baidu.com/map/images/dest_markers.png";
width = 42;
height = 34;
myIcon = new BMap.Icon(url, new BMap.Size(width, height), { offset: new BMap.Size(14, 32), imageOffset: new BMap.Size(0, 0 - index * height) });
} else {
url = "http://openapi.baidu.com/map/images/trans_icons.png";
width = 22;
height = 25;
var d = 25;
var cha = 0;
var jia = 0
if (index == 2) {
d = 21;
cha = 5;
jia = 1;
}
myIcon = new BMap.Icon(url, new BMap.Size(width, d), { offset: new BMap.Size(10, (11 + jia)), imageOffset: new BMap.Size(0, 0 - index * height - cha) });
}
var marker = new BMap.Marker(point, { icon: myIcon });
if (title != null && title != "") {
marker.setTitle(title);
}
// 起点和终点放在最上面
if (imgType == 1) {
marker.setTop(true);
}
aMap.addOverlay(marker);
}
var addPoints = function(points) {
for (var i = 0; i < points.length; i++) {
bounds.push(points[i]);
}
}
// 清空覆盖物
aMap.clearOverlays();
// 绘制驾车步行线路
for (var i = 0; i < planObj.getNumRoutes(); i++) {
var route = planObj.getRoute(i);
if (route.getDistance(false) > 0) {
// 步行线路有可能为0
aMap.addOverlay(new BMap.Polyline(route.getPath(), { strokeStyle: "dashed", strokeColor: "#30a208", strokeOpacity: 0.75, strokeWeight: 4, enableMassClear: true }));
}
}
// 绘制公交线路
for (i = 0; i < planObj.getNumLines(); i++) {
var line = planObj.getLine(i);
addPoints(line.getPath());
// 公交
if (line.type == BMAP_LINE_TYPE_BUS) {
// 上车
addMarkerFun(line.getGetOnStop().point, 2, 2, line.getGetOnStop().title);
// 下车
addMarkerFun(line.getGetOffStop().point, 2, 2, line.getGetOffSt
- 1
- 2
- 3
- 4
- 5
前往页