zoukankan      html  css  js  c++  java
  • 自定义AJAX请求获取地图范围

        该程序功能的实现过程大致是这样的:

        在更新地图范围时,通过向服务器发送AJAX请求,在后台实现该特定请求的监听,获取相应的地图信息后,通过XML形式返回客户端,客户端也实现了相应的JS函数利用返回的xmlhttp参数对浏览页面进行更新。

        首先需要给地图控件添加监听器,当指定事件发生时调用指定函数处理该业务功能,这段初始化的JS函数需要在bodyload事件发生时即调用,JS代码如下:

     

    Code

     

    接下来在服务端定义监听器JAVA类,需要实现PhaseListener接口,代码如下所示:

     

    Code

    public class MapUpdatePhaseListener 
       implements PhaseListener
    {
       
    public PhaseId getPhaseId()
       {
          
    return PhaseId.APPLY_REQUEST_VALUES;
       }


       

    public void beforePhase(PhaseEvent event)
       {
          
    // do nothing
       }


       

    public void afterPhase(PhaseEvent event)
       {
          FacesContext facesContext 
    = event.getFacesContext();
          Map 
    params = facesContext.getExternalContext().getRequestParameterMap();
          WebContext webContext 
    = WebUtil.getWebContext(facesContext.getViewRoot());
          WebMap webMap 
    = webContext.getWebMap();


          

    if("true".equals(params.get("getMapInfo")))
          {
            WebExtent webExtent 
    = webMap.getCurrentExtent();
            
    double xmin = webExtent.getMinX();
            
    double ymin = webExtent.getMinY();
            
    double xmax = webExtent.getMaxX();
            
    double ymax = webExtent.getMaxY();
            
    double scale = webMap.getMapScale();

            Document doc 
    = XMLUtil.newDocument();
            Element mapinfo 
    = XMLUtil.createElement(doc, "mapinfo"""null);
            XMLUtil.createElement(doc, 
    "xmin", String.valueOf(xmin), mapinfo);
            XMLUtil.createElement(doc, 
    "ymin", String.valueOf(ymin), mapinfo);
            XMLUtil.createElement(doc, 
    "xmax", String.valueOf(xmax), mapinfo);
            XMLUtil.createElement(doc, 
    "ymax", String.valueOf(ymax), mapinfo);
            XMLUtil.createElement(doc, 
    "scale", String.valueOf(scale), mapinfo);


            

    try
            {
               AJAXUtil.writeResponse(facesContext, doc);
            }
            
    catch(IOException ex)
            {
               ex.printStackTrace();
            }
            
    finally
            {
               facesContext.responseComplete();
            }


          }
       }
    }

     

    然后在faces-config.xml文件中进行注册

     

     <lifecycle>

     
    <phase-listener>com.igsnrr.MapUpdatePhaseListener</phase-listener>

     
    </lifecycle>

     

    最后一步既是实现分析返回的xml结果的JS函数,更新页面进行显示,代码如下:

     

    Code

     

    按照上述步骤依次完成后,即可运行程序,查看效果图,如下所示:

  • 相关阅读:
    关于使用css3属性:transform固定菜单位置,在滑动页面时菜单闪现抖动的问题
    使用iscroll.js插件时,遇到在Android端无法点击a超链的解决办法
    window.external的使用
    SpringMVC的拦截器(Interceptor)和过滤器(Filter)的区别与联系
    chain.doFilter(request,response)含义
    巧用Ajax的beforeSend 提高用户体验
    getRequestDispatcher(path).forward(),,执行完,后面的代码居然还会执行!!!记得加return 啊亲
    @JsonSerialize(include=JsonSerialize.Inclusion.NON_NULL)
    ajax修改表单的值后dom没更新的解决办法
    .net core json序列化首字符小写和日期格式处理
  • 原文地址:https://www.cnblogs.com/jevonsea/p/1520685.html
Copyright © 2011-2022 走看看