仍然是建议个异步小任务

 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. php 文件上传 以及保存在本地的乱码问题处理

    要知道两点: ①浏览器传到PHP程序中是UTF-8编码 ②PHP程序保存上传的文件,要转换成GBK编码才保存在本地中,否则如果直接使用浏览器传过来的文件名保存在本地,会出现文件名乱码. <?ph ...

  2. Android java程序获取assets资产文件

    AssetManager assetManager=this.getAssets(); inputStream = assetManager.open("test.xml");

  3. dynamic 使用

    dynamic a = , B = }; Console.WriteLine("a.A=" + a.A); dynamic b = new Dictionary<string ...

  4. linux系统配置文件和用户配置文件及其作用

    我的博客:www.while0.com /etc/issue 未登陆时控制台显示的文字 /etc/issue.net 远程登陆时控制台显示的文字 /etc/motd 用户登陆时显示的文字 这里先提供两 ...

  5. perl静态编译DBD

    编译DBD 项目中经常使用perl,但perl在连接数据库时,需要依赖DBI,DBD驱动,但默认安装DBD驱动时,需要依赖数据库的lib库. 比如perl连接MySQL,需要安装MySQL clien ...

  6. 转自 x_x的百度空间

    空华人生         by 淡漠的心情 昨天,又昨天. 今天,又今天. 明天,又明天. 日历渐渐稀薄,忽然发现,那是时间的痕迹. 似乎,总是在麻木的等待. 何时,才能历尽. 再算算,我又还有多少天 ...

  7. 解决ASP.NET MVC AllowAnonymous属性无效导致无法匿名访问控制器的问题

    在ASP.NET MVC项目中,一般都要使用身份验证和权限控制,但总有部分网页是可以匿名访问的.使用AllowAnonymous属性就可以指定需要匿名访问的控制器,从而跳过身份验证. 但是今天却遇到一 ...

  8. Chrome 浏览器地址栏直接搜索太慢的解决方案

    用Chrome经常直接把要搜索的内容写在地址栏, 回国就搜索,但最近发现搜索结果出来得太慢,要刷新好几次才行. 解决方案如下: 打开Chrome的"设置", 找到”管理搜索引擎“, ...

  9. java List遍历的方法

    List可以用序号来遍历,但通常推荐使用iterator来遍历Iterator itr = list.iterator();while (itr.hasNext()) { Object nextObj ...

  10. FileUtils类介绍

    Java的文件操作太基础,缺乏很多实用工具,比如对目录的操作,支持就非常的差了.如果你经常用Java操作文件或文件夹,你会觉得反复编写这些代码是令人沮丧的问题,而且要大量用到递归. 下面是的一个解决方 ...