zoukankan      html  css  js  c++  java
  • Google Map API v2 番外篇 关于gps位置偏差及修正方法探讨

    我的手机是M35C,在我自己的map activity中,通过gps获取到的经纬度比实际地址总是有500米左右的偏差。

    在网上搜索了很多,都说这个是测绘局为了保密故意弄成这样的。gps全球定位系统获得的location是WGS84坐标系,而咱们是用的自己的坐标系,不知道是BJ54还是大地坐标系。

    反正就是不一样。

    但我没想明白,为什么安装google自己的地图apk 就没有这个问题呢。

    转换模型貌似能搜到些很正规的论文,呃。。我没仔细看,对于我这样高数只能勉强的及格的人来说,那些公式具有强烈的眩晕效果。@_@

    一个修正的办法是通过百度的坐标转换web api

    发送一个httpGet的请求

    String urlString = "http://api.map.baidu.com/ag/coord/convert?from=0&to=2&x=" + lng + "&y=" + lat;

    from=0&to=2 即表示要从gps坐标系转到google坐标系,我也搞不清这是什么意思。
                //gps坐标的type=0
                //google坐标的type=2
                //baidu坐标的type=4

    这个http请求返回的是json,其中x字段是经度,y字段是纬度,发送http的部分就不贴了,只贴一段json的解析

     1 String mHttpResponse = httpClient.execute(httpGet);
     2                 
     3 if (mHttpResponse.getStatusLine().getStatusCode() == 200){  
     4     mHttpEntity = mHttpResponse.getEntity();
     5     responseString = mHttpEntity.toString();
     6 }
     7 
     8 String err = responseString.substring(responseString.indexOf("error") + 7, responseString.indexOf("error") + 8);
     9 if ("0".equals(err))
    10 {
    11     JSONObject js;
    12     try {
    13         js = new JSONObject(responseString);
    14         String x = js.getString("x");
    15         String y = js.getString("y");
    16         byte[] xByte = Base64.decode(x,0);
    17         byte[] yByte = Base64.decode(y,0);
    18         Location retLoc = new Location("");
    19                     
    20         String lngString = new String(xByte);
    21         String latString = new String(yByte);
    22                     
    23         double lng = Double.parseDouble(lngString);
    24         double lat = Double.parseDouble(latString);
    25                     
    26         retLoc.setLatitude(lat);
    27         retLoc.setLongitude(lng);
    28         return retLoc;
    29      } catch (JSONException e) {
    30         log("JSONException in doInBackground():" + e);
    31      }        
    32 }

    从第11行开始是JSON解析,x,y也是base64的。

    接下来要想办法让google的“我的位置”图层使用我们修正后的经纬度。

    1,首先实现一个LocationSource给google地图的MyLocationLayer送数据

    private LocationSource.OnLocationChangedListener myLocationListener = null;
    private class MyLocationSource implements LocationSource{
    
        @Override
        public void activate(OnLocationChangedListener listener) {
            log("MyLocationSource activate");
            myLocationListener = listener;
        }
    
        @Override
        public void deactivate() {
            log("MyLocationSource deactivate");
            myLocationListener = null;
        }     
    
    }

    2,调用GoolgeMap.setLocationSource(new MyLocationSource());

    当my location layer显示的时候会跑到public void activate(OnLocationChangedListener listener),入参就是my location layer用来监听位置变化的listener,把它赋值给private的myLocationListener。

    3,得到转换后的经纬度后,调用myLocationListener.onLocationChanged(location)通知给google地图。

    结果,位置是正确了,但是实时性很差,而且在google地图里面用baidu的api,显得很无赖。

    我最终用百度地图重写了这个activity,算是投降了。这里记录一下,以后有空再研究吧

  • 相关阅读:
    第七组第五次Alpha冲刺会议
    第七组第四次Alpha冲刺会议
    第三次Alpha冲刺会议&&期中回顾总结
    八级大狂风-项目系统设计与数据库设计
    Python之路——hmac模块
    Python之路——多线程
    Python之路——requests模块
    Python之路——并行编程之multiprocessing模块
    Python之路——迷宫问题
    Python之路——configparser模块
  • 原文地址:https://www.cnblogs.com/inkheart0124/p/3536889.html
Copyright © 2011-2022 走看看