仍然是建议个异步小任务

 private GetPathTask mGetPathTask = null;

 private void getGuidePath(LatLng origin){
if(mGetPathTask != null){
mGetPathTask.cancel(true);
}
mGetPathTask = new GetPathTask();
mGetPathTask.execute(origin, mMarker.getPosition());
}

第8行的两个入参都是LatLng对象,分别为其实地点和目标地点。

GetPathTask任务定义如下:

 private class GetPathTask extends AsyncTask<LatLng, Void, List<LatLng>>{

   @Override
protected void onPostExecute(List<LatLng> result){
if((result == null) || result.isEmpty()){
Toast.makeText(MapsActivity.this, R.string.GetPathErr, Toast.LENGTH_LONG).show();
return;
} if(mGuidePath == null){
mGuidePath = mMapView.addPolyline(new PolylineOptions()
.color(0xfff5cb08)
.width(10)
.geodesic(true));
}
mGuidePath.setPoints(result);
} @Override
protected List<LatLng> doInBackground(LatLng... params) {
LatLng origin = params[0];
LatLng destination = params[1];
String responseString = null;
String urlString = "http://maps.google.com/maps/api/directions/xml?sensor=true&mode=walking&avoid=highways"; HttpGet httpGet = new HttpGet(urlString + "&origin=" + origin.latitude + "," + origin.longitude
+ "&destination=" + destination.latitude + "," + destination.longitude); HttpResponse mHttpResponse = null;
HttpEntity mHttpEntity = null;
try{
HttpParams httpParameters = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(httpParameters, 3000);
HttpClient httpClient = new DefaultHttpClient(httpParameters); mHttpResponse = httpClient.execute(httpGet); if (mHttpResponse.getStatusLine().getStatusCode() == 200){
mHttpEntity = mHttpResponse.getEntity();
responseString = mHttpEntity.toString();
responseString = EntityUtils.toString(mHttpEntity);
}
}
catch (Exception e){
log("Exception in GetPathTask doInBackground():" + e);
} if(responseString == null){
log("GetPathTask doInBackground() responseString == null");
}else if (-1 == responseString.indexOf("<status>OK</status>")){
log("GetPathTask doInBackground() response status error");
}else{
int pos1 = responseString.indexOf("<overview_polyline>");
pos1 = responseString.indexOf("<points>", pos1 + 1);
int pos2 = responseString.indexOf("</points>", pos1);
responseString = responseString.substring(pos1 + 8, pos2); List<LatLng> points = decodePoly(responseString);
return points;
}
return null;
} private List<LatLng> decodePoly(String encoded) {
List<LatLng> poly = new ArrayList<LatLng>();
int index = 0, len = encoded.length();
int lat = 0, lng = 0; while (index < len) {
if(isCancelled())
break;
int b, shift = 0, result = 0;
do {
b = encoded.charAt(index++) - 63;
result |= (b & 0x1f) << shift;
shift += 5;
} while (b >= 0x20);
int dlat = ((result & 1) != 0 ? ~(result >> 1) : (result >> 1));
lat += dlat; shift = 0;
result = 0;
do {
b = encoded.charAt(index++) - 63;
result |= (b & 0x1f) << shift;
shift += 5;
} while (b >= 0x20);
int dlng = ((result & 1) != 0 ? ~(result >> 1) : (result >> 1));
lng += dlng; double dLat = lat / 1E5;
double dLng = lng / 1E5;
LatLng p = new LatLng(dLat, dLng); poly.add(p);
}
if(isCancelled())
return null; return poly;
}
}

看doInBackgound函数

24~27行,构造一个google directions API的HTTP请求,格式类似这样:

http://maps.google.com/maps/api/directions/xml?sensor=true&mode=walking&avoid=highways&origin=纬度,经度&destination=纬度,经度
我这个设置的步行模式,避免高速,返回数据是XML格式。更多的可选项请参考官文:

https://developers.google.com/maps/documentation/directions/

48~59行,返回的response中<point></point>标签中是base64编码的数据,是一组point,即google direction 服务为我们规划出的路径途经的各个点,调用decodePoly解析成List<LatLng>。

4行,onPostExecute函数中

10~16行,调用GoogleMap.addPolyline(PolylineOptions polylineOption) 在地图上加一个折线。调用Polyline.setPoints(point)设置折线的各个点。

贴个图看看,黄色的粗线把我们导向了美丽的莫愁湖。这里我还加了一条细红线,直接指向目的地,用于在移动过程中始终标识出目的地所在方位。

google地图支持在地图上添加几种shape对象,详见官文。

Google Map API v2 (四)----- 导航路径的更多相关文章

  1. Android中Google地图路径导航,使用mapfragment地图上画出线路(google map api v2)详解

    在这篇里我们只聊怎么在android中google map api v2地图上画出路径导航,用mapfragment而不是mapview,至于怎么去申请key,manifest.xml中加入的权限,系 ...

  2. Google Map API v2 步步为营(一) ----- 初见地图

    官方文档:https://developers.google.com/maps/documentation/android/start?hl=zh-CN 先谷歌后百度.使用google的api基本上按 ...

  3. google map api v2的使用详细过程,图文并茂(原创)

    上一篇中说到怎么获取key,下面来介绍怎么使用key来显示google地图 步骤1:eclipse上打开android SDK Manager,安装google play services. 步骤2: ...

  4. Google Map API v2 (三)----- 地图上添加标记(Marker),标记info窗口,即指定经纬度获取地址字符串

    接上篇 http://www.cnblogs.com/inkheart0124/p/3536322.html 1,在地图上打个标记 private MarkerOptions mMarkOption; ...

  5. Google Map API V2密钥申请

    之前用的都是v1,用的是MapView,好吧,仅仅能认命了.废话不再多说,開始android 的Google Maps Android API v2吧 之前參考了http://www.cnblogs. ...

  6. Google Map API v2 步步为营 (二)----- Location

    接上篇. 改造一下MapsActivity: public class MapsActivity extends Activity implements LocationListener, InfoW ...

  7. Google Map API v2 番外篇 关于gps位置偏差及修正方法探讨

    我的手机是M35C,在我自己的map activity中,通过gps获取到的经纬度比实际地址总是有500米左右的偏差. 在网上搜索了很多,都说这个是测绘局为了保密故意弄成这样的.gps全球定位系统获得 ...

  8. Google Map API 学习四

  9. Google Map API 使用总结

    Google Map API (一):显示一个最基本的地图 1 实现一个地图:<head>中引用: <script type="text/javascript" ...

随机推荐

  1. Android4.0 -- UI控件之 Menu 菜单的的使用(一)

      这一讲开始我们来讲一下Android中菜单的使用方法,菜单是应用中的普通的组件,主要是提供友好和专注的用户体验,你可以在你的Activity中使用 Menu APIs 来提供用户动作和其他选项的操 ...

  2. 动态规划(DP计数):HDU 5116 Everlasting L

    Matt loves letter L.A point set P is (a, b)-L if and only if there exists x, y satisfying:P = {(x, y ...

  3. FileUpload上传文件无法获取文件名

    原因:将FileUpload控件放到了UpdatePannel控件中了 解决办法:将FileUpload控件位置移动到UpdatePannel控件外面

  4. leecode 排列的学习

    前面写过3个排列.这里再写一次. 1.全部都不重复https://oj.leetcode.com/problems/permutations/ (使用交换法)只是本人对c++ stl不熟,不会把排列结 ...

  5. 曾经记录——asp.net中的点滴

    “<%#....%>”这是数据绑定,里面可以调用C#的方法,比如在数据控件里执行绑定某个字段<%# Eval("Name")%>这样帮顶一个Name的字段. ...

  6. 向老项目JSP集成JSTL遇到的问题

    today,I Failed to load or instantiate TagLibraryValidator class - JstlCoreTLV <%@ page contentTyp ...

  7. vijosP1038 添加括号

    vijosP1038 添加括号 链接:https://vijos.org/p/1038 [思路] 区间DP. 本题的关键在于如何输出解.对于求和表达式而言可以用一个p[][]记录决策然后递归输出,对于 ...

  8. 大众点评的大数据实践-CSDN.NET

    大众点评的大数据实践-CSDN.NET 大众点评的大数据实践 爬虫工程师成大数据时代的"宠儿" - 杭州新闻中心 - 杭州网 爬虫工程师成大数据时代的"宠儿"

  9. nyoj 353 3D dungeon

    3D dungeon 时间限制:1000 ms  |  内存限制:65535 KB 难度:2   描述 You are trapped in a 3D dungeon and need to find ...

  10. Mockito测试

    Mockito 一 mockito基本概念 Mock测试是单元测试的重要方法之一,而Mockito作为一个流行的Mock框架,简单易学,且有非常简洁的API,测试代码的可读性很高. Mock测试就是在 ...