谷歌离线地图的使用(1)


实质: 整个地图都是以由 小的地图瓦片组成的,因此可以将这些小瓦片图片保存下来,在需要的时候读取即可。
过程: 客户端将地图的三维坐标发送回服务器,服务器经过计算后,返回对应的 图片。

API 下载地址: http://download.youkuaiyun.com/detail/iamwangch/4933837


Google的离线地图并未真正意义上的离线,由于代码过于凌乱,无法正确解读,所以采用了添加本地服务器的方法实现离线

 过程: 

  • (一)创建项目并添加GoogleMapAPIV3 文件包

下载地址: http://www.cnblogs.com/liongis/archive/2011/04/28/2032316.html

  • (二)修改文件包内的mapapi.js的myUrl变量为项目登录网址如:http:           hostlocal:8080/xx
  • (三)响应请求 项目获取有js发出的一个action的请求 如:

http://"+myUrl+"/mt0/vt.action?lyrs=m@152&src=apiv3&hl=zh-CN&x=216&y=104&z=8&s=

上面的请求有x,y,z的三个参数传进来,这三个参数就是地图瓦片的值

根据这三个参数传回瓦片图片即可。

  • (四)经纬度 转换到 图片坐标

参考: http://code.google.com/intl/zh-CN/apis/maps/documentation/javascript/overlays.html#CustomMapTypes

 



服务器代码

下载

/**
       * 保存文件
       */
        private  void  SaveImage( int  x,  int  y, int  z,String basePath){
            String outPath = basePath+  "/" +x+ "-"  +y+ "-" +z+  ".png" ;
            String urlStr =  "http://mt1.googleapis.com/vt?lyrs=m@203000000&src=apiv3&hl=zh-CN&"  +
                          "x="  +x+
                          "&y="  +y+
                          "&z="  +z+
                          "&s=Galileo&style=api%7Csmartmaps"  ;
              int  chByte = 0;         
            URL url =  null ;         
            HttpURLConnection httpConn =  null ;        
            InputStream in =  null ;        
            FileOutputStream out =  null ;        
              try  {                   
                  url =  new  URL(urlStr);              
                  httpConn = (HttpURLConnection)
                  url.openConnection();               
                  HttpURLConnection. setFollowRedirects( true );                 
                  httpConn.setRequestMethod(  "GET" );               
                  httpConn.setRequestProperty(  "User-Agent" ,                               
                                "Mozilla/4.0 (compatible; MSIE 6.0; Windows 2000)" );              
                    // logger.info(httpConn.getResponseMessage());              
                  in = httpConn.getInputStream();                 
                  out =  new  FileOutputStream( new  File(outPath));              
                  chByte = in.read();                 
                    while  (chByte != -1) {                    
                        out.write(chByte);                        
                        chByte = in.read();                 
                        }           
                  }  catch  (MalformedURLException e) {                   
                        e.printStackTrace();          
                        }  catch  (IOException e) {                 
                              e.printStackTrace();    
                              }
            }


返回客户端


public  String doLoad()  throws  IOException {
            HttpServletRequest request = ServletActionContext.getRequest();
            HttpServletResponse response = ServletActionContext.getResponse();
            String basePath  =  "" ;   //基本路径
            basePath = request.getSession().getServletContext().getRealPath( "/map-images"  );
              //System.out.println("基本路径 ->"+basePath);
            System.  out .println( "坐标: x:"  +  x  +  " y:"  +  y  +  " z:"  +  z  );
            ByteArrayOutputStream byteArrayOutputStream =  new  ByteArrayOutputStream();
            
            
            String path = basePath+  "/" + x  + "-"  + y  + "-"  + z  + ".png"  ;
            File file =  new  File(path);  // 读取指定路径的图片
            
            
            InputStream in =  null ;
              try  {
                  in =  new  FileInputStream(file);
            }  catch  (FileNotFoundException e) {
                    this .SaveImage( x  ,  y  ,  z  , basePath);
                  in =  new  FileInputStream(file);
                    //e.printStackTrace();
                  System.  out .println( "文件不存在,需要下载."  );
            }
              int  tag = 0;
              byte [] b =  new  byte [100];
              while  ((tag = in.read(b))!=-1) {
                    //System.out.println("读取到的大小:"+tag);
                  byteArrayOutputStream.write(b, 0, tag);
            }
            
              this . imageStream  =  new  ByteArrayInputStream(byteArrayOutputStream.toByteArray());
            
              return  "success"  ;
      }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值