SQL查询


SQL查询是最常见也是最容易实现的查询方式,一般在线地图默认使用的就是SQL查询。SQL查询通过对数据的属性表中的字段,进行相等、大于、小于等运算,可以快速检索需要的地物(空间要素)。

本例将以World地图为例,查询Countries@World.1图层中,人口和面积均满足要求的国家(Pop_1994>1000000000 and SmArea>900),并把查询结果作为矢量图层,以指定的样式叠加在地图上。

var map, local, layer, vectorLayer,
        style = {
            strokeColor: "#304DBE",
            strokeWidth: 1,
            fillColor: "#304DBE",
            fillOpacity: "0.8"
        },
        url = "http://www.supermapol.com/iserver/services/vm3sbiax/rest/maps/World";
var value = "VZ88xbrMEMpGv4yiisTojgVq",
    name = "ak";
SuperMap.Credential.CREDENTIAL = new SuperMap.Credential(value, "ak");

function init(){
    map = new SuperMap.Map("map",{controls: [
        new SuperMap.Control.LayerSwitcher(),
        new SuperMap.Control.ScaleLine(),
        new SuperMap.Control.Zoom(),
        new SuperMap.Control.Navigation({
            dragPanOptions: {
                enableKinetic: true
            }
        })]
                                                             });
    layer = new SuperMap.Layer.TiledDynamicRESTLayer("World", url, {transparent: true, cacheEnabled: true}, {maxResolution:"auto"});
    layer.events.on({"layerInitialized":addLayer});
    //定义一个矢量图层用于保存查询到的要素
    vectorLayer = new SuperMap.Layer.Vector("Vector Layer");
}

function addLayer() {
    map.addLayers([layer, vectorLayer]);
    map.setCenter(new SuperMap.LonLat(0, 0), 0);
}
function queryBySQL() {
    vectorLayer.removeAllFeatures();

    var queryParam, queryBySQLParams, queryBySQLService;
    //查询条件为:Countries@World.1图层中,人口和面积均满足要求的国家
    queryParam = new SuperMap.REST.FilterParameter({
        name: "Countries@World.1",
        attributeFilter: "Pop_1994>1000000000 and SmArea>900"
    });
    queryBySQLParams = new SuperMap.REST.QueryBySQLParameters({
        queryParams: [queryParam]
    });
    //根据定义的条件执行SQL查询
    queryBySQLService = new SuperMap.REST.QueryBySQLService(url, {
        eventListeners: {"processCompleted": processCompleted, "processFailed": processFailed}});
    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);
                }
            }
        }
    }
}
function processFailed(e) {
    alert(e.error.errorMsg);
}
function clearFeatures() {
    //先清除上次的显示结果
    vectorLayer.removeAllFeatures();
    vectorLayer.refresh();
}

在线演示与源码编辑

您可以在线访问完整代码、体验演示效果,也可以直接在线编辑源码并实时查看效果。