Google Map API v2 (四)----- 导航路径
仍然是建议个异步小任务
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 (四)----- 导航路径的更多相关文章
- Android中Google地图路径导航,使用mapfragment地图上画出线路(google map api v2)详解
在这篇里我们只聊怎么在android中google map api v2地图上画出路径导航,用mapfragment而不是mapview,至于怎么去申请key,manifest.xml中加入的权限,系 ...
- Google Map API v2 步步为营(一) ----- 初见地图
官方文档:https://developers.google.com/maps/documentation/android/start?hl=zh-CN 先谷歌后百度.使用google的api基本上按 ...
- google map api v2的使用详细过程,图文并茂(原创)
上一篇中说到怎么获取key,下面来介绍怎么使用key来显示google地图 步骤1:eclipse上打开android SDK Manager,安装google play services. 步骤2: ...
- Google Map API v2 (三)----- 地图上添加标记(Marker),标记info窗口,即指定经纬度获取地址字符串
接上篇 http://www.cnblogs.com/inkheart0124/p/3536322.html 1,在地图上打个标记 private MarkerOptions mMarkOption; ...
- Google Map API V2密钥申请
之前用的都是v1,用的是MapView,好吧,仅仅能认命了.废话不再多说,開始android 的Google Maps Android API v2吧 之前參考了http://www.cnblogs. ...
- Google Map API v2 步步为营 (二)----- Location
接上篇. 改造一下MapsActivity: public class MapsActivity extends Activity implements LocationListener, InfoW ...
- Google Map API v2 番外篇 关于gps位置偏差及修正方法探讨
我的手机是M35C,在我自己的map activity中,通过gps获取到的经纬度比实际地址总是有500米左右的偏差. 在网上搜索了很多,都说这个是测绘局为了保密故意弄成这样的.gps全球定位系统获得 ...
- Google Map API 学习四
- Google Map API 使用总结
Google Map API (一):显示一个最基本的地图 1 实现一个地图:<head>中引用: <script type="text/javascript" ...
随机推荐
- ANDROID_MARS学习笔记_S02_014_GSON解析JSON串为对象
package com.json2; import android.app.Activity; import android.os.Bundle; import android.view.View; ...
- ANDROID_MARS学习笔记_S04_006_用获取access_token,access_token_secrect
一.代码流程 1.MainActivity会开启PrepareRequestTokenActivity 2.PrepareRequestTokenActivity会根据配置文件的CONSUMER_KE ...
- jap中文转码
因为js url在传值的过程中使用的是js自己默认的字符集编码规则,我们必须把它转成属于我们自己的编码规格,JSP页面 url=encodeURI(encodeURI(url)); //用了2次enc ...
- STL set multiset map multimap unordered_set unordered_map example
I decide to write to my blogs in English. When I meet something hard to depict, I'll add some Chines ...
- 【HDOJ】4513 吉哥系列故事——完美队形II
这题目上学期就看了,不过最近发现可以用马拉车来解,而且还是基本算法. 稍微对回文串成立条件变形一下即可. /* 4513 */ #include <iostream> #include & ...
- 《大数据Spark企业级实战 》
基本信息 作者: Spark亚太研究院 王家林 丛书名:决胜大数据时代Spark全系列书籍 出版社:电子工业出版社 ISBN:9787121247446 上架时间:2015-1-6 出版日期:20 ...
- Makefile中include、-include、sinclude的区别
如果指示符“include”指定的文件不是以斜线开始(绝对路径,如/usr/src/Makefile...),而且当前目录下也不存在此文件:make将根据文件名试图在以下几个目录下查找:首先,查找使用 ...
- 【转】修改eclipse中的M2_REPO变量
转自:http://superseven.iteye.com/blog/1625429 从eclipse中增加了maven2的插件之后,maven默认的本地库的路径是${user}/.m2/repos ...
- 关于Session
转自:http://blog.csdn.net/wang379275614/article/details/9627755 Session理解: Session:在计算机中,尤其是在网络应用中,称 ...
- [转]NHibernate之旅(9):探索父子关系(一对多关系)
本节内容 引入 NHibernate中的集合类型 建立父子关系 父子关联映射 结语 引入 通过前几篇文章的介绍,基本上了解了NHibernate,但是在NHibernate中映射关系是NHiberna ...