Android 取得 Google Map 導航資料

下面連結是可以直接取用
用來轉換Google Map的導航路徑的
[Android] Google Map(三)– 取得導航資訊
http://www.dotblogs.com.tw/alonstar/archive/2011/05/26/26310.aspx


private List _points = new ArrayList();

public List GetDirection()
{
String mapAPI = "http://maps.google.com/maps/api/directions/json?origin={0}&destination={1}&language=zh-TW&sensor=true";
String url = MessageFormat.format(mapAPI, _from, _to);

HttpGet get = new HttpGet(url);
String strResult = "";
try
{

HttpParams httpParameters = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(httpParameters, 3000);
HttpClient httpClient = new DefaultHttpClient(httpParameters);

HttpResponse httpResponse = null;
httpResponse = httpClient.execute(get);

if (httpResponse.getStatusLine().getStatusCode() == 200)
{
strResult = EntityUtils.toString(httpResponse.getEntity());

JSONObject jsonObject = new JSONObject(strResult);
JSONArray routeObject = jsonObject.getJSONArray("routes");
String polyline = routeObject.getJSONObject(0).getJSONObject("overview_polyline").getString("points");

if (polyline.length() > 0)
{
decodePolylines(polyline);
}

}
}
catch (Exception e)
{
Log.e("map", "MapRoute:" + e.toString());
}

return _points;
}

private void decodePolylines(String poly)
{
int len = poly.length();
int index = 0;
int lat = 0;
int lng = 0;

while (index < len) { int b, shift = 0, result = 0; do { b = poly.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 = poly.charAt(index++) - 63;
result |= (b & 0x1f) << shift; shift += 5; } while (b >= 0x20);
int dlng = ((result & 1) != 0 ? ~(result >> 1) : (result >> 1));
lng += dlng;

GeoPoint p = new GeoPoint((int) (((double) lat / 1E5) * 1E6), (int) (((double) lng / 1E5) * 1E6));
_points.add(p);

}
}

留言

熱門文章