Google Map API v2 (四)----- 导航路径

时间:2023-03-09 00:50:10
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 Map API v2 (四)----- 导航路径

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