距离查询


距离查询,是指查询距离指定几何对象一定距离范围内的地物。对于点几何对象,则查询以该点为圆心,以距离半径画圆,落在该圆形范围内的地物;对于线和面几何对象,则查询距离对像边界一定范围内的地物。

本例将以World地图为例,Capitals@World.1图层中,距离中心点30地图单位(度)内的首都,并把查询结果作为Marker标注在地图上。

var local, map, layer, markerLayer1, markerLayer2,
        centerPoint = new SuperMap.Geometry.Point(121, 31),//指定的查询中心点
        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});
    markerLayer1 = new SuperMap.Layer.Markers("Markers");
    markerLayer2 = new SuperMap.Layer.Markers("Markers");

    //在地图上具体位置上画一个图形
    var point = centerPoint,
            size = new SuperMap.Size(44, 40), //查询点的图标的大小
            offset = new SuperMap.Pixel(-(size.w/2), -size.h), 
            icon = new SuperMap.Icon("http://www.supermapol.com/static/portal/images/markers/mark_purple.png", size, offset);  
    markerLayer1.addMarker(new SuperMap.Marker(new SuperMap.LonLat(point.x, point.y), icon)); //把查询点作为Marker添加到地图上,并指定图标
}

function addLayer() {
    map.addLayers([layer, markerLayer1, markerLayer2]);
    map.setCenter(new SuperMap.LonLat(0, 0), 0);
}
function queryByDistance() {
    markerLayer2.clearMarkers();

    //查询Capitals@World.1图层中,距离中心点30地图单位(度)内的首都
    var queryByDistanceParams = new SuperMap.REST.QueryByDistanceParameters({
        queryParams: new Array(new SuperMap.REST.FilterParameter({name: "Capitals@World.1"})),
        returnContent: true,
        distance: 30,
        geometry: centerPoint
    });
    //使用QueryByDistanceService接口,执行距离查询
    var queryByDistanceService = new SuperMap.REST.QueryByDistanceService(url);
    queryByDistanceService.events.on({
        "processCompleted": processCompleted,
        "processFailed": processFailed
    });
    queryByDistanceService.processAsync(queryByDistanceParams);
}
//返回查询结果,并把查询结果中的要素,作为Marker添加到地图上
function processCompleted(queryEventArgs) {
    var i, j, result = queryEventArgs.result;
    for(i = 0;i < result.recordsets.length; i++) {
        for(j = 0; j < result.recordsets[i].features.length; j++) {
            var point = result.recordsets[i].features[j].geometry,
                    size = new SuperMap.Size(44, 33),
                    offset = new SuperMap.Pixel(-(size.w/2), -size.h),
                    icon = new SuperMap.Icon("http://www.supermapol.com/static/portal/images/markers/mark_blue.png", size, offset);
            markerLayer2.addMarker(new SuperMap.Marker(new SuperMap.LonLat(point.x, point.y), icon));
        }
    }
}
function processFailed(e) {
    alert(e.error.errorMsg);
}
function clearFeatures() {
    //清除上次的显示结果
    markerLayer2.clearMarkers();
}

在线演示与源码编辑

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