main.js 6.37 KB

var main = {
    map:null,
    layer:null,
    vectorLayer:null,
    dragFeature:null,
    drawPoint:null,
    currentPoint:null,
    pastPoint:null,
    pointId:null,
    init:function () {
        //初始化地图
        this.map = new SuperMap.Map("map", {
            controls: [
                new SuperMap.Control.Navigation()
                // new SuperMap.Control.Zoom(),
                // new SuperMap.Control.LayerSwitcher()
            ]
        });
        this.map.events.on({
            zoomend: function (evt) {
                mapLevel.setLevel(main.map.getZoom());
            }
        });
        this.layer = new SuperMap.Layer.CloudLayer();
        //实例化矢量图层 vectorLayer
        this.vectorLayer = new SuperMap.Layer.Vector("vectorLayer");
        //实例化 DragFeature 控件
        // this.dragFeature = new SuperMap.Control.DragFeature(this.vectorLayer);
        //实例化 DrawFeature 控件
        this.drawPoint = new SuperMap.Control.DrawFeature(this.vectorLayer, SuperMap.Handler.Point, {multi: true});
        //监听 featureadded 事件,当添加要素时会触发此事件
        this.drawPoint.events.on({"featureadded": this.drawCompleted});
        //map上添加控件
        // this.map.addControl(this.dragFeature);
        this.map.addControl(this.drawPoint);
        //加载图层
        this.map.addLayers([this.layer,this.vectorLayer]);
        //画点样式
        this.vectorLayer.style = {
            externalGraphic:"src/image/icon.png",
            graphicWidth:21,
            graphicHeight:30
        };
        // this.map.setCenter(new SuperMap.LonLat(11339634.286396, 4588716.5813769), 5);
        //加载地图大小缩放控件
        mapLevel.loadLevelControl(main.map);
        //加载按钮事件
        $('#location_cancel').hide();
        $('#location_confirm').hide();
        $('#location_update').click(function () {
            main.updateLocation("location_update");
        });
        $('#location_cancel').click(function () {
            main.updateLocation("location_cancel");
        });
        $('#location_confirm').click(function () {
            main.updateLocation("location_confirm");
        });
        $('#btn_update').click(function () {
            main.updateReturn();
        });
        //获取网点
        this.validate();
        // this.getInfoSuccess(11);
    },
    validate:function () {
        var method = "/v1/point/search";
        var parameter = {
            custom_id:"{\"fieldName\":\"名称\",\"fieldValue\":\"网点1\"}"
        };
        saas.saasHttpRequest(method,parameter,this.validateSuccess,this.validateError);
    },
    validateSuccess:function (data) {
        if (data.code == "S001"){
            var result = data.result;
            if (result.length > 0){
                var pointName = result[0].pointName;
                main.pointId = result[0].id;
                main.getInfo(main.pointId);
            }
        }
    },
    validateError:function (error) {

    },
    getInfo:function (id) {
        var method = "/v1/point/getPointById";
        var parameter = {
            pointId:id,
            needExtCols:true
        };
        saas.saasHttpRequest(method,parameter,this.getInfoSuccess,this.getInfoError);
    },
    getInfoSuccess:function (data) {
        // var lonlat = {x:120.42995, y:36.131393};
        // var po = latLonToMeters(lonlat);
        // main.addMarker(po.x, po.y,1, "src/image/icon.png");
        // main.map.setCenter(new SuperMap.LonLat(po.x, po.y), 5);
        if (data.code == "S001"){
            var result = data.result;
            main.addMarker(result.x, result.y,result.id, "src/image/icon.png");
            main.map.setCenter(new SuperMap.LonLat(result.x, result.y), 5);
        }

    },
    getInfoError:function (error) {

    },
    updateLocation:function (btn) {
        if (btn == "location_update"){
            $('#location_update').hide();
            $('#location_cancel').show();
            $('#location_confirm').show();
            //激活控件
            // main.dragFeature.activate();
            main.drawPoint.activate();
        }else if (btn == "location_cancel"){
            $('#location_update').show();
            $('#location_cancel').hide();
            $('#location_confirm').hide();
            //注销控件
            // main.dragFeature.deactivate();
            main.drawPoint.deactivate();
        }else if (btn == "location_confirm"){
            $('#location_update').show();
            $('#location_cancel').hide();
            $('#location_confirm').hide();
            //注销控件
            // main.dragFeature.deactivate();
            main.drawPoint.deactivate();
            // var point = this.pointFeature.geometry.getCentroid();
            // console.log(point.x+","+point.y);
        }
    },
    addMarker: function (pointX,pointY,code,iconPath) {
        var lonlat = {
            x:pointX,
            y:pointY
        };
        // var lonlat = latLonToMeters(pointX,pointY);
        var geometry = new SuperMap.Geometry.Point(lonlat.x,lonlat.y);
        var style = {
            // fillColor:iconPath,
            // pointRadius:3,
            // stroke:true,
            // strokeColor:iconPath
            externalGraphic:iconPath,
            graphicWidth:21,
            graphicHeight:30
        };
        this.currentPoint = new SuperMap.Feature.Vector(geometry,{id:code},style);
        this.vectorLayer.addFeatures(this.currentPoint);
    },
    updateReturn:function () {
        console.log(main.pointId)
        //跳转修改信息界面
        localStorage.setItem('pointId',main.pointId)
        window.location.href="dataPage.html";
    },
    drawCompleted:function (eventArgs) {
        main.pastPoint = main.currentPoint;
        main.currentPoint = eventArgs.feature;
        main.vectorLayer.removeAllFeatures();
        main.vectorLayer.addFeatures(main.currentPoint);
        var geometry = eventArgs.feature.geometry.getCentroid();
        main.updatePoint(geometry.x,geometry.y);
    },
    updatePoint: function (x,y) {
        var method = "/point/updatePoint";
        var parameter = {
            custom_id:"{\"fieldName\":\"名称\",\"fieldValue\":\"网点1\"}",
            infos:"{\"fieldName\":\"名称\",\"fieldValue\":\"网点1\"}",
            x:x,
            y:y,
            coord_type:"gcj02mc"
        };
        saas.customHttpRequest(method,parameter,this.getInfoSuccess,this.getInfoError);
    }
}