var DEF_PI = Math.PI; // 3.14159265359
var DEF_2PI = 2 * Math.PI; // 6.28318530712
var DEF_PI180 = Math.PI / 180.0; // 0.01745329252
var DEF_R = 6370996.81; // radius of earth
function initMap(mapId, ptLng, ptLat, zoom, overviewMap, mapType) {
var point = new BMap.Point(ptLng, ptLat);
createMap(mapId, point, zoom); //创建地图
setMapEvent(); //设置地图事件
addMapControl(overviewMap, mapType, point, 1); //向地图添加控件
}
function initMapByCityName(mapId, cityName, zoom, overviewMap, mapType) {
createMap(mapId, cityName, zoom); //创建地图
setMapEvent(); //设置地图事件
addMapControl(overviewMap, mapType, cityName, 0); //向地图添加控件
}
// 利用弧夹角方式计算地图上两点之间的距离,和百度地图计算结果不一致
function GetShortDistance(lon1, lat1, lon2, lat2) {
var ew1, ns1, ew2, ns2;
var dx, dy, dew;
var distance;
// 角度转换为弧度
ew1 = lon1 * DEF_PI180;
ns1 = lat1 * DEF_PI180;
ew2 = lon2 * DEF_PI180;
ns2 = lat2 * DEF_PI180;
// 经度差
dew = ew1 - ew2;
// 若跨东经和西经180 度,进行调整
if (dew > DEF_PI)
dew = DEF_2PI - dew;
else if (dew < -DEF_PI)
dew = DEF_2PI + dew;
dx = DEF_R * Math.cos(ns1) * dew; // 东西方向长度(在纬度圈上的投影长度)
dy = DEF_R * (ns1 - ns2); // 南北方向长度(在经度圈上的投影长度)
// 勾股定理求斜边长
distance = Math.sqrt(dx * dx + dy * dy);
return distance;
}
// 利用弧夹角方式计算地图上两点之间的距离,与百度方式一致
function GetLongDistance(lon1, lat1, lon2, lat2) {
var ew1, ns1, ew2, ns2;
var distance;
// 角度转换为弧度
ew1 = lon1 * DEF_PI180;
ns1 = lat1 * DEF_PI180;
ew2 = lon2 * DEF_PI180;
ns2 = lat2 * DEF_PI180;
// 求大圆劣弧与球心所夹的角(弧度)
distance = Math.sin(ns1) * Math.sin(ns2) + Math.cos(ns1) * Math.cos(ns2) * Math.cos(ew1 - ew2);
// 调整到[-1..1]范围内,避免溢出
if (distance > 1.0)
distance = 1.0;
else if (distance < -1.0)
distance = -1.0;
// 求大圆劣弧长度
distance = DEF_R * Math.acos(distance);
return distance;
}
// 画线
function DrawLines(types, names, lons, lats) {
var points = [];
for (i = 0; i < lons.length; i++) {
var point = new BMap.Point(lons[i], lats[i]);
points.push(point);
var _staContent = names[i].split("|");
var sContent = "<div>"
+ "<h4 style='margin:0 0 5px 0;padding:0.2em 0'>"
+ _staContent[0]
+ "</h4>"
+ "<p style=''float:right;margin:0;line-height:1.5;font-size:13px;text-indent:2em'>"
+ "预计到站时间:" + _staContent[1]
+ "</p>"
+ "<p style=''float:right;margin:0;line-height:1.5;font-size:13px;text-indent:2em'>"
+ "备注:" + _staContent[2] + "</p>" + "</div>";
var staNameLabel = new BMap.Label(_staContent[0], {
offset: new BMap.Size(20, -10)
}); // 标注显示名称
// 设置label 文本框的样式
staNameLabel.setStyle({
"borderColor": "red",
"color": "red",
"cursor": "pointer"
});
if (types[i] == "1") {
var marker = createMark(point, sContent);
map.addOverlay(marker); // 将标注添加到地图中
marker.setLabel(staNameLabel); // 添加百度label
}
}
var polyline = new BMap.Polyline(points, { strokeColor: "blue", strokeWeight: 4, strokeOpacity: 0.5 });
map.addOverlay(polyline);
}
// 设置鼠标获取地图上点的样式
function SetMouseCursor() {
var drawingManager = new BMapLib.DrawingManager(map, {
isOpen: true, //是否开启绘制模式
enableDrawingTool: false, //是否显示工具栏
drawingToolOptions: {
anchor: BMAP_ANCHOR_TOP_RIGHT, //位置
offset: new BMap.Size(5, 5), //偏离值
scale: 0.8, //工具栏缩放比例
drawingModes: [BMAP_DRAWING_CIRCLE]
}
});
}
//---内部调用方法--begin--//
//创建地图函数:
function createMap(mapId, pointOrName, zoom) {
var map = new BMap.Map(mapId); //在百度地图容器中创建一个地图
map.centerAndZoom(pointOrName, zoom); //设定地图的中心点和坐标并将地图显示在地图容器中
window.map = map; //将map变量存储在全局
}
//地图事件设置函数:
function setMapEvent() {
map.enableDragging(); //启用地图拖拽事件,默认启用(可不写)
map.enableScrollWheelZoom(true); //启用地图滚轮放大缩小
map.enableDoubleClickZoom(); //启用鼠标双击放大,默认启用(可不写)
map.enableKeyboard(); //启用键盘上下左右键移动地图
map.addEventListener("click", showInfo);
}
//地图控件添加函数:
function addMapControl(overviewMap, mapType, point, isPoint) {
//向地图中添加缩放控件
map.addControl(new BMap.NavigationControl({ anchor: BMAP_ANCHOR_TOP_LEFT, type: BMAP_NAVIGATION_CONTROL_LARGE, showZoomInfo: true }));
//向地图中添加比例尺控件
map.addControl(new BMap.ScaleControl({ anchor: BMAP_ANCHOR_BOTTOM_LEFT }));
//为地图添加鹰眼控件
if (overviewMap == 1) {
map.addControl(new BMap.OverviewMapControl({ isOpen: true, anchor: BMAP_ANCHOR_BOTTOM_RIGHT }));
}
//为地图添加2D3D切换控件
if (mapType == 1) {
map.addControl(new BMap.MapTypeControl());
if (isPoint == 1) {
var gc = new BMap.Geocoder();
gc.getLocation(point, function (rs) {
var addComp = rs.addressComponents;
map.setCurrentCity(addComp.city);
});
}
else {
map.setCurrentCity(point);
}
}
map.enableScrollWheelZoom();
map.enableAutoResize();
}
//设置标注点图片
var createMark = function (point, info_html) {
var _icon = new BMap.Icon('../../images/node.png', new BMap.Size(11, 11), { anchor: new BMap.Size(5, 5) });
var _marker = new BMap.Marker(point, { icon: _icon });
_marker.addEventListener("click", function (e) {
this.openInfoWindow(new BMap.InfoWindow(info_html));
});
return _marker;
}
//---内部调用方法--end--//