单击地图时我正在获取路线,但是我想在自动加载地图后手动提供目的地位置,路线将显示如何执行此操作,请发送代码..
问问题
3217 次
2 回答
0
请检查此代码:
1)调用谷歌地图服务获取路线信息:
StringBuilder urlString = new StringBuilder();
urlString
.append("http://maps.googleapis.com/maps/api/directions/json?sensor=true");
urlString.append("&origin=");// from
urlString.append(curLocation.getLatitude());
urlString.append(",");
urlString.append(curLocation.getLongitude());
urlString.append("&destination=");// to
urlString.append(carPark.getLat());
urlString.append(",");
urlString.append(carPark.getLng());
Log.d("xxx", "URL=" + urlString.toString());
aq.ajax(urlString.toString(), JSONObject.class, this,
"gmapCallback");
2)解析来自谷歌的响应字符串。
public GeoPoint[] parseRoute(String content) {
Document doc = null;
GeoPoint[] route = null;
try {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
doc = db.parse(new ByteArrayInputStream(content.getBytes()));
if (doc.getElementsByTagName("GeometryCollection").getLength() > 0) {
// String path =
// doc.getElementsByTagName("GeometryCollection").item(0).getFirstChild().getFirstChild().getNodeName();
String path = doc.getElementsByTagName("GeometryCollection")
.item(0).getFirstChild().getFirstChild()
.getFirstChild().getNodeValue();
Log.d("xxx", "path=" + path);
String[] pairs = path.split(" ");
String[] lngLat;
// lngLat[0]=longitude
// lngLat[1]=latitude
// lngLat[2]=height
route = new GeoPoint[pairs.length];
GeoPoint gp;
for (int i = 0; i < pairs.length; i++) {
lngLat = pairs[i].split(",");
gp = new GeoPoint(
(int) (Double.parseDouble(lngLat[1]) * 1E6),
(int) (Double.parseDouble(lngLat[0]) * 1E6));
route[i] = gp;
Log.d("xxx", "pair:" + pairs[i]);
}
}
} catch (Exception e) {
Log.e(Utils.class.getName(), e.getMessage());
}
return route;
}
于 2013-09-13T09:15:30.770 回答