问题描述
目前,我用这code,从一个地址到另一个询问谷歌地图的方向,然后,我只是画它从它的GeometryCollection一个图形页面。但是,这还不够我也需要提取的KML预计总工期。有人可以给一个小样本code来帮助我?谢谢
StringBuilder的urlString =新的StringBuilder();
urlString.append(http://maps.google.com/maps?f=d&hl=en);
urlString.append(&放大器; SADDR =); //从
urlString.append(Double.toString((双)src.getLatitudeE6()/ 1.0E6));
urlString.append(,);
urlString.append(Double.toString((双)src.getLongitudeE6()/ 1.0E6));
urlString.append(&放大器; DADDR =); //给
urlString.append(Double.toString((双)dest.getLatitudeE6()/ 1.0E6));
urlString.append(,);
urlString.append(Double.toString((双)dest.getLongitudeE6()/ 1.0E6));
urlString.append(&放大器,即= UTF8&功放; 0安培; OM = 0&放大器;输出= KML);
//Log.d("xxx,URL =+ urlString.toString());
//获取KML(XML)文档。并解析它来获得坐标(方向线)。
文档DOC = NULL;
HttpURLConnection类的URLConnection = NULL;
网址URL = NULL;
尝试
{
URL =新的URL(urlString.toString());
的URLConnection =(HttpURLConnection类)url.openConnection();
urlConnection.setRequestMethod(GET);
urlConnection.setDoOutput(真正的);
urlConnection.setDoInput(真正的);
urlConnection.connect();
DBF = DocumentBuilderFactory.newInstance();
DB = dbf.newDocumentBuilder();
文档= db.parse(urlConnection.getInputStream());
如果(doc.getElementsByTagName(GeometryCollection的)的getLength()> 0)
{
//字符串路径= doc.getElementsByTagName("GeometryCollection").item(0).getFirstChild().getFirstChild().getNodeName();
字符串路径= doc.getElementsByTagName("GeometryCollection").item(0).getFirstChild().getFirstChild().getFirstChild().getNodeValue() ;
//Log.d("xxx","path="+路径);
的String []对= path.split();
串[] lngLat =双[0] .split(,); // lngLat [0] =经度lngLat [1] =纬度lngLat [2] =高度
// SRC
的GeoPoint startGP =新的GeoPoint((int)的(Double.parseDouble(lngLat [1])* 1E6),(int)的(Double.parseDouble(lngLat [0])* 1E6));
。mMapView01.getOverlays()增加(新MyOverLay(startGP,startGP,1));
的GeoPoint GP1;
的GeoPoint GP2 = startGP;
的for(int i = 1; I< pairs.length;我++)//最后一个会崩溃
{
lngLat =对[I] .split(,);
GP1 = GP2;
// 小心!对于GeoPoint对象,第一:纬度,第二:经度
GP2 =新的GeoPoint((int)的(Double.parseDouble(lngLat [1])* 1E6),(int)的(Double.parseDouble(lngLat [0])* 1E6));
。mMapView01.getOverlays()增加(新MyOverLay(GP1,gp2,2,颜色));
//Log.d("xxx","pair:+对[I]);
}
。mMapView01.getOverlays()增加(新MyOverLay(DEST,DEST,3)); //使用默认颜色
}
}赶上(MalformedURLException异常E){
e.printStackTrace();
}赶上(IOException异常E){
e.printStackTrace();
}赶上(的SAXException E){
e.printStackTrace();
}赶上(的ParserConfigurationException E){
e.printStackTrace();
}
}
使用新的 API。持续时间在响应中返回和KML,我认为断谷歌地图许可证条款反正不多解析。
更新。确定使用JSON输出,它解析内置了Android系统。
前<一href="http://maps.google.com/maps/api/directions/json?origin=Chicago,IL&destination=Los+Angeles,CA&waypoints=Joplin,MO|网址
响应是
{
状态:OK,
路线:[{
摘要:I-40 W,
腿:[{
脚步: [ {
travel_mode:驾驶,
START_LOCATION:{
纬度:41.8507300,
液化天然气:-87.6512600
},
END_LOCATION:{
纬度:41.8525800,
液化天然气:-87.6514100
},
折线:{
点:一〜L〜FJK〜uOwHJy @ P,
层次:B B?
},
持续时间:{
值:19,
文:1分钟
},
html_instructions:头\ u003cb \ u003enorth \ u003c / B \上\ u003cb \ u003eS摩根圣\ u003c / B \ u003e对\ u003cb \ u003eW塞马克路\ u003c / B \ u003e u003e
距离: {
值:207,
文:0.1英里
}
},
...
...额外的步骤这个腿
...
......这条路线的附加腿
持续时间:{
值:74384,
文:20.小时40分钟
},
距离: {
值:2137146,
文:1328 MI
},
START_LOCATION:{
纬度:35.4675602,
液化天然气:-97.5164276
},
END_LOCATION:{
纬度:34.0522342,
液化天然气:-118.2436849
},
START_ADDRESS:俄克拉何马城,OK,USA,
end_address:洛杉矶,加利福尼亚,美国
}],
版权:地图数据©2010谷歌,桑伯恩
overview_polyline:{
点: "a~l~Fjk~uOnzh@vlbBtc~@tsE`vnApw{A`dw@~w\\|tNtqf@l{Yd_Fblh@rxo@b}@xxSfytAblk@xxaBeJxlcBb~t@zbh@jc|Bx}C`rv@rw|@rlhA~dVzeo@vrSnc}Axf]fjz@xfFbw~@dz{A~d{A|zOxbrBbdUvpo@`cFp~xBc`Hk@nurDznmFfwMbwz@bbl@lq~@loPpxq@bw_@v|{CbtY~jGqeMb{iF|n\\~mbDzeVh_Wr|Efc\\x`Ij{kE}mAb~uF{cNd}xBjp]fulBiwJpgg@|kHntyArpb@bijCk_Kv~eGyqTj_|@`uV`k|DcsNdwxAott@r}q@_gc@nu`CnvHx`k@dse@j|p@zpiAp|gEicy@`omFvaErfo@igQxnlApqGze~AsyRzrjAb__@ftyB}pIlo_BflmA~yQftNboWzoAlzp@mz`@|}_@fda@jakEitAn{fB_a]lexClshBtmqAdmY_hLxiZd~XtaBndgC",
层次:BBBAAAAABAABAAAAAABBAAABBAAAABBAAABABAAABABBAABAABAAAABABABABBABAABB
},
警告:[],
waypoint_order:[0,1]
}]
}
让我们假设你使用标准的HTTP客户端库,现在有一个字符串的响应。
的JSONObject的JSONObject =新的JSONObject(响应); //解析响应成JSON对象
JSONObject的routeObject = jsonObject.getJSONObject(路线); //拉出路线对象
JSONObject的durationObject = jsonObject.getJSONObject(期限); //拉出持续时间对象
字符串长度= durationObject.getString(文字); //这应该是持续时间文本值(20小时40分钟)
您将不得不将所有在一个try-catch块捕获 JSONExceptions
。
At the moment I am using this code to inquire google maps for directions from an address to another one, then I simply draw it on a mapview from its GeometryCollection. But yet this isn't enough I need also to extract the total expected duration from the kml. can someone give a little sample code to help me? thanks
StringBuilder urlString = new StringBuilder();
urlString.append("http://maps.google.com/maps?f=d&hl=en");
urlString.append("&saddr=");//from
urlString.append( Double.toString((double)src.getLatitudeE6()/1.0E6 ));
urlString.append(",");
urlString.append( Double.toString((double)src.getLongitudeE6()/1.0E6 ));
urlString.append("&daddr=");//to
urlString.append( Double.toString((double)dest.getLatitudeE6()/1.0E6 ));
urlString.append(",");
urlString.append( Double.toString((double)dest.getLongitudeE6()/1.0E6 ));
urlString.append("&ie=UTF8&0&om=0&output=kml");
//Log.d("xxx","URL="+urlString.toString());
// get the kml (XML) doc. And parse it to get the coordinates(direction route).
Document doc = null;
HttpURLConnection urlConnection= null;
URL url = null;
try
{
url = new URL(urlString.toString());
urlConnection=(HttpURLConnection)url.openConnection();
urlConnection.setRequestMethod("GET");
urlConnection.setDoOutput(true);
urlConnection.setDoInput(true);
urlConnection.connect();
dbf = DocumentBuilderFactory.newInstance();
db = dbf.newDocumentBuilder();
doc = db.parse(urlConnection.getInputStream());
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 = pairs[0].split(","); // lngLat[0]=longitude lngLat[1]=latitude lngLat[2]=height
// src
GeoPoint startGP = new GeoPoint((int)(Double.parseDouble(lngLat[1])*1E6),(int)(Double.parseDouble(lngLat[0])*1E6));
mMapView01.getOverlays().add(new MyOverLay(startGP,startGP,1));
GeoPoint gp1;
GeoPoint gp2 = startGP;
for(int i=1;i<pairs.length;i++) // the last one would be crash
{
lngLat = pairs[i].split(",");
gp1 = gp2;
// watch out! For GeoPoint, first:latitude, second:longitude
gp2 = new GeoPoint((int)(Double.parseDouble(lngLat[1])*1E6),(int)(Double.parseDouble(lngLat[0])*1E6));
mMapView01.getOverlays().add(new MyOverLay(gp1,gp2,2,color));
//Log.d("xxx","pair:" + pairs[i]);
}
mMapView01.getOverlays().add(new MyOverLay(dest,dest, 3)); // use the default color
}
}catch (MalformedURLException e){
e.printStackTrace();
}catch (IOException e){
e.printStackTrace();
}catch (SAXException e){
e.printStackTrace();
} catch (ParserConfigurationException e) {
e.printStackTrace();
}
}
Use the new Directions api. The duration is returned in the response and no more parsing of KML, which I think breaks the Google maps license terms anyways.
Update. Ok use the JSON output, with it parsing is built into Android.
Ex url
Response is
{
"status": "OK",
"routes": [ {
"summary": "I-40 W",
"legs": [ {
"steps": [ {
"travel_mode": "DRIVING",
"start_location": {
"lat": 41.8507300,
"lng": -87.6512600
},
"end_location": {
"lat": 41.8525800,
"lng": -87.6514100
},
"polyline": {
"points": "a~l~Fjk~uOwHJy@P",
"levels": "B?B"
},
"duration": {
"value": 19,
"text": "1 min"
},
"html_instructions": "Head \u003cb\u003enorth\u003c/b\u003e on \u003cb\u003eS Morgan St\u003c/b\u003e toward \u003cb\u003eW Cermak Rd\u003c/b\u003e",
"distance": {
"value": 207,
"text": "0.1 mi"
}
},
...
... additional steps of this leg
...
... additional legs of this route
"duration": {
"value": 74384,
"text": "20 hours 40 mins"
},
"distance": {
"value": 2137146,
"text": "1,328 mi"
},
"start_location": {
"lat": 35.4675602,
"lng": -97.5164276
},
"end_location": {
"lat": 34.0522342,
"lng": -118.2436849
},
"start_address": "Oklahoma City, OK, USA",
"end_address": "Los Angeles, CA, USA"
} ],
"copyrights": "Map data ©2010 Google, Sanborn",
"overview_polyline": {
"points": "a~l~Fjk~uOnzh@vlbBtc~@tsE`vnApw{A`dw@~w\\|tNtqf@l{Yd_Fblh@rxo@b}@xxSfytAblk@xxaBeJxlcBb~t@zbh@jc|Bx}C`rv@rw|@rlhA~dVzeo@vrSnc}Axf]fjz@xfFbw~@dz{A~d{A|zOxbrBbdUvpo@`cFp~xBc`Hk@nurDznmFfwMbwz@bbl@lq~@loPpxq@bw_@v|{CbtY~jGqeMb{iF|n\\~mbDzeVh_Wr|Efc\\x`Ij{kE}mAb~uF{cNd}xBjp]fulBiwJpgg@|kHntyArpb@bijCk_Kv~eGyqTj_|@`uV`k|DcsNdwxAott@r}q@_gc@nu`CnvHx`k@dse@j|p@zpiAp|gEicy@`omFvaErfo@igQxnlApqGze~AsyRzrjAb__@ftyB}pIlo_BflmA~yQftNboWzoAlzp@mz`@|}_@fda@jakEitAn{fB_a]lexClshBtmqAdmY_hLxiZd~XtaBndgC",
"levels": "BBBAAAAABAABAAAAAABBAAABBAAAABBAAABABAAABABBAABAABAAAABABABABBABAABB"
},
"warnings": [ ],
"waypoint_order": [ 0, 1 ]
} ]
}
Let's say you used the standard http client libraries and now have the response in a String.
JSONObject jsonObject = new JSONObject(response); // parse response into json object
JSONObject routeObject = jsonObject.getJSONObject("route"); // pull out the "route" object
JSONObject durationObject = jsonObject.getJSONObject("duration"); // pull out the "duration" object
String duration = durationObject.getString("text"); //this should be the duration text value (20 hours 40 mins)
You will have to wrap all that in a try-catch block to catch the JSONExceptions
.
这篇关于从maps.google.com方向Android开时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!