在使用NullPointerException异常GMapsV2Direction [英] NullPointerException when using GMapsV2Direction
问题描述
我一直试图为过去的这几天运行时错误修复不得要领,不知道为什么它的投掷一个NullPointerException异常时,它会创建时getDirection方法被称为节点树。由经纬度坐标和方向类型正在通过getDocument()方法通过调试器来判断,但一个文件没有被从getDocument()方法返回
公开文件getDocument(经纬度开始,经纬度结束,字符串模式){
字符串URL =http://maps.googleapis.com/maps/api/directions/xml?
+出身=+ start.latitude +,+ start.longitude
+与&目的地=+ end.latitude +,+ end.longitude
+&放大器;传感器=假放;单位=指标和放大器;模式=驾驶; 尝试{
HttpClient的HttpClient的=新DefaultHttpClient();
HttpContext的localContext =新BasicHttpContext();
HttpPost httpPost =新HttpPost(URL);
HTT presponse响应= httpClient.execute(httpPost,localContext);
。InputStream的时间= response.getEntity()的getContent();
的DocumentBuilder建设者= DocumentBuilderFactory.newInstance()newDocumentBuilder()。
文档的文档= builder.parse(上);
返回文档;
}赶上(例外五){
e.printStackTrace();
}
返回null;
}公众的ArrayList<&经纬度GT; getDirection(DOC文件){
节点列表NL1,NL2,NL3,NL4;
ArrayList的<&经纬度GT; listGeopoints =新的ArrayList<&经纬度GT;();
双时间= 0;
NL1 = doc.getElementsByTagName(步);
如果(nl1.getLength()大于0){
的for(int i = 0; I< nl1.getLength();我++){
双T = 0;
节点节点1 = nl1.item(ⅰ);
NL2 = node1.getChildNodes(); 节点locationNode = nl2.item(getNodeIndex(NL2的start_location));
NL3 = locationNode.getChildNodes();
节点latNode = nl3.item(getNodeIndex(NL3纬度));
双纬度= Double.parseDouble(latNode.getTextContent());
节点lngNode = nl3.item(getNodeIndex(NL3,LNG));
双经度= Double.parseDouble(lngNode.getTextContent());
listGeopoints.add(新经纬度(纬度,经度)); locationNode = nl2.item(getNodeIndex(NL2,折线));
NL3 = locationNode.getChildNodes();
latNode = nl3.item(getNodeIndex(NL3,点));
ArrayList的<&经纬度GT; ARR =去$ C $℃聚(latNode.getTextContent());
对于(INT J = 0; J< arr.size(); J ++){
listGeopoints.add(新经纬度(arr.get(J).latitude,arr.get(J).longitude));
} locationNode = nl2.item(getNodeIndex(NL2END_LOCATION));
NL3 = locationNode.getChildNodes();
latNode = nl3.item(getNodeIndex(NL3纬度));
纬度= Double.parseDouble(latNode.getTextContent());
lngNode = nl3.item(getNodeIndex(NL3,LNG));
经度= Double.parseDouble(lngNode.getTextContent());
listGeopoints.add(新经纬度(纬度,经度));
locationNode = nl2.item(getNodeIndex(NL2,持续时间));
NL4 = locationNode.getChildNodes();
节点节点2 = nl4.item(getNodeIndex(NL4,价值));
T = Double.parseDouble(node2.getTextContent());
时间=时间+ T;
}
Log.i(时间,将String.valueOf(时间));
} 返回listGeopoints;
}
下面是我使用调用getDirection方法的功能:
公共无效onClick_GetDirections(视图v){
MyGPSToolDirections MD =新MyGPSToolDirections();经纬度fromPosition =新的经纬度(13.68714,100.53525);
经纬度toPosition =新的经纬度(13.68366,100.53900);文档DOC1 = md.getDocument(fromPosition,toPosition,MyGPSToolDirections.MODE_DRIVING);ArrayList的<&经纬度GT; directionPoint = md.getDirection(DOC1);
的PolylineOptions rectLine =新的PolylineOptions()宽(3)。颜色(Color.RED)。的for(int i = 0; I< directionPoint.size();我++){
rectLine.add(directionPoint.get(I));GPSToolMap.addPolyline(rectLine);
}
堆栈跟踪:
08-07 17:14:31.682:E / AndroidRuntime(2201):致命异常:主要
08-07 17:14:31.682:E / AndroidRuntime(2201):java.lang.IllegalStateException:无法执行活动的方法
08-07 17:14:31.682:E / AndroidRuntime(2201):在android.view.View $ 1.onClick(View.java:3599)
08-07 17:14:31.682:E / AndroidRuntime(2201):在android.view.View.performClick(View.java:4204)
08-07 17:14:31.682:E / AndroidRuntime(2201):在android.view.View $ PerformClick.run(View.java:17355)
08-07 17:14:31.682:E / AndroidRuntime(2201):在android.os.Handler.handleCallback(Handler.java:725)
08-07 17:14:31.682:E / AndroidRuntime(2201):在android.os.Handler.dispatchMessage(Handler.java:92)
08-07 17:14:31.682:E / AndroidRuntime(2201):在android.os.Looper.loop(Looper.java:137)
08-07 17:14:31.682:E / AndroidRuntime(2201):在android.app.ActivityThread.main(ActivityThread.java:5041)
08-07 17:14:31.682:E / AndroidRuntime(2201):在java.lang.reflect.Method.invokeNative(本机方法)
08-07 17:14:31.682:E / AndroidRuntime(2201):在java.lang.reflect.Method.invoke(Method.java:511)
08-07 17:14:31.682:E / AndroidRuntime(2201):在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:793)
08-07 17:14:31.682:E / AndroidRuntime(2201):在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
08-07 17:14:31.682:E / AndroidRuntime(2201):在dalvik.system.NativeStart.main(本机方法)
08-07 17:14:31.682:E / AndroidRuntime(2201):因:java.lang.reflect.InvocationTargetException
08-07 17:14:31.682:E / AndroidRuntime(2201):在java.lang.reflect.Method.invokeNative(本机方法)
08-07 17:14:31.682:E / AndroidRuntime(2201):在java.lang.reflect.Method.invoke(Method.java:511)
08 -07 17:14:31.682:E / AndroidRuntime(2201):在android.view.View $ 1.onClick(View.java:3594)
08-07 17:14:31.682:E / AndroidRuntime(2201):11 ...更多
08-07 17:14:31.682:E / AndroidRuntime(2201):因:显示java.lang.NullPointerException
08-07 17:14:31.682:E / AndroidRuntime(2201):在com.L00081183.mygpstool.MyGPSToolDirections.getDirection(MyGPSToolDirections.java:54)
08-07 17:14:31.682:E / AndroidRuntime(2201):在com.L00081183.mygpstool.MainActivity.onClick_GetDirections(MainActivity.java:145)
这是我用来获取谷歌API方向行驶方向。这使用JSON。可能是这可以帮助你。
本类构造查询
公共类DirectionAPI扩展AbstractQuery { 私有静态最终字符串URL =http://maps.googleapis.com/maps/api/directions/; 私有静态最终布尔USE_SENSOR = TRUE; / **
*为必填参数
* / / **
*指定用于计算路线的起始位置
* /
私有静态最后弦乐PARAM_ORIGIN =原产地; / **
*指定结束位置计算路线。
* /
私有静态最后弦乐PARAM_DESTINATION =目的地; / **
*指示方向的请求是否来自装有位置传感器的设备。
*此值必须是真还是假
* /
私有静态最后弦乐PARAM_SENSOR =传感器; / **
*可选参数
* / / **
*指定的出行方式计算路线时要使用
* /
私有静态最后弦乐PARAM_TRAVEL_MODE =模式; / **
*出行方式
* /
公共静态最后弦乐TRAVEL_MODE_DRIVING =驾驶;
公共静态最后弦乐TRAVEL_MODE_BICYCLING =骑自行车;
公共静态最后弦乐TRAVEL_MODE_TRANSIT =过境;
公共静态最后弦乐TRAVEL_MODE_WALKING =行走; / **
*指定显示结果时使用的单位系统
* /
私有静态最后弦乐PARAM_UNIT_SYSTEM =单位; / **
*单位系统
* /
公共静态最后弦乐UNIT_SYSTEM_METRIC =公制;
公共静态最后弦乐UNIT_SYSTEM_IMPERIAL =帝王; / **
*路标通过指定位置路由,改变路由(S)
* /
私有静态最后弦乐PARAM_WAYPOINTS =航点; / **
*指定使用提供的路点的路线可以优化,以提供
*最短的路线。如果为true,路线服务将返回重新排序
*在waypoint_order,其中外地的航点
* /
私有静态最后弦乐PARAM_OPTIMIZE_WAYPOINTS =优化; / **
*当设置为true指定路线服务可能会提供一个以上的航线
*替代在响应中。注意,提供替代路线可能会增加
*响应从服务器时间
* /
私有静态最后弦乐PARAM_PROVIDE_ROUTE_ALTERNATIVES =替代品; / **
*表示计算的路线(S)应避免指定功能。
*目前,该参数支持以下两个参数:
*通行费,用于表示计算的路线应避开收费公路/桥梁。
*高速公路表示计算的路线应避开高速公路。
* /
私有静态最后弦乐PARAM_AVOID =避; 私有静态最后弦乐AVOID_HIGHWAYS =高速公路;
私有静态最后弦乐AVOID_TOLLS =过路费; / **
*指定区域code,指定为ccTLD(顶级域)双字符值
* https://developers.google.com/maps/documentation/javascript/directions#DirectionsRegionBiasing
* /
私有静态最后弦乐PARAM_REGION =区域; 私人经纬度起源;
私人经纬度目的地; 私人地图<经纬度,布尔>航点;
私人地图<字符串,字符串> optionalParams; 公共DirectionAPI(经纬度原点,经纬度目的地){
如果(原产地== NULL ||目的地== NULL){
抛出新的NullPointerException(原产地和目的地不能为空);
}
this.origin =来源;
this.destination =目标;
optionalParams =新的HashMap<字符串,字符串>();
航点=新的HashMap<经纬度,布尔>();
} 公共无效addWaypoint(经纬度航点,布尔中途停留){
waypoints.put(航点,经停);
} 公共无效removeWaypoint(经纬度航点){
waypoints.remove(航点);
} 公共无效setTravelMode(字符串模式){
optionalParams.put(PARAM_TRAVEL_MODE,模式);
} 公共无效setUnitSystem(字符串unitSystem来){
把(PARAM_UNIT_SYSTEM,unitSystem来);
} 公共无效optimizeWaypoints(布尔优化){
把(PARAM_OPTIMIZE_WAYPOINTS,将String.valueOf(优化));
} 公共无效provideRouteAlternatives(布尔提供){
把(PARAM_PROVIDE_ROUTE_ALTERNATIVES,将String.valueOf(提供));
} 公共无效忌(布尔高速公路,通行费布尔){
如果(高速公路和放大器;!&安培;!过路费){
把(PARAM_AVOID,NULL);
}否则如果(高速公路和放大器;&安培;!过路费){
把(PARAM_AVOID,AVOID_HIGHWAYS);
}如果别人(!公路){
把(PARAM_AVOID,AVOID_TOLLS);
}其他{
把(PARAM_AVOID,AVOID_TOLLS +|+ AVOID_HIGHWAYS);
}
} 公共无效setRegion(字符串区域){
把(PARAM_REGION,区);
} 私人无效认沽(字符串键,字符串值){
如果(价值== NULL){
optionalParams.remove(键);
}其他{
optionalParams.put(键,值);
}
} 公共字符串constructQuery(){
StringBuilder的查询=新的StringBuilder(URL);
。query.append(JSON)追加(?);
。query.append(PARAM_ORIGIN).append(=)附加(origin.latitude).append(,)附加(origin.longitude)。
query.append(与&);
query.append(PARAM_DESTINATION).append(\"=\").append(destination.latitude).append(\",\").append(destination.longitude);
query.append(与&);
。query.append(PARAM_SENSOR).append(=)附加(USE_SENSOR);
对于(Map.Entry的<字符串,字符串>项:optionalParams.entrySet()){
query.append(与&);
。query.append(entry.getKey())附加(=)追加(entry.getValue())。
}
如果(waypoints.size()大于0){
query.append(与&);
query.append(PARAM_WAYPOINTS);
对于(Map.Entry的<经纬度,布尔>项:waypoints.entrySet()){
如果(!entry.getValue()){
query.append(内经);
}
LatLng值= entry.getKey();
query.append(value.latitude).append(,)附加(value.longitude)。
query.append(|);
}
query.deleteCharAt(query.length() - 1);
}
返回query.toString();
} 公共GoogleResponse的execute(){
GoogleResponse googleResponse;
尝试{
java.net.URL中的URL =新的URL(constructQuery());
HttpURLConnection的HttpURLConnection类=(HttpURLConnection类)url.openConnection();
如果(httpURLConnection.getResponse code()== 200){
的JSONObject的JSONObject = JSONParser.parse(httpURLConnection.getInputStream());
googleResponse =新GoogleResponse(jsonObject.getString(身份));
如果(googleResponse.isOk()){
googleResponse.setJsonObject(JSONObject的);
}
}其他{
googleResponse =新GoogleResponse(httpURLConnection.getResponse code());
}
}赶上(MalformedURLException的E){
Log.e(Debug.TAG,e.getMessage(),E);
googleResponse =新GoogleResponse(E);
}赶上(IOException异常五){
Log.e(Debug.TAG,e.getMessage(),E);
googleResponse =新GoogleResponse(E);
}赶上(JSONException E){
Log.e(Debug.TAG,e.getMessage(),E);
googleResponse =新GoogleResponse(E);
}
返回googleResponse;
}}
本类创建JSON对象
公共类JSONParser { 公共静态的JSONObject解析(的InputStream的InputStream){
BufferedReader中读者;
尝试{
读者=新的BufferedReader(新的InputStreamReader(
InputStream的,ISO-8859-1),8);
StringBuilder的SB =新的StringBuilder();
串线= NULL;
而((行= reader.readLine())!= NULL){
sb.append(线).append(\\ n);
}
inputStream.close();
返回新的JSONObject(sb.toString());
}赶上(UnsupportedEncodingException五){
Log.e(Debug.TAG,e.getMessage(),E);
}赶上(IOException异常五){
Log.e(Debug.TAG,e.getMessage(),E);
}赶上(JSONException E){
Log.e(Debug.TAG,e.getMessage(),E);
}
返回null;
}}
本类读取响应状态,并检查是否正常。
公共类GoogleResponse { 私有静态最后弦乐RESPONSE_OK =确定;
私有静态最后弦乐RESPONSE_ZERO_RESULT =ZERO_RESULTS;
私有静态最后弦乐RESPONSE_OVER_QUERY_LIMIT =OVER_QUERY_LIMIT;
私有静态最后弦乐RESPONSE_REQUEST_DENIED =REQUEST_DENIED;
私有静态最后弦乐RESPONSE_INVALID_REQUEST =INVALID_REQUEST; 公共静态最终诠释OK = 0;
公共静态最终诠释ERROR_ZERO_RESULT = 10;
公共静态最终诠释ERROR_OVER_QUERY_LIMIT = 11;
公共静态最终诠释ERROR_REQUEST_DENIED = 12;
公共静态最终诠释ERROR_INVALID_REQUEST = 13;
公共静态最终诠释ERROR_UNKNOWN_RESPONSE = 14; 公共静态最终诠释ERROR_MALFORMED_URL = 15;
公共静态最终诠释ERROR_IO = 16;
公共静态最终诠释ERROR_JSON = 17; 私人INT响应= -1;
私人的JSONObject的JSONObject; 公共GoogleResponse(字符串响应){
如果(response.contentEquals(RESPONSE_OK)){
this.response = OK;
}否则如果(response.contentEquals(RESPONSE_ZERO_RESULT)){
this.response = ERROR_ZERO_RESULT;
}否则如果(response.contentEquals(RESPONSE_OVER_QUERY_LIMIT)){
this.response = ERROR_OVER_QUERY_LIMIT;
}否则如果(response.contentEquals(RESPONSE_REQUEST_DENIED)){
this.response = ERROR_REQUEST_DENIED;
}否则如果(response.contentEquals(RESPONSE_INVALID_REQUEST)){
this.response = ERROR_INVALID_REQUEST;
}其他{
Log.e(Debug.TAG,未知的回应:+响应);
this.response = ERROR_UNKNOWN_RESPONSE;
}
} 公共GoogleResponse(例外五){
如果(E的instanceof MalformedURLException的){
响应= ERROR_MALFORMED_URL;
}否则如果(E的instanceof IOException异常){
响应= ERROR_IO;
}否则如果(E的instanceof JSONException){
响应= ERROR_JSON;
}
} 公共GoogleResponse(INT响应){
this.response =响应;
} 公众诠释的GetResponse(){
返回响应;
} 公共布尔isOk(){
返回响应< 10;
} 公众的JSONObject getJsonObject(){
返回的JSONObject;
} 公共无效setJsonObject(的JSONObject的JSONObject){
this.jsonObject =的JSONObject;
}
}
本类解析JSON数据。
公共类DrivingDirection { 私有静态最后弦乐TAG_ROUTES =路线; 私有静态最后弦乐TAG_WAYPOINT_ORDER =waypoint_order,其中;
私有静态最后弦乐TAG_SUMMARY =摘要;
私有静态最后弦乐TAG_BOUNDS =界限;
私有静态最后弦乐TAG_LEGS =腿;
私有静态最后弦乐TAG_WARNINGS =警告;
私有静态最后弦乐TAG_OVERVIEW_POLYLINE =overview_polyline;
私有静态最后弦乐TAG_COPYRIGHTS =著作权;
私有静态最后弦乐TAG_BOUND_NORTH_EAST =东北;
私有静态最后弦乐TAG_BOUND_SOUTH_WEST =西南; 私人字符串摘要;
私人字符串版权;
私人的ArrayList<&经纬度GT; overviewPolyline;
私人的ArrayList<&经纬度GT; smoothPolyline; 私人JSONArray警告;
私人JSONArray waypointOrder; 私人经纬度boundNorthEast;
私人经纬度boundSouthWest; 私人的ArrayList< DirectionLeg> directionLegs; 私人JSON字符串; 公共DrivingDirection(JSONObject的响应){
尝试{
JSONArray jsonArray = response.getJSONArray(TAG_ROUTES);
的JSONObject的JSONObject = jsonArray.getJSONObject(0);
总结= jsonObject.getString(TAG_SUMMARY);
版权= jsonObject.getString(TAG_COPYRIGHTS);
overviewPolyline =去codePolyline(jsonObject.getJSONObject(TAG_OVERVIEW_POLYLINE).getString(点));
警告= jsonObject.getJSONArray(TAG_WARNINGS);
waypointOrder = jsonObject.getJSONArray(TAG_WAYPOINT_ORDER);
JSONObject的边界= jsonObject.getJSONObject(TAG_BOUNDS);
boundNorthEast =间pretLatLng(bounds.getJSONObject(TAG_BOUND_NORTH_EAST));
boundSouthWest =间pretLatLng(bounds.getJSONObject(TAG_BOUND_SOUTH_WEST));
JSONArray腿= jsonObject.getJSONArray(TAG_LEGS);
directionLegs =新的ArrayList< DirectionLeg>(legs.length());
的for(int i = 0; I< legs.length();我++){
directionLegs.add(新DirectionLeg(legs.getJSONObject(ⅰ)));
}
JSON = response.toString();
}赶上(JSONException E){
Log.e(Debug.TAG,e.getMessage(),E);
}
} 公共字符串getSummary(){
返回总结;
} 公共字符串getCopyrights(){
返回著作权;
} 公众的ArrayList<&经纬度GT; getOverviewPolyline(){
返回overviewPolyline;
} 公众的ArrayList<&经纬度GT; getTotalPolyline(){
如果(smoothPolyline == NULL){
smoothPolyline =新的ArrayList<&经纬度GT;();
对于(DirectionLeg directionLeg:directionLegs){
为(DirectionStep directionStep:directionLeg.getDirectionSteps()){
smoothPolyline.addAll(directionStep.getPolyline());
}
}
}
返回smoothPolyline;
} 公共JSONArray getWarnings(){
返回警告;
} 公共JSONArray getWaypointOrder(){
返回waypointOrder;
} 公共经纬度getBoundNorthEast(){
返回boundNorthEast;
} 公共经纬度getBoundSouthWest(){
返回boundSouthWest;
} 公众的ArrayList< DirectionLeg> getDirectionLegs(){
返回directionLegs;
} 众长getDistance的(){
长途= 0L;
对于(DirectionLeg directionLeg:directionLegs){
距离+ = directionLeg.getDistance();
}
返回距离;
} 众长getDuration(){
持续时间长= 0L;
对于(DirectionLeg directionLeg:directionLegs){
时间长+ = directionLeg.getDuration();
}
返回时间;
} 公共无效addPolyline(折线折线){
polyline.setPoints(this.overviewPolyline);
} 公共静态的ArrayList<&经纬度GT;德codePolyline(字符串连接codeD){
ArrayList的<&经纬度GT;聚=新的ArrayList<&经纬度GT;();
INT指数= 0,LEN = EN coded.length();
INT纬度= 0,经度= 0;
而(指数< LEN){
INT B,移= 0,结果为0;
做{
B = EN coded.charAt(指数++) - 63;
结果| =(B&安培; 0x1F的)LT;<转移;
移动+ = 5;
}而(B> = 0x20的);
INT DLAT =((导致与放大器; 1)= 0〜(结果>→1):?(导致与GT;→1));
纬度+ = DLAT;
移= 0;
结果= 0;
做{
B = EN coded.charAt(指数++) - 63;
结果| =(B&安培; 0x1F的)LT;<转移;
移动+ = 5;
}而(B> = 0x20的);
INT DLNG =((导致与放大器; 1)= 0〜(结果>→1):?(导致与GT;→1));
LNG + = DLNG;
的LatLng P =新的经纬度((((双)纬度/ 1E5))
(((双)LNG / 1E5)));
poly.add(P);
}
返回聚;
} 私有静态经纬度间pretLatLng(JSONObject的对象){
尝试{
返回新的经纬度(object.getDouble(纬度),object.getDouble(LNG));
}赶上(JSONException E){
Log.e(Debug.TAG,e.getMessage(),E);
}
返回null;
} 公共字符串的getJSON(){
返回JSON;
} 公共静态类DirectionLeg { 私人持续时间长;
私人长途电话;
私人经纬度startLocation;
私人经纬度EndLocation组件;
私人字符串startAddress;
私人字符串endAddress;
私人JSONArray viaWaypoints;
私人的ArrayList< DirectionStep> directionSteps; 公共DirectionLeg(的JSONObject的JSONObject){
尝试{
距离= jsonObject.getJSONObject(距离)getLong之(值)。
持续时间= jsonObject.getJSONObject(期限),getLong之(值)。
startAddress = jsonObject.getString(START_ADDRESS);
endAddress = jsonObject.getString(END_ADDRESS);
viaWaypoints = jsonObject.getJSONArray(via_waypoint);
startLocation = DrivingDirection.inter pretLatLng(jsonObject.getJSONObject(START_LOCATION));
EndLocation组件= DrivingDirection.inter pretLatLng(jsonObject.getJSONObject(END_LOCATION));
JSONArray步骤= jsonObject.getJSONArray(步);
directionSteps =新的ArrayList< DirectionStep>(steps.length());
的for(int i = 0; I< steps.length();我++){
directionSteps.add(新DirectionStep(steps.getJSONObject(ⅰ)));
}
}赶上(JSONException E){
Log.e(Debug.TAG,e.getMessage(),E);
}
} 众长getDuration(){
返回时间;
} 众长getDistance的(){
返回距离;
} 公共经纬度getStartLocation(){
返回startLocation;
} 公共经纬度getEndLocation(){
返回EndLocation组件;
} 公共字符串getStartAddress(){
返回startAddress;
} 公共字符串getEndAddress(){
返回endAddress;
} 公共JSONArray getViaWaypoints(){
返回viaWaypoints;
} 公众的ArrayList< DirectionStep> getDirectionSteps(){
返回directionSteps;
}
} 公共静态类DirectionStep { 私人字符串htmlInstruction;
私人持续时间长;
私人长途电话;
私人经纬度startLocation;
私人经纬度EndLocation组件;
私人字符串travelMode;
私人字符串折线;
私人的ArrayList<&经纬度GT; polylinePoints; 公共DirectionStep(的JSONObject的JSONObject){
尝试{
htmlInstruction = jsonObject.getString(html_instructions);
持续时间= jsonObject.getJSONObject(期限),getLong之(值)。
距离= jsonObject.getJSONObject(距离)getLong之(值)。
JSONObject的startLocation = jsonObject.getJSONObject(START_LOCATION);
this.startLocation =新的经纬度(startLocation.getDouble(纬度),startLocation.getDouble(LNG));
JSONObject的EndLocation组件= jsonObject.getJSONObject(END_LOCATION);
this.endLocation =新的经纬度(endLocation.getDouble(纬度),endLocation.getDouble(LNG));
travelMode = jsonObject.getString(TRAVEL_MODE);
折线= jsonObject.getJSONObject(折线)的getString(点)。
}赶上(JSONException E){
Log.e(Debug.TAG,e.getMessage(),E);
}
} 公共字符串getHtmlInstruction(){
返回htmlInstruction;
} 众长getDuration(){
返回时间;
} 众长getDistance的(){
返回距离;
} 公共经纬度getStartLocation(){
返回startLocation;
} 公共经纬度getEndLocation(){
返回EndLocation组件;
} 公共字符串getTravelMode(){
返回travelMode;
} 公众的ArrayList<&经纬度GT; getPolyline(){
如果(polylinePoints == NULL){
polylinePoints =去codePolyline(折线);
}
返回polylinePoints;
}
}}
典型的用法。如果你愿意,你可以设置可选参数,如航点等。
经纬度开始=新的经纬度(1.11,1.11);
最终的LatLng =新经纬度(2.22,2.22);
DirectionAPI directionAPI =新DirectionAPI(开始,结束);
GoogleResponse googleResponse = directionAPI.execute();
如果(googleResponse.isOk()){
DrivingDirection drivingDirection =新DrivingDirection(googleResponse.getJsonObject());
ArrayList的<&经纬度GT;折线= drivingDirection.getTotalPolyline();
}
I've been trying to fix this run-time error for the last few days to no avail, not sure why it's throwing a NullPointerException when it creates the NodeList when the getDirection method is called. Judging by the debugger the LatLng co-ordinates and the direction type is being passed through the getDocument() method, but a document isn't being returned from the getDocument() method.
public Document getDocument(LatLng start, LatLng end, String mode) {
String url = "http://maps.googleapis.com/maps/api/directions/xml?"
+ "origin=" + start.latitude + "," + start.longitude
+ "&destination=" + end.latitude + "," + end.longitude
+ "&sensor=false&units=metric&mode=driving";
try {
HttpClient httpClient = new DefaultHttpClient();
HttpContext localContext = new BasicHttpContext();
HttpPost httpPost = new HttpPost(url);
HttpResponse response = httpClient.execute(httpPost, localContext);
InputStream in = response.getEntity().getContent();
DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
Document doc = builder.parse(in);
return doc;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
public ArrayList<LatLng> getDirection (Document doc) {
NodeList nl1, nl2, nl3, nl4;
ArrayList<LatLng> listGeopoints = new ArrayList<LatLng>();
double time=0;
nl1 = doc.getElementsByTagName("step");
if (nl1.getLength() > 0) {
for (int i = 0; i < nl1.getLength(); i++) {
double t=0;
Node node1 = nl1.item(i);
nl2 = node1.getChildNodes();
Node locationNode = nl2.item(getNodeIndex(nl2, "start_location"));
nl3 = locationNode.getChildNodes();
Node latNode = nl3.item(getNodeIndex(nl3, "lat"));
double lat = Double.parseDouble(latNode.getTextContent());
Node lngNode = nl3.item(getNodeIndex(nl3, "lng"));
double lng = Double.parseDouble(lngNode.getTextContent());
listGeopoints.add(new LatLng(lat, lng));
locationNode = nl2.item(getNodeIndex(nl2, "polyline"));
nl3 = locationNode.getChildNodes();
latNode = nl3.item(getNodeIndex(nl3, "points"));
ArrayList<LatLng> arr = decodePoly(latNode.getTextContent());
for(int j = 0 ; j < arr.size() ; j++) {
listGeopoints.add(new LatLng(arr.get(j).latitude, arr.get(j).longitude));
}
locationNode = nl2.item(getNodeIndex(nl2, "end_location"));
nl3 = locationNode.getChildNodes();
latNode = nl3.item(getNodeIndex(nl3, "lat"));
lat = Double.parseDouble(latNode.getTextContent());
lngNode = nl3.item(getNodeIndex(nl3, "lng"));
lng = Double.parseDouble(lngNode.getTextContent());
listGeopoints.add(new LatLng(lat, lng));
locationNode = nl2.item(getNodeIndex(nl2, "duration"));
nl4 = locationNode.getChildNodes();
Node node2 = nl4.item(getNodeIndex(nl4, "value"));
t = Double.parseDouble(node2.getTextContent());
time=time+t;
}
Log.i("duration", String.valueOf(time));
}
return listGeopoints;
}
Here is the function I'm using to call the getDirection method:
public void onClick_GetDirections(View v){
MyGPSToolDirections md = new MyGPSToolDirections();
LatLng fromPosition = new LatLng(13.68714, 100.53525);
LatLng toPosition = new LatLng(13.68366, 100.53900);
Document doc1 = md.getDocument(fromPosition, toPosition, MyGPSToolDirections.MODE_DRIVING);
ArrayList<LatLng> directionPoint = md.getDirection(doc1);
PolylineOptions rectLine = new PolylineOptions().width(3).color(Color.RED);
for(int i = 0 ; i < directionPoint.size() ; i++) {
rectLine.add(directionPoint.get(i));
GPSToolMap.addPolyline(rectLine);
}
Stack trace:
08-07 17:14:31.682: E/AndroidRuntime(2201): FATAL EXCEPTION: main
08-07 17:14:31.682: E/AndroidRuntime(2201): java.lang.IllegalStateException: Could not execute method of the activity
08-07 17:14:31.682: E/AndroidRuntime(2201): at android.view.View$1.onClick(View.java:3599)
08-07 17:14:31.682: E/AndroidRuntime(2201): at android.view.View.performClick(View.java:4204)
08-07 17:14:31.682: E/AndroidRuntime(2201): at android.view.View$PerformClick.run(View.java:17355)
08-07 17:14:31.682: E/AndroidRuntime(2201): at android.os.Handler.handleCallback(Handler.java:725)
08-07 17:14:31.682: E/AndroidRuntime(2201): at android.os.Handler.dispatchMessage(Handler.java:92)
08-07 17:14:31.682: E/AndroidRuntime(2201): at android.os.Looper.loop(Looper.java:137)
08-07 17:14:31.682: E/AndroidRuntime(2201): at android.app.ActivityThread.main(ActivityThread.java:5041)
08-07 17:14:31.682: E/AndroidRuntime(2201): at java.lang.reflect.Method.invokeNative(Native Method)
08-07 17:14:31.682: E/AndroidRuntime(2201): at java.lang.reflect.Method.invoke(Method.java:511)
08-07 17:14:31.682: E/AndroidRuntime(2201): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
08-07 17:14:31.682: E/AndroidRuntime(2201): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
08-07 17:14:31.682: E/AndroidRuntime(2201): at dalvik.system.NativeStart.main(Native Method)
08-07 17:14:31.682: E/AndroidRuntime(2201): Caused by: java.lang.reflect.InvocationTargetException
08-07 17:14:31.682: E/AndroidRuntime(2201): at java.lang.reflect.Method.invokeNative(Native Method)
08-07 17:14:31.682: E/AndroidRuntime(2201): at java.lang.reflect.Method.invoke(Method.java:511)
08 -07 17:14:31.682: E/AndroidRuntime(2201): at android.view.View$1.onClick(View.java:3594)
08-07 17:14:31.682: E/AndroidRuntime(2201): ... 11 more
08-07 17:14:31.682: E/AndroidRuntime(2201): Caused by: java.lang.NullPointerException
08-07 17:14:31.682: E/AndroidRuntime(2201): at com.L00081183.mygpstool.MyGPSToolDirections.getDirection(MyGPSToolDirections.java:54)
08-07 17:14:31.682: E/AndroidRuntime(2201): at com.L00081183.mygpstool.MainActivity.onClick_GetDirections(MainActivity.java:145)
This is how I used to fetch Driving direction from Google Direction API. This uses JSON. May be this helps you.
This class constructs the query
public class DirectionAPI extends AbstractQuery {
private static final String URL = "http://maps.googleapis.com/maps/api/directions/";
private static final boolean USE_SENSOR = true;
/**
* Required parameters
*/
/**
* Specifies the start location from which to calculate directions
*/
private static final String PARAM_ORIGIN = "origin";
/**
* Specifies the end location to which to calculate directions.
*/
private static final String PARAM_DESTINATION = "destination";
/**
* Indicates whether or not the directions request comes from a device with a location sensor.
* This value must be either true or false
*/
private static final String PARAM_SENSOR = "sensor";
/**
* Optional parameters
*/
/**
* Specifies what mode of transport to use when calculating directions
*/
private static final String PARAM_TRAVEL_MODE = "mode";
/**
* Travel Modes
*/
public static final String TRAVEL_MODE_DRIVING = "driving";
public static final String TRAVEL_MODE_BICYCLING = "bicycling";
public static final String TRAVEL_MODE_TRANSIT = "transit";
public static final String TRAVEL_MODE_WALKING = "walking";
/**
* Specifies what unit system to use when displaying results
*/
private static final String PARAM_UNIT_SYSTEM = "units";
/**
* Unit systems
*/
public static final String UNIT_SYSTEM_METRIC = "metric";
public static final String UNIT_SYSTEM_IMPERIAL = "imperial";
/**
* Waypoints alter a route by routing it through the specified location(s)
*/
private static final String PARAM_WAYPOINTS = "waypoints";
/**
* Specifies that the route using the supplied waypoints may be optimized to provide the
* shortest possible route. If true, the Directions service will return the reordered
* waypoints in an waypoint_order field
*/
private static final String PARAM_OPTIMIZE_WAYPOINTS = "optimize";
/**
* When set to true specifies that the Directions service may provide more than one route
* alternative in the response. Note that providing route alternatives may increase the
* response time from the server
*/
private static final String PARAM_PROVIDE_ROUTE_ALTERNATIVES = "alternatives";
/**
* Indicates that the calculated route(s) should avoid the indicated features.
* Currently, this parameter supports the following two arguments:
* tolls indicates that the calculated route should avoid toll roads/bridges.
* highways indicates that the calculated route should avoid highways.
*/
private static final String PARAM_AVOID = "avoid";
private static final String AVOID_HIGHWAYS = "highways";
private static final String AVOID_TOLLS = "tolls";
/**
* Specifies the region code, specified as a ccTLD ("top-level domain") two-character value
* https://developers.google.com/maps/documentation/javascript/directions#DirectionsRegionBiasing
*/
private static final String PARAM_REGION = "region";
private LatLng origin;
private LatLng destination;
private Map<LatLng, Boolean> waypoints;
private Map<String, String> optionalParams;
public DirectionAPI(LatLng origin, LatLng destination) {
if (origin == null || destination == null){
throw new NullPointerException("Origin and destination should not be null");
}
this.origin = origin;
this.destination = destination;
optionalParams = new HashMap<String, String>();
waypoints = new HashMap<LatLng, Boolean>();
}
public void addWaypoint(LatLng waypoint, boolean stopOver){
waypoints.put(waypoint, stopOver);
}
public void removeWaypoint(LatLng waypoint){
waypoints.remove(waypoint);
}
public void setTravelMode(String mode){
optionalParams.put(PARAM_TRAVEL_MODE, mode);
}
public void setUnitSystem(String unitSystem){
put(PARAM_UNIT_SYSTEM, unitSystem);
}
public void optimizeWaypoints(boolean optimize){
put(PARAM_OPTIMIZE_WAYPOINTS, String.valueOf(optimize));
}
public void provideRouteAlternatives(boolean provide){
put(PARAM_PROVIDE_ROUTE_ALTERNATIVES, String.valueOf(provide));
}
public void avoid(boolean highways, boolean tolls){
if (!highways && !tolls){
put(PARAM_AVOID, null);
} else if (highways && !tolls){
put(PARAM_AVOID, AVOID_HIGHWAYS);
} else if (!highways){
put(PARAM_AVOID, AVOID_TOLLS);
} else {
put(PARAM_AVOID, AVOID_TOLLS + "|" + AVOID_HIGHWAYS);
}
}
public void setRegion(String region){
put(PARAM_REGION, region);
}
private void put(String key, String value){
if (value == null){
optionalParams.remove(key);
} else {
optionalParams.put(key, value);
}
}
public String constructQuery() {
StringBuilder query = new StringBuilder(URL);
query.append("json").append("?");
query.append(PARAM_ORIGIN).append("=").append(origin.latitude).append(",").append(origin.longitude);
query.append("&");
query.append(PARAM_DESTINATION).append("=").append(destination.latitude).append(",").append(destination.longitude);
query.append("&");
query.append(PARAM_SENSOR).append("=").append(USE_SENSOR);
for (Map.Entry<String, String> entry : optionalParams.entrySet()){
query.append("&");
query.append(entry.getKey()).append("=").append(entry.getValue());
}
if (waypoints.size() > 0){
query.append("&");
query.append(PARAM_WAYPOINTS);
for (Map.Entry<LatLng, Boolean> entry : waypoints.entrySet()){
if (!entry.getValue()){
query.append("via:");
}
LatLng value = entry.getKey();
query.append(value.latitude).append(",").append(value.longitude);
query.append("|");
}
query.deleteCharAt(query.length()-1);
}
return query.toString();
}
public GoogleResponse execute(){
GoogleResponse googleResponse;
try {
java.net.URL url = new URL(constructQuery());
HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
if (httpURLConnection.getResponseCode() == 200){
JSONObject jsonObject = JSONParser.parse(httpURLConnection.getInputStream());
googleResponse = new GoogleResponse(jsonObject.getString("status"));
if (googleResponse.isOk()){
googleResponse.setJsonObject(jsonObject);
}
} else {
googleResponse = new GoogleResponse(httpURLConnection.getResponseCode());
}
} catch (MalformedURLException e) {
Log.e(Debug.TAG, e.getMessage(), e);
googleResponse = new GoogleResponse(e);
} catch (IOException e) {
Log.e(Debug.TAG, e.getMessage(), e);
googleResponse = new GoogleResponse(e);
} catch (JSONException e) {
Log.e(Debug.TAG, e.getMessage(), e);
googleResponse = new GoogleResponse(e);
}
return googleResponse;
}
}
This class creates JSON object
public class JSONParser {
public static JSONObject parse(InputStream inputStream){
BufferedReader reader;
try {
reader = new BufferedReader(new InputStreamReader(
inputStream, "iso-8859-1"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line).append("\n");
}
inputStream.close();
return new JSONObject(sb.toString());
} catch (UnsupportedEncodingException e) {
Log.e(Debug.TAG, e.getMessage(), e);
} catch (IOException e) {
Log.e(Debug.TAG, e.getMessage(), e);
} catch (JSONException e) {
Log.e(Debug.TAG, e.getMessage(), e);
}
return null;
}
}
This class reads the response status and check whether it is ok.
public class GoogleResponse {
private static final String RESPONSE_OK = "OK";
private static final String RESPONSE_ZERO_RESULT = "ZERO_RESULTS";
private static final String RESPONSE_OVER_QUERY_LIMIT = "OVER_QUERY_LIMIT";
private static final String RESPONSE_REQUEST_DENIED = "REQUEST_DENIED";
private static final String RESPONSE_INVALID_REQUEST = "INVALID_REQUEST";
public static final int OK = 0;
public static final int ERROR_ZERO_RESULT = 10;
public static final int ERROR_OVER_QUERY_LIMIT = 11;
public static final int ERROR_REQUEST_DENIED = 12;
public static final int ERROR_INVALID_REQUEST = 13;
public static final int ERROR_UNKNOWN_RESPONSE = 14;
public static final int ERROR_MALFORMED_URL = 15;
public static final int ERROR_IO = 16;
public static final int ERROR_JSON = 17;
private int response = -1;
private JSONObject jsonObject;
public GoogleResponse(String response) {
if (response.contentEquals(RESPONSE_OK)){
this.response = OK;
} else if (response.contentEquals(RESPONSE_ZERO_RESULT)){
this.response = ERROR_ZERO_RESULT;
} else if (response.contentEquals(RESPONSE_OVER_QUERY_LIMIT)){
this.response = ERROR_OVER_QUERY_LIMIT;
} else if (response.contentEquals(RESPONSE_REQUEST_DENIED)){
this.response = ERROR_REQUEST_DENIED;
} else if (response.contentEquals(RESPONSE_INVALID_REQUEST)){
this.response = ERROR_INVALID_REQUEST;
} else {
Log.e(Debug.TAG, "Unknown response : " + response);
this.response = ERROR_UNKNOWN_RESPONSE;
}
}
public GoogleResponse(Exception e) {
if (e instanceof MalformedURLException){
response = ERROR_MALFORMED_URL;
} else if (e instanceof IOException){
response = ERROR_IO;
} else if (e instanceof JSONException){
response = ERROR_JSON;
}
}
public GoogleResponse(int response) {
this.response = response;
}
public int getResponse() {
return response;
}
public boolean isOk(){
return response < 10;
}
public JSONObject getJsonObject() {
return jsonObject;
}
public void setJsonObject(JSONObject jsonObject) {
this.jsonObject = jsonObject;
}
}
This class parses the JSON data.
public class DrivingDirection {
private static final String TAG_ROUTES = "routes";
private static final String TAG_WAYPOINT_ORDER = "waypoint_order";
private static final String TAG_SUMMARY = "summary";
private static final String TAG_BOUNDS = "bounds";
private static final String TAG_LEGS = "legs";
private static final String TAG_WARNINGS = "warnings";
private static final String TAG_OVERVIEW_POLYLINE = "overview_polyline";
private static final String TAG_COPYRIGHTS = "copyrights";
private static final String TAG_BOUND_NORTH_EAST = "northeast";
private static final String TAG_BOUND_SOUTH_WEST = "southwest";
private String summary;
private String copyrights;
private ArrayList<LatLng> overviewPolyline;
private ArrayList<LatLng> smoothPolyline;
private JSONArray warnings;
private JSONArray waypointOrder;
private LatLng boundNorthEast;
private LatLng boundSouthWest;
private ArrayList<DirectionLeg> directionLegs;
private String json;
public DrivingDirection(JSONObject response) {
try {
JSONArray jsonArray = response.getJSONArray(TAG_ROUTES);
JSONObject jsonObject = jsonArray.getJSONObject(0);
summary = jsonObject.getString(TAG_SUMMARY);
copyrights = jsonObject.getString(TAG_COPYRIGHTS);
overviewPolyline = decodePolyline(jsonObject.getJSONObject(TAG_OVERVIEW_POLYLINE).getString("points"));
warnings = jsonObject.getJSONArray(TAG_WARNINGS);
waypointOrder = jsonObject.getJSONArray(TAG_WAYPOINT_ORDER);
JSONObject bounds = jsonObject.getJSONObject(TAG_BOUNDS);
boundNorthEast = interpretLatLng(bounds.getJSONObject(TAG_BOUND_NORTH_EAST));
boundSouthWest = interpretLatLng(bounds.getJSONObject(TAG_BOUND_SOUTH_WEST));
JSONArray legs = jsonObject.getJSONArray(TAG_LEGS);
directionLegs = new ArrayList<DirectionLeg>(legs.length());
for (int i=0; i<legs.length(); i++){
directionLegs.add(new DirectionLeg(legs.getJSONObject(i)));
}
json = response.toString();
} catch (JSONException e) {
Log.e(Debug.TAG, e.getMessage(), e);
}
}
public String getSummary() {
return summary;
}
public String getCopyrights() {
return copyrights;
}
public ArrayList<LatLng> getOverviewPolyline() {
return overviewPolyline;
}
public ArrayList<LatLng> getTotalPolyline(){
if (smoothPolyline == null) {
smoothPolyline = new ArrayList<LatLng>();
for (DirectionLeg directionLeg : directionLegs){
for (DirectionStep directionStep : directionLeg.getDirectionSteps()){
smoothPolyline.addAll(directionStep.getPolyline());
}
}
}
return smoothPolyline;
}
public JSONArray getWarnings() {
return warnings;
}
public JSONArray getWaypointOrder() {
return waypointOrder;
}
public LatLng getBoundNorthEast() {
return boundNorthEast;
}
public LatLng getBoundSouthWest() {
return boundSouthWest;
}
public ArrayList<DirectionLeg> getDirectionLegs() {
return directionLegs;
}
public long getDistance(){
long distance = 0L;
for (DirectionLeg directionLeg : directionLegs){
distance += directionLeg.getDistance();
}
return distance;
}
public long getDuration(){
long duration = 0L;
for (DirectionLeg directionLeg : directionLegs){
duration += directionLeg.getDuration();
}
return duration;
}
public void addPolyline(Polyline polyline){
polyline.setPoints(this.overviewPolyline);
}
public static ArrayList<LatLng> decodePolyline(String encoded) {
ArrayList<LatLng> poly = new ArrayList<LatLng>();
int index = 0, len = encoded.length();
int lat = 0, lng = 0;
while (index < len) {
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;
LatLng p = new LatLng((((double) lat / 1E5)),
(((double) lng / 1E5)));
poly.add(p);
}
return poly;
}
private static LatLng interpretLatLng(JSONObject object){
try {
return new LatLng(object.getDouble("lat"), object.getDouble("lng"));
} catch (JSONException e) {
Log.e(Debug.TAG, e.getMessage(), e);
}
return null;
}
public String getJson() {
return json;
}
public static class DirectionLeg {
private long duration;
private long distance;
private LatLng startLocation;
private LatLng endLocation;
private String startAddress;
private String endAddress;
private JSONArray viaWaypoints;
private ArrayList<DirectionStep> directionSteps;
public DirectionLeg(JSONObject jsonObject) {
try {
distance = jsonObject.getJSONObject("distance").getLong("value");
duration = jsonObject.getJSONObject("duration").getLong("value");
startAddress = jsonObject.getString("start_address");
endAddress = jsonObject.getString("end_address");
viaWaypoints = jsonObject.getJSONArray("via_waypoint");
startLocation = DrivingDirection.interpretLatLng(jsonObject.getJSONObject("start_location"));
endLocation = DrivingDirection.interpretLatLng(jsonObject.getJSONObject("end_location"));
JSONArray steps = jsonObject.getJSONArray("steps");
directionSteps = new ArrayList<DirectionStep>(steps.length());
for (int i=0; i<steps.length(); i++){
directionSteps.add(new DirectionStep(steps.getJSONObject(i)));
}
} catch (JSONException e) {
Log.e(Debug.TAG, e.getMessage(), e);
}
}
public long getDuration() {
return duration;
}
public long getDistance() {
return distance;
}
public LatLng getStartLocation() {
return startLocation;
}
public LatLng getEndLocation() {
return endLocation;
}
public String getStartAddress() {
return startAddress;
}
public String getEndAddress() {
return endAddress;
}
public JSONArray getViaWaypoints() {
return viaWaypoints;
}
public ArrayList<DirectionStep> getDirectionSteps() {
return directionSteps;
}
}
public static class DirectionStep{
private String htmlInstruction;
private long duration;
private long distance;
private LatLng startLocation;
private LatLng endLocation;
private String travelMode;
private String polyline;
private ArrayList<LatLng> polylinePoints;
public DirectionStep(JSONObject jsonObject){
try {
htmlInstruction = jsonObject.getString("html_instructions");
duration = jsonObject.getJSONObject("duration").getLong("value");
distance = jsonObject.getJSONObject("distance").getLong("value");
JSONObject startLocation = jsonObject.getJSONObject("start_location");
this.startLocation = new LatLng(startLocation.getDouble("lat"), startLocation.getDouble("lng"));
JSONObject endLocation = jsonObject.getJSONObject("end_location");
this.endLocation = new LatLng(endLocation.getDouble("lat"), endLocation.getDouble("lng"));
travelMode = jsonObject.getString("travel_mode");
polyline = jsonObject.getJSONObject("polyline").getString("points");
} catch (JSONException e) {
Log.e(Debug.TAG, e.getMessage(), e);
}
}
public String getHtmlInstruction() {
return htmlInstruction;
}
public long getDuration() {
return duration;
}
public long getDistance() {
return distance;
}
public LatLng getStartLocation() {
return startLocation;
}
public LatLng getEndLocation() {
return endLocation;
}
public String getTravelMode() {
return travelMode;
}
public ArrayList<LatLng> getPolyline() {
if (polylinePoints == null){
polylinePoints = decodePolyline(polyline);
}
return polylinePoints;
}
}
}
Typical usage. If you want you can set optional parameters like waypoints etc.
LatLng start = new LatLng(1.11, 1.11);
LatLng end = new LatLng(2.22, 2.22);
DirectionAPI directionAPI = new DirectionAPI(start, end);
GoogleResponse googleResponse = directionAPI.execute();
if (googleResponse.isOk()){
DrivingDirection drivingDirection = new DrivingDirection(googleResponse.getJsonObject());
ArrayList<LatLng> polyline = drivingDirection.getTotalPolyline();
}
这篇关于在使用NullPointerException异常GMapsV2Direction的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!