let BMapModule = (function () {
let map = null;
function isBMapAvailable() {
return typeof BMap !== 'undefined';
}
function alertMapCenter() {
let what = map.getCenter();
alert(what.lng + '/' + what.lat);
}
function initMap() {
if (!isBMapAvailable()) {
alert("Baidu map api module is not available.")
return;
}
//! 初始化地图基础信息
map = new BMap.Map("map_container"
, { mapType: BMAP_NORMAL_MAP }
, { enableHighResolution: false}
, { enableAutoResize: false }
, { enableMapClick: true }
);
map.enableDragging();
map.enableScrollWheelZoom();
map.enableDoubleClickZoom();
map.enableKeyboard();
map.disableInertialDragging();
map.disableContinuousZoom();
map.enablePinchToZoom();
// map.enableAutoResize();
// map.highResolutionEnabled();
map.setDefaultCursor("default");
map.setMinZoom(6);
map.setMaxZoom(9);
// let bounds = map.getBounds();
// let sw = bounds.getSouthWest();
// let ne = bounds.getNorthEast();
//
// let size = map.getSize();
// let w = size.width;
// let h = size.height;
// let points = [
// new BMap.Point(116.404, 39.915),
// new BMap.Point(116.418, 39.921),
// new BMap.Point(116.386, 39.942)
// ];
// let viewport = map.getViewport(points
// // , { enableAnimation: false }
// , { margins: [30, 20, 0, 20] }
// , { zoomFactor: -3 }
// // , { delay: 500 }
// );
// map.centerAndZoom(viewport.center, viewport.zoom)
// points.forEach(function (p) {
// let marker = new BMap.Marker(p);
// map.addOverlay(marker);
// });
let p = new BMap.Point(117.03, 25.08);
map.centerAndZoom(p, 9);
let marker = new BMap.Marker(p);
map.addOverlay(marker);
map.setZoom(9);
// map.zoomIn();
// for (let i = 0; i < 4; ++i) {
// map.zoomOut();
// }
// // 添加热区
// let hotspotPoint = new BMap.Point(117.03, 25.08);
// let hotspotOptions = {
// text: "这是一个热区",
// // offsets: [10, 10, 10, 10], // 上、右、下、左的扩展偏移值
// // userData: { key: "value" }, // 自定义数据
// // minZoom: 10,
// // maxZoom: 18
// };
// let hotspot = new BMap.Hotspot(hotspotPoint, hotspotOptions);
// map.addHotspot(hotspot);
map.addControl(new BMap.ScaleControl({ anchor: BMAP_ANCHOR_BOTTOM_RIGHT }));
map.addControl(new BMap.MapTypeControl({ anchor: BMAP_ANCHOR_BOTTOM_RIGHT, offset: new BMap.Size(0, 10) }));
return map;
}
map = initMap();
function getMap() {
return map;
}
function clearMap() {
if (!isBMapAvailable()) {
alert("Baidu map api module is not available.")
return;
}
map.clearOverlays();
}
return {
GetMap : getMap,
ClearMap : clearMap
}
})();
JS模块化语法示例
最新推荐文章于 2024-08-08 07:45:15 发布