首页 / 浏览问题 / / 问题详情
怎么实现点击点弹出一个alert呢?(代码如下)
8月21日, 2017
<script type="text/javascript">
//        定义地图变量
        var map,layer,selectFeature,vectorLayer,vectorLayer2,queryParam, queryBySQLParams, queryBySQLService;
        var url = "${mapUrl}/iserver/services/map-bhdt/rest/maps/bh";
        style = {
            strokeColor: "#FFDAB9",
            strokeWidth: 2,
            pointerEvents: "visiblePainted",
            fillColor: "#FFDAB9",
            fillOpacity: 0.9         // 填充透明度
        };
        //定义地图
//        function init() {
//    }
        function addLayer() {
            //将Layer图层加载到Map对象上
            map.addLayers([layer,vectorLayer]);
            //出图,map.setCenter函数显示地图
            map.setCenter(new SuperMap.LonLat(12951739.74, 4863339.02), 0);
//            vectorLayer2 = new SuperMap.Layer.Vector("高亮显示图层");
//            vectorLayer2.displayInLayerSwitcher = false;
//        map.minZoom = 0;
            queryBySQL();

        }
        //sql查询
        function  queryBySQL(){
            queryParam = new SuperMap.REST.FilterParameter({
                name: "bh_sxt@北航",
                attributeFilter: "SMID>0"
            });
            queryBySQLParams = new SuperMap.REST.QueryBySQLParameters({
                queryParams: [queryParam]
            });
            queryBySQLService = new SuperMap.REST.QueryBySQLService(url, {
                eventListeners: {"processCompleted": processCompleted}});
            queryBySQLService.processAsync(queryBySQLParams);
            function processCompleted(queryEventArgs) {
                var i, j, feature,result = queryEventArgs.result;
                if (result && result.recordsets) {
                    for (i=0; i<result.recordsets.length; i++) {
                        if (result.recordsets[i].features) {
                            for (j=0; j<result.recordsets[i].features.length; j++) {
                                feature = result.recordsets[i].features[j];
                                feature.style = style;
                                vectorLayer.addFeatures(feature);
                            }
                        }
                    }
                }
            }
        }
</script>
<script>
    $(document).ready(function() {
//        init();
        map = new SuperMap.Map("map", {
            controls: [
                new SuperMap.Control.Navigation({     // 此控件处理伴随鼠标事件(拖拽,双击、鼠标滚轮缩放)的地图浏览, 如果创建地图时没有设置任何控件,此控件会默认添加到地图。
                    dragPanOptions: {
                        enableKinetic: false         // 拖拽动画
                    }
                }),
            ], allOverlays: true,
        });
        layer = new SuperMap.Layer.TiledDynamicRESTLayer("bh", url,
            null, {maxResolution: "auto"});
        layer.events.on({"layerInitialized": addLayer});
//        定义图层
        vectorLayer = new SuperMap.Layer.Vector("要素选择图层");
//        // 是否需要在图层管理器中显示(默认为false)
//        vectorLayer.displayInLayerSwitcher = false;
//        //将初始化查询结果的透明度设置为0
//        vectorLayer.setOpacity(0);

        var callbacks = {
            over: function(currentFeature){
//                vectorLayer2.removeAllFeatures();
//
//                var f = new SuperMap.Feature.Vector;
//                f.geometry = currentFeature.geometry.clone();
//                f.style= style;
//                vectorLayer2.addFeatures(f);
            },
            out: function(currentFeature){
            },
            click: function(currentFeature){
                alert("hello")
            }

        };
        selectFeature = new SuperMap.Control.SelectFeature(vectorLayer,
            {hover: false, callbacks: callbacks});
        map.addControl(selectFeature);
        selectFeature.activate();
    });
</script>

1个回答

您好,您可以参考范例http://support.supermap.com.cn:8090/iserver/iClient/forJavaScript/examples/examples.html#mapevent,添加一个click事件,通过click实现弹出alert。

李熙y  (4,524分)  名扬四海
8月21日, 2017
您好,我把代码粘上面了,代码里面有click事件,怎么还是点击不会弹出呢?麻烦您看看代码,是哪里出现问题了吗?

我在

 addLayer()这个函数中调用
queryBySQL()这个sql查询方法对吗?
添加一个click事件,通过回调函数触发。

map.events.on({"click":callbackFunction});

function callbackFunction(e)
        {
          alert("鼠标事件");

        }
您是想要在addLayer的时候就触发sql查询?

恩恩  是的,因为根据技术人员的所说,那个

TiledDynamicRESTLayer()是加载的一张图片,无法对图片就行操作,所以需要先做一个sql查询,吧点的属性和几何数据查出来,然后在new Vector(),用SelectFeature()中的click时间就行alert事件,如果能alert我就可以在这个事件中做一些其他的处理,现在是连这个alert也实现不了.
function init()
        {
            map = new SuperMap.Map("map",{controls:[
                new SuperMap.Control.Zoom() ,
                new SuperMap.Control.Navigation() ,
                new SuperMap.Control.LayerSwitcher()
            ]});
            layer= new SuperMap.Layer.TiledDynamicRESTLayer("World", url, null,{maxResolution:"auto"});
            layer.events.on({"layerInitialized":addLayer});
            vectorlayer= new SuperMap.Layer.Vector("vectorLayer");
            addData();
            var callbacks={
                click: function(currentFeature){
                alert("鼠标事件");
             }
            };
            var  selectFeature = new SuperMap.Control.SelectFeature(vectorlayer,
                    {
                        callbacks: callbacks
                    });
            map.addControl(selectFeature);
            selectFeature.activate();

        }

        function addLayer(){
            map.addLayers([layer,vectorlayer]);
            map.setCenter(new SuperMap.LonLat(0, 0), 0);
        }
       
        function addData()
        {
            var point= new SuperMap.Geometry.Point(0,0);
             pointFeature = new SuperMap.Feature.Vector(point);
             pointFeature.style={
                fillColor:"red",
                strokeColor:"yellow",
                pointRadius:7
             };
            vectorlayer.addFeatures(pointFeature);
        }

参考下这个吧
...