busline

;(function(){
    var MapShare = App.MapShare, mapView= MapShare.mapView,
        detailView= MapShare.detailView,
        searchView= MapShare.searchView,
        // locationModel=MapShare.locationModel,
        routPathView = null, //来自后面实例化的。。。
        configLan=MapShare.configLan,
        topHeight=MapShare.topHeight;
    var AMap = window.AMap;    
    
    var loadingMov = {
        loadingBegin : App.loadingBegin,
        loadingFinish : App.loadingFinish
    };  
    var hasResulted = false;  
    var mapObj=mapView.getMapObj();    
    var SearchPathModel = PackTools.Model.extend({
        defaults: {
            busInfo: {start: {name:'', x:0, y:0}, end: {name:'', x:0, y:0}},
            busSearchOpt02:{
                type: 0x02,
                batch: 1,
                number: 100
            },
            autoCompleterOpt:{
                range: 10000,
                sort: 0,
                retvalue: 1
            }
        }
    })
    var searchPathModel=new SearchPathModel();
    var SearchPathView = PackTools.View.extend({
        el: $("#querypath"),    
        model: searchPathModel,
        initialize : function() {
            var $root = this.el;
            var $busList = $("#buslist");
            _.extend(this,{
                $root : $root,
                $startPlace : $root.find("#startPlace"),    //起点节点
                $endPlace : $root.find("#endPlace"),        //终点节点
                $searchBusLineBtn : $root.find(".pathform-seach-transit"),  //查询公交的按钮
                $switchBtn : $root.find(".exchange_key"),         //切换起点和终点的按钮
                $searchSelfDriving : $root.find(".pathform-seach-selfdriving"),                
                topbarback : $root.find('.position_box p'),
                
                $busList : $busList,
                topbar:$busList.find('.position_box'),
                busSearchBar : $busList.find('#busSearchBar'),
                $listWrap : $busList.find("#listWrap"),
                busBarPan: $busList.find("#busBarPan"),
                $busStartTip : $busList.find("#busBarPan #startPlace"), //$busList.find(".bus-position-qi"),
                $busEndTip : $busList.find('#busBarPan #endPlace'), //$busList.find(".bus-position-zhong"),
                $busItemContainer : $busList.find(".bus-list"),
                listTopbarback : $busList.find('.position_box p')
            });
            this.changeToCarBtn = this.topbar.find('.by_way ul li').eq(1);
            
            // this.busSearchBar = this.$listWrap.find('#busSearchBar');
            var view = this;
            this.changeToCarBtn.click(function(){
                var busInfo = view.model.get('busInfo');
                var startXY = new AMap.LngLat(busInfo.start.x, busInfo.start.y),
                    endXY = new AMap.LngLat(busInfo.end.x, busInfo.end.y);
                searchPathView.el.hide();
                searchPathView.$busList.hide();
                if(!routPathView) routPathView = MapShare.routPathView;
                routPathView.getRouteDetails(startXY, endXY, busInfo);
            });
            
            var spans = this.busSearchBar.find('span.text');
            for(var i=0,len=spans.length; i<len; i++){
                spans.eq(i).tap((function(i){
                    return function(){
                        spans.removeClass('current');
                        if(i==0){
                            $(this).addClass('current');
                            searchPathView.model.set('busSearchOpt02',{
                                type: 0x00,
                                batch: 1,
                                number: 100
                            })
                            searchPathView.searchBusLine();
                        }else if(i==1){
                            $(this).addClass('current');
                            searchPathView.model.set('busSearchOpt02',{
                                type: 0x02,
                                batch: 1,
                                number: 100
                            })
                            searchPathView.searchBusLine();
                        }else if(i==2){
                            $(this).addClass('current');
                            searchPathView.model.set('busSearchOpt02',{
                                type: 0x03,
                                batch: 1,
                                number: 100
                            })
                            searchPathView.searchBusLine();
                        }else if(i==3){
                            $(this).addClass('current');
                            searchPathView.model.set('busSearchOpt02',{
                                type: 0x01,
                                batch: 1,
                                number: 100
                            })
                            searchPathView.searchBusLine();
                        }
                    }
                })(i));
            }
            
            var busInfo = view.model.get('busInfo');
            this.topbarback.tap(function(){
                hasResulted = false;
                if(detailView.model.get('pageFrom')==detailView.name){
                     view.el.hide();
                     detailView.el.show();
                     //uPAResizePage();
                }else{
                     searchPathView.backToSearchMain();
                }
                
            });
            this.$searchSelfDriving.on("click", function(e){
                if(!view.$startPlace.text()){
                    mapView.showMapTip(configLan.Tip, configLan.putInStartPlace);
                    return;
                }
                if(!view.$endPlace.text()){
                    mapView.showMapTip(configLan.Tip, configLan.putInEndPlace);
                    return;
                }
                reviseBusInfo(busInfo.start);
                var startXY = new AMap.LngLat(busInfo.start.x, busInfo.start.y),
                    endXY = new AMap.LngLat(busInfo.end.x, busInfo.end.y);
                searchPathView.el.hide();
                if(!routPathView) routPathView = MapShare.routPathView;
                routPathView.getRouteDetails(startXY, endXY, busInfo);
            });
            //切换起点和终点
            this.$switchBtn.on("click", function(e){
                if(!view.$startPlace.text()){
                    mapView.showMapTip(configLan.Tip, configLan.putInStartPlace);
                    return;
                }
                if(!view.$endPlace.text()){
                    mapView.showMapTip(configLan.Tip, configLan.putInEndPlace);
                    return;
                }
                var tempVal = view.$startPlace.text();
                view.$startPlace.text(view.$endPlace.text());
                view.$endPlace.text(tempVal);
                reviseBusInfo(busInfo.start);
                tempVal = busInfo.start; // MapShare.clone
                busInfo.start = busInfo.end;
                busInfo.end = tempVal;
                return false;
            });
            
            //查询公交路线
            this.$searchBusLineBtn.on("click", function() {
                if(!view.$startPlace.text()){
                    mapView.showMapTip(configLan.Tip, configLan.putInStartPlace);
                    return;
                }
                if(!view.$endPlace.text()){
                    mapView.showMapTip(configLan.Tip, configLan.putInEndPlace);
                    return;
                }
                searchPathView.model.set('busSearchOpt02',{
                    type: 0x02,
                    batch: 1,
                    number: 100
                })
                searchPathView.searchBusLine();
            });//查询公交路线
            
            this.listTopbarback.tap(function(){                
                searchPathView.showSearchBus();
            });
            /* 暂时失效
            var isInList = true;
            this.$busItemContainer.on("click", ".bus-item", function(e) {
                var $target = $(e.currentTarget), lineData = JSON.parse($target.attr("lineData"));
                //console.dir(lineData);
                isInList = false;
                hasResulted = false;
                searchPathView.showSearchMap(lineData);
            });
            */
        }, //initial
        setSearchInput : function(start, end){
            //start:起点名称和位置 {name:'',x:number经度, y:number:纬度}, end终点的
            var busInfo = searchPathView.model.get('busInfo');
            if(start){
                busInfo.start = {
                    name:start.name,
                    x:start.x,
                    y:start.y
                };
                this.$startPlace.text(start.name);
            }else{
                this.$startPlace.text('');
            }
            if(end){
                busInfo.end = {
                    name:end.name,
                    x:end.x,
                    y:end.y
                }
                this.$endPlace.text(end.name);
            }else{
                this.$endPlace.text('');
            }
            this.el.show();
            //uPAResizePage();
        },
        backToSearchMain : function(){
            // mapView.getMapObj().clearMap();
            mapView.showCenterMap();
            searchPathView.el.hide();
            searchPathView.$busList.hide();
            mapView.el.show();    
            //uPAResizePage();
        },
        searchBusLine : function(){
            loadingMov.loadingBegin();
            var busInfo = searchPathView.model.get('busInfo');
            reviseBusInfo(busInfo.start);
            
            var startXY = new AMap.LngLat(busInfo.start.x, busInfo.start.y),
                endXY = new AMap.LngLat(busInfo.end.x, busInfo.end.y),
                busSearcher = new AMap.BusSearch(searchPathView.model.get('busSearchOpt02')),
                city = mapView.model.city || '';
            
            searchPathView.$busStartTip.html(busInfo.start.name);
            searchPathView.$busEndTip.html(busInfo.end.name);
            searchPathView.$busItemContainer.empty();
            busSearcher.byTwoPoi([startXY, endXY], city, function(data) {
                var list = data.list, line = [], currPaths, items = [], currItem, title, itemPath, content;
                if(list && list.length) {
                    for(var i = 0, len = list.length; i < len; i ++) {
                        currItem = $('<div class="bus-item"/>');
                        title = $('<div class="bus-item-title"/>');
                        content = $('<div class="bus-item-content"/>');
                        itemPath = $('<div class="bus-item-path"/>');
                        line = list[i];
                        currPaths = line.segmentList;
                        title.html('<p class="bus-item-no">' +
                        leftPad("0", 2, ""+(i+1))
                        +'</p>');
                        for(var j = 0, jLen = currPaths.length; j < jLen; j ++) {
                            itemPath.append('<p class="bus-item-path-text">' +
                                currPaths[j].busName
                            +'</p>\
                            <p class="bus-item-path-remark">' +
                                currPaths[j].startName+' '+currPaths[j].passDepotName+' '+currPaths[j].endName
                            + '</p>');
                        }
                        title.append(itemPath).append('<p class="bus-item-icon">&nbsp;</p>');
                        currItem.append(title).append(content);
                        items.push(currItem[0]);
                        currItem.attr("lineData", JSON.stringify(line)); //attr
                    }
                    searchPathView.$busItemContainer.append(items);
                }
                
                // searchPathView.$busList.css({
                    // position:'relative',
                    // top: 'auto'
                // });
                // $root.hide();
                // $busList.show();
                // uPAResizePage();
                searchPathView.showSearchList();
                loadingMov.loadingFinish();
            }); //byTwoPoi    
        },
        showSearchMap : function(lineData){
            searchPathView.$busList.css({
                position:'absolute',
                top: topHeight+'px',
                'z-index': 501
            });    
            
            if(!hasResulted){
               mapObj.clearMap();
               searchPathView.drawBusLine(lineData);
               hasResulted = true;
            }
            mapView.showCenterMap(true);
            // searchPathView.el.hide();
            searchPathView.busSearchBar.hide();
            searchPathView.$listWrap.hide();
            mapView.el.show();    
            searchPathView.busBarPan.css({
                position:'absolute',
                'z-index': 501,
                'margin-top': mapView.el.height()-searchPathView.topbar.height()-searchPathView.busBarPan.height()+'px'
            });
            //162 底部栏高度不知为何得不到
            //uPAResizePage();
        },
        showSearchList : function(){
            searchPathView.$busList.css({
                position:'relative',
                top: 'auto'
            });    
            searchPathView.$root.hide();
            searchPathView.busSearchBar.show();
            searchPathView.$listWrap.show();
            mapView.el.hide();  
            searchPathView.$busList.show();  
            searchPathView.busBarPan.css({
                position:'relative',
                'margin-top':'0px'
            });
            //uPAResizePage();
            // $busList.hide();
            // mapView.showCenterMap();
            // mapView.el.show();    
            // searchPathView.$root.hide();
            // uPAResizePage();
        },
        showSearchBus : function(){
            searchPathView.$busList.hide();
            // mapView.showCenterMap();
            mapView.el.hide();    
            searchPathView.$root.show();
            //uPAResizePage();
        },
        /**
         * 画地图上的线的方法
         * @param lineData 数据
         */
        drawBusLine:function(lineData){
            var lineList = lineData.segmentList, aLine, tempBusXY,
                busXY = [],     //公交经过的线路的经纬度数组
                tempBusXY_xy,
                view=this;
            if(lineList && lineList.length) {
                for(var i = 0, iLen = lineList.length; i < iLen; i ++) {
                    aLine = lineList[i];
                    tempBusXY = aLine.coordinateList.split(";");
                    for(var j = 0, jLen = tempBusXY.length; j < jLen; j ++) {
                        tempBusXY_xy = tempBusXY[j].split(',')
                        busXY.push(new AMap.LngLat(tempBusXY_xy[0], tempBusXY_xy[1]));
                        if(j === 0 || jLen === j+1) {
                            view.drawBusStation(new AMap.LngLat(tempBusXY_xy[0], tempBusXY_xy[1]), i, j,
                                j === 0 &&("名称:" + aLine.startName + "<br/>类型: 公交站<br/>公交路线名:" + aLine.busName)||("名称:" + aLine.endName + "<br/>类型: 公交站<br/>公交路线名:" + aLine.busName));
                        }
                    }
                }
                var line = new AMap.Polyline({
                    map: mapObj,
                    path : busXY, //线经纬度数组
                    strokeColor : "#005cb5", //线颜色
                    strokeOpacity : 0.8, //线透明度
                    strokeWeight : 6 //线宽
                });
                //mapObj.setFitView();
            }
        },
        /**
         * 标示公交站
         * @param data 数据
         */
        drawBusStation : function(change, i, j,  name){
            var marker = new AMap.Marker({
                map: mapObj,
                draggable: 0,
                icon : "http://webapi.amap.com/images/bx11.png", //复杂图标
                visible : true, //可见
                position : change
            });
            var infoWindow = new AMap.InfoWindow({
                size: new AMap.Size(400, 150),
                offset: new AMap.Pixel(0, -30), //new AMap.Pixel(0, -30),
                content: name
            });
            if(i === j && 0 === i) {
                infoWindow.open(mapObj, marker.getPosition());
            }
            mapObj.bind(marker, 'click', function() {
                if(infoWindow.getIsOpen()) {
                    infoWindow.close();
                } else {
                    infoWindow.open(mapObj, marker.getPosition());
                }
            });
        }
    });
    var searchPathView = new SearchPathView();
    function leftPad(theChar, num, str) {
        if(str.length >= num){
             return str;
        }else {
            for(var i = str.length; i < num; i ++) {
                str = theChar + str;
            }
            return str;
        }
    }
    function reviseBusInfo(place){
        if(!place.x || !place.y){
            var aroundLocal=mapView.model.get('aroundLocal');
            place.name = aroundLocal.name;
            place.x=aroundLocal.pos.lng;
            place.y=aroundLocal.pos.lat;
        }
    }
    App.MapShare.searchPathView = searchPathView;
    App.MapShare.searchPathModel = searchPathModel;
})();

posted on 2014-04-22 09:48 koradji 阅读(195) 评论(0)  编辑  收藏


只有注册用户登录后才能发表评论。


网站导航:
 
<2024年5月>
2829301234
567891011
12131415161718
19202122232425
2627282930311
2345678

导航

统计

常用链接

留言簿(2)

随笔分类

随笔档案

文章分类

文章档案

收藏夹

db2

dos

Groovy

Hibernate

java

WAS

web application

搜索

最新评论

阅读排行榜

评论排行榜