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" ...
随机推荐
- 【Xamarin挖墙脚系列:学习资料大放送】
原文:[Xamarin挖墙脚系列:学习资料大放送] 最靠谱的还是官方的文档,英文的,借着翻译工具,硬看吧.还能学习英文........... https://developer.xamarin.com ...
- 使用@ResponseBody 出现错误Could not find acceptable representation
org.springframework.web.HttpMediaTypeNotAcceptableException: Could not find acceptable representatio ...
- 【HDOJ】2721 Persistent Bits
题目有点长,但是题意说的很清楚.位操作. #include <stdio.h> ]; int main() { int a, b, c, s; int i, j, k, n, tmp, m ...
- 人一生必看的100部电影(全球最佳电影排名榜TOP250)
人一生必看的100部电影(全球最佳电影排名榜TOP250) 人的一生能看多少部电影?假设我们每周都看一部,从10岁看到80岁将会看3640部.但是我们也不可能喜欢这全部的电影.大多数的可能,我们会根据 ...
- C#语言的几个层次
接到一位前不久C#培训学员的来信,这位学员虽然以前功底欠缺,但学习劲头很足,在培训中成长很快.即便基本吃透<.NET框架(修订版)>还嫌不够过瘾,一心要成为高手中的高手.来信的目的是希望我 ...
- 实验记录贴 —— 账号同步实验 RTX 和 LDAP(AD域)
目前,公司有多个系统,RTX,邮箱(MD),OA,NC. 这些系统之间,如果要实现单点登录的话,账户肯定需要同步,或者某一种映射机制. 如果所有数据都和中央账号数据库(LDAP,这里是AD域)看齐,那 ...
- 【转】提供android 5.0 AOSP源码下载
http://blog.csdn.net/innost/article/details/41148335 android-5.0.tar.gz 115网盘礼包码:5lbcl16a1k7q http:/ ...
- Gilde加载网络图片(一)
前两天 一个朋友要在本地加载几M的大图 用于用户滚动查看.按照思路 是压缩后加载显示但是这样会不清晰, 其实gilde用来加载图片 很牛掰 于是了解一下 下面贴上两个工具类: package com. ...
- HDU-1253 胜利大逃亡 (BFS)
此题可以做为三维深搜模板题.. 胜利大逃亡 Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Ot ...
- HDU-2710 Max Factor
看懂: Max Factor Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) T ...