android在google map上画出导航路线图

android在google map上画出导航路线图 收藏
android在google map上画线比较容易实现的,但是现在问题在于如何获取起点和终点之间的路线图。这里我们使用Google Directions API来实现, Google Directions API是一种使用 HTTP 请求计算多个位置间路线的服务。路线可以以文本字符串或纬度/经度坐标的形式指定起点、目的地和路标。Google Directions API 可以使用一系列路标传回多段路线。

 Google Directions API 请求是以下形式的 HTTP 网址:http://maps.google.com/maps/api/directions/output?parameters

其中,output 可能是以下任何一个值:

l  json(建议)表示以 JavaScript 对象表示法 (JSON) 的形式输出

l  xml 表示以 XML 的形式输出

具体参数参见http://code.google.com/intl/zh-CN/apis/maps/documentation/directions/

       通过http请求获取线路,接下来我们需要对返回结果进行解析,提取出导航线路的一系列路标。

如果我们只是简单的画图路线路,返回结果中的字段overview_path包含可我们所需要的数据。它包含一个对象,该对象包含一组表示生成路线的近似(平滑)路径的已编码 points 和 levels。编码算法参见http://code.google.com/intl/zh-CN/apis/maps/documentation/utilities/polylinealgorithm.html说明。

我们只需要提取points字段中的字符串进行解码就可以得到我们所需的一系列点了,将这些点按顺序连接起来就是我们所要的路线图了。

 

view plaincopy to clipboardprint?
01./** 
02.     * 通过解析google map返回的xml,在map中画路线图 
03.     */ 
04.    public void drawRoute(){  
05.          
06.        String url = "http://maps.google.com/maps/api/directions/xml?origin=23.055291,113.391802" +  
07.                "&destination=23.046604,113.397510&sensor=false&mode=walking";  
08.          
09.        HttpGet get = new HttpGet(url);  
10.        String strResult = "";  
11.        try {  
12.            HttpParams httpParameters = new BasicHttpParams();  
13.            HttpConnectionParams.setConnectionTimeout(httpParameters, 3000);  
14.            HttpClient httpClient = new DefaultHttpClient(httpParameters);   
15.              
16.            HttpResponse httpResponse = null;  
17.            httpResponse = httpClient.execute(get);  
18.              
19.            if (httpResponse.getStatusLine().getStatusCode() == 200){  
20.                strResult = EntityUtils.toString(httpResponse.getEntity());  
21.            }  
22.        } catch (Exception e) {  
23.            return;  
24.        }  
25.          
26.        if (-1 == strResult.indexOf("<status>OK</status>")){  
27.            Toast.makeText(this, "获取导航路线失败!", Toast.LENGTH_SHORT).show();  
28.            this.finish();  
29.            return;  
30.        }  
31.          
32.        int pos = strResult.indexOf("<overview_polyline>");  
33.        pos = strResult.indexOf("<points>", pos + 1);  
34.        int pos2 = strResult.indexOf("</points>", pos);  
35.        strResult = strResult.substring(pos + 8, pos2);  
36.          
37.        List<GeoPoint> points = decodePoly(strResult);  
38.          
39.        MyOverLay mOverlay = new MyOverLay(points);  
40.        List<Overlay> overlays = mMapView.getOverlays();  
41.        overlays.add(mOverlay);  
42.          
43.        if (points.size() >= 2){  
44.            mMapController.animateTo(points.get(0));  
45.        }  
46.           
47.        mMapView.invalidate();  
48.    }  
49. /** 
50.     * 解析返回xml中overview_polyline的路线编码 
51.     *  
52.     * @param encoded 
53.     * @return 
54.     */ 
55.    private List<GeoPoint> decodePoly(String encoded) {  
56.        List<GeoPoint> poly = new ArrayList<GeoPoint>();  
57.        int index = 0, len = encoded.length();  
58.        int lat = 0, lng = 0;  
59.        while (index < len) {  
60.            int b, shift = 0, result = 0;  
61.            do {  
62.                b = encoded.charAt(index++) - 63;  
63.                result |= (b & 0x1f) << shift;  
64.                shift += 5;  
65.            } while (b >= 0x20);  
66.            int dlat = ((result & 1) != 0 ? ~(result >> 1) : (result >> 1));  
67.            lat += dlat;  
68.            shift = 0;  
69.            result = 0;  
70.            do {  
71.                b = encoded.charAt(index++) - 63;  
72.                result |= (b & 0x1f) << shift;  
73.                shift += 5;  
74.            } while (b >= 0x20);  
75.            int dlng = ((result & 1) != 0 ? ~(result >> 1) : (result >> 1));  
76.            lng += dlng;  
77.            GeoPoint p = new GeoPoint((int) (((double) lat / 1E5) * 1E6),  
78.                 (int) (((double) lng / 1E5) * 1E6));  
79.            poly.add(p);  
80.        }  
81.        return poly;  
82.    } 

 

本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/snailandjava/archive/2011/05/20/6435734.aspx

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值