利用OpenLayers怎样实现定位查询

 我来答
匿名用户
推荐于2016-10-08
展开全部

  要知道openlayers是客户端的代码,需要向服务器端请求数据才能知道对象在哪里啊,然后实现定位,从服务器端获得了对象的Extent之后,加上gutter,就可以居中定位地图了;

参考实例如下:

//初始地图  

function init(){  

    ......  

    vectors = new OpenLayers.Layer.Vector(...);  

    geojson = new OpenLayers.Format.GeoJSON();  

    map.addLayer(vectors);  

}  

//通过DWR异步取得GeoJSon串,交由OpenLayers.Format.GeoJSON来处理.  

function locorie(){  

    //异步发出请求  

    dwrService.orie(orieSeri,function(data){  

        //通过OpenLayers.Format.GeoJSON处理服务端提供的GenJSon串  

        var features = geojson.read(data,"FeatureCollection");  

        if(features) {  

            //将结果展示到地图上  

            vectors.addFeatures(features);  

        }  

    });  

}  

//十秒更新一次数据  

function startOrie(){  

    var t=setTimeout("locorie();startOrie();",10000);  

}  


public String[] orie(String orieSeri) {  

    String orieStr = "";  

    boolean state = true;  

    if (null == orieStr || "".equals(orieSeri)) {  

        state = false;  

    }  

    int dataSeri = Integer.parseInt(orieSeri);  

    List<Location> list = null;  

    if (state) {  

        // 获得dataSeri之后的位置数据 Location.getDataSeri >= dataSeri  

        list = locDao.findLocByFlow(dataSeri);  

    }  

    if (state && (list == null || list.size() == 0)) {  

        // 没有数据  

        state = false;  

    }  

    if (state&&(list.size() == 1 && dataSeri == list.get(0).getDataSeri()  

                    .intValue())) {  

        // 没有新的数据  

        state = false;  

    }  

      

    if (state) {  

        LineString line = new LineString();  

        StringBuffer geo = new StringBuffer();  

        Point pointEnd = new Point();  

        Feature feaPoint = new Feature(pointEnd);  

        Map<String, String> propoint = new HashMap<String, String>();  

        feaPoint.setProperties(propoint);  

        for (int i = 0; i < list.size(); i++) {  

            geo.append("[" + list.get(i).getCurLoc() + "]");  

            if (i != (list.size() - 1)) {  

                geo.append(",");  

            }  

        }  

        line.setLine(geo.toString());  

        Feature feaLine = new Feature(line);  

        Map<String, String> properties = new HashMap<String, String>();  

        properties.put("color", "#1A60CA");  

        feaLine.setProperties(properties);  

        List<Component> components = new ArrayList<Component>();  

        components.add(feaLine);  

        components.add(feaPoint);  

        FeatureCollection feaCol = new FeatureCollection(components);  

        orieStr = feaCol.draw();  

    }  

}  

 服务器端生成json串,用装饰者模式写了一个模块,只要调用这个模块的API就会生成需要的Json串,如下:

AiPPT
2024-09-19 广告
随着AI技术的飞速发展,如今市面上涌现了许多实用易操作的AI生成工具1、简介:AiPPT: 这款AI工具智能理解用户输入的主题,提供“AI智能生成”和“导入本地大纲”的选项,生成的PPT内容丰富多样,可自由编辑和添加元素,图表类型包括柱状图... 点击进入详情页
本回答由AiPPT提供
time陌言成殇
2015-01-28 · TA获得超过8.4万个赞
知道大有可为答主
回答量:1.4万
采纳率:91%
帮助的人:9880万
展开全部

您好,很高兴为您解答。

页面发起异步请求;服务端程序将请求结果处理成GeoJSON串回传至请求页面;请求页面通过OpenLayers提供的OpenLayers.Format.GeoJSON解析GeoJSON串,将结果展现到地图上。

//初始地图  
function init(){  
    ......  
    vectors = new OpenLayers.Layer.Vector(...);  
    geojson = new OpenLayers.Format.GeoJSON();  
    map.addLayer(vectors);  
}  
//通过DWR异步取得GeoJSon串,交由OpenLayers.Format.GeoJSON来处理.  
function locorie(){  
    //异步发出请求  
    dwrService.orie(orieSeri,function(data){  
        //通过OpenLayers.Format.GeoJSON处理服务端提供的GenJSon串  
        var features = geojson.read(data,"FeatureCollection");  
        if(features) {  
            //将结果展示到地图上  
            vectors.addFeatures(features);  
        }  
    });  
}  
//十秒更新一次数据  
function startOrie(){  
    var t=setTimeout("locorie();startOrie();",10000);  
}

服务端程序:

public String[] orie(String orieSeri) {  
    String orieStr = "";  
    boolean state = true;  
    if (null == orieStr || "".equals(orieSeri)) {  
        state = false;  
    }  
    int dataSeri = Integer.parseInt(orieSeri);  
    List<Location> list = null;  
    if (state) {  
        // 获得dataSeri之后的位置数据 Location.getDataSeri >= dataSeri  
        list = locDao.findLocByFlow(dataSeri);  
    }  
    if (state && (list == null || list.size() == 0)) {  
        // 没有数据  
        state = false;  
    }  
    if (state&&(list.size() == 1 && dataSeri == list.get(0).getDataSeri()  
                    .intValue())) {  
        // 没有新的数据  
        state = false;  
    }  
      
    if (state) {  
        LineString line = new LineString();  
        StringBuffer geo = new StringBuffer();  
        Point pointEnd = new Point();  
        Feature feaPoint = new Feature(pointEnd);  
        Map<String, String> propoint = new HashMap<String, String>();  
        feaPoint.setProperties(propoint);  
        for (int i = 0; i < list.size(); i++) {  
            geo.append("[" + list.get(i).getCurLoc() + "]");  
            if (i != (list.size() - 1)) {  
                geo.append(",");  
            }  
        }  
        line.setLine(geo.toString());  
        Feature feaLine = new Feature(line);  
        Map<String, String> properties = new HashMap<String, String>();  
        properties.put("color", "#1A60CA");  
        feaLine.setProperties(properties);  
        List<Component> components = new ArrayList<Component>();  
        components.add(feaLine);  
        components.add(feaPoint);  
        FeatureCollection feaCol = new FeatureCollection(components);  
        orieStr = feaCol.draw();  
    }  
}


服务器端提供的数据格式如下:

//一条线  
{  
"type":"FeatureCollection",  
"features":[{  
"type":"Feature",  
"geometry":{  
"type":"LineString",  
"coordinates":[[42.895131111145, 22.148587703705],[43.895131111145, 23.148587703705]]}}]  
}

服务器端生成json串,用装饰者模式写了一个模块,只要调用这个模块的API就会生成需要的Json串。


如若满意,请点击右侧【采纳答案】,如若还有问题,请点击【追问】

希望我的回答对您有所帮助,望采纳!

                                                                                                                            ~ O(∩_∩)O~

本回答被提问者和网友采纳
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式