bdmap.js 5.14 KB
/**
* simple baidu map
* Code in 2014/10/14
* By van
*/

define( 'bdmap' ,function( require, exports, module ){

	require( 'fancybox' );

	var bdmap = function( target, options ){
		this.target = target;
		this.o = {
			container : "showmap", //显示地图容器ID
            sWidth: 290, //地址窗口宽度
            sHeight: 36, //地址窗口高度
			locate : "locate",  //获取地址数据的标签属性
			now: false, //设置即时显示在当前页面
            where: []  //立即生成传入的坐标
		}

		this.o = $.extend( this.o,options );

		this.init();
	};
	
	bdmap.prototype = {
		init: function(){
            //赋予地图对象
            window.map = window.map || new BMap.Map( this.o.container ),    // 创建Map实例
            window.geoc = window.geoc || new BMap.Geocoder(); //实例化地图解析器

			this.makeMap( [104.072236,30.663439] );
            this.setMap();
			//this.clickbtn();
			this.getfancy();
		},
		getfancy: function(){
			$( this.target ).fancybox({
				padding: 0
			});
		},
        setMap: function(){
            var _this = this, data, point;
			
			$( this.target ).click(function(){
                window.map.clearOverlays();
				
                data = _this.getData( $(this) );

				if ( _this.testZh( data[0] ) ){
					_this.getpoint(
						window.map,
						data
					);
				}else {
					point = new BMap.Point( data[0], data[1] );

					_this.getlocation(
						window.map,
						point
					);
				}
			});
           
        },
        makeMap: function( data ){
            var _this = this,
                point = new BMap.Point( data[0], data[1] );

	        window.map.centerAndZoom( point, 16 );  // 初始化地图,设置中心点坐标和地图级别
	        window.map.setCurrentCity("成都");          // 设置地图显示的城市 此项是必须设置的
	        window.map.enableScrollWheelZoom(true);  //设置鼠标缩放功能 
            window.map.addControl(
				new BMap.ScaleControl({
                    anchor: BMAP_ANCHOR_TOP_RIGHT
                })
            );  //添加缩放平移控件
			window.map.addControl(
                new BMap.NavigationControl({
                    anchor: BMAP_ANCHOR_TOP_RIGHT
                })
			);  //添加缩放平移控件
            //清空所有覆盖物
            window.map.clearOverlays();

            //依据参数立即生成覆盖层
            if ( _this.o.now && _this.o.where.length > 0 ){
                var searchInfoWindow = null;
                    searchInfoWindow = _this.openCover(
                        window.map,
                        _this.o.where
                    );
            };
        },
        getData: function( target ){
			//获取地址信息
            return locate = $( target )
                        .attr( this.o.locate )
                        .split( /[,;]/ );
        },
        testZh: function( o ){
            //检测传入字符中是否含有中文
            return /[\u4e00-\u9fa5]/.test( o );
        },
        getpoint: function( map, data ){
            //传入真实街道地址   
            var _this = this;

            return window.geoc.getPoint(
                    data[0],
                    function( point ){
                        map.centerAndZoom( point, 16 );
                        var searchInfoWindow = null;
                        searchInfoWindow = _this.openCover(
                            window.map,
                            [ data[0], data[1] ]
                        );
                        //添加标注并显示信息窗口
                        var marker = new BMap.Marker( point );
                        map.addOverlay( marker );
                        searchInfoWindow.open( marker );
                    }
                );
        },
        getlocation: function( map, point ){
            //传入百度坐标
            var _this = this;

            return window.geoc.getLocation(
                    point,
                    function( rs ){
                        var searchInfoWindow = null;
                        searchInfoWindow = _this.openCover(
                            map,
                            [ rs.address, rs.business ]
                        );
                        //添加标注并显示信息窗口
                        var marker = new BMap.Marker( point ); //创建marker对象
                        map.addOverlay( marker ); //在地图中添加marker
                        searchInfoWindow.open( marker );
                    }
                );
        },
        openCover: function( map, arr ){
            var _this = this;

            return new BMapLib.SearchInfoWindow(
                map,
                arr[0],
                {
                    title: arr[1],
                    width: _this.o.sWidth,
                    height: _this.o.sHeight,
                    panel: "panel",
                    enableAutoPan: true,
			        searchTypes   :[
				        BMAPLIB_TAB_SEARCH,   //周边检索
				        BMAPLIB_TAB_TO_HERE,  //到这里去
				        BMAPLIB_TAB_FROM_HERE //从这里出发
			        ]
                     
                }
            );
        }
	};

    return bdmap;
});