@Override public Road getRoad(ArrayList<GeoPoint> waypoints) { String url = getUrl(waypoints); Log.d(BonusPackHelper.LOG_TAG, "OSRMRoadManager.getRoad:" + url); // String jString = BonusPackHelper.requestStringFromUrl(url); HttpConnection connection = new HttpConnection(); connection.setUserAgent(mUserAgent); connection.doGet(url); String jString = connection.getContentAsString(); connection.close(); if (jString == null) { Log.e(BonusPackHelper.LOG_TAG, "OSRMRoadManager::getRoad: request failed."); return new Road(waypoints); } Locale l = Locale.getDefault(); HashMap<String, String> directions = (HashMap<String, String>) DIRECTIONS.get(l.getLanguage()); if (directions == null) directions = (HashMap<String, String>) DIRECTIONS.get("en"); Road road = new Road(); try { JSONObject jObject = new JSONObject(jString); road.mStatus = jObject.getInt("status"); String route_geometry = jObject.getString("route_geometry"); road.mRouteHigh = PolylineEncoder.decode(route_geometry, 1, false); JSONArray jInstructions = jObject.getJSONArray("route_instructions"); int n = jInstructions.length(); RoadNode lastNode = null; for (int i = 0; i < n; i++) { JSONArray jInstruction = jInstructions.getJSONArray(i); RoadNode node = new RoadNode(); int positionIndex = jInstruction.getInt(3); node.mLocation = road.mRouteHigh.get(positionIndex); node.mLength = jInstruction.getInt(2) / 1000.0; node.mDuration = jInstruction.getInt(4); // Segment duration in seconds. String direction = jInstruction.getString(0); String roadName = jInstruction.getString(1); if (lastNode != null && "1".equals(direction) && "".equals(roadName)) { // node "Continue" with no road name is useless, don't add it lastNode.mLength += node.mLength; lastNode.mDuration += node.mDuration; } else { node.mManeuverType = getManeuverCode(direction); node.mInstructions = buildInstructions(direction, roadName, directions); // Log.d(BonusPackHelper.LOG_TAG, direction+"=>"+node.mManeuverType+"; // "+node.mInstructions); road.mNodes.add(node); lastNode = node; } } JSONObject jSummary = jObject.getJSONObject("route_summary"); road.mLength = jSummary.getInt("total_distance") / 1000.0; road.mDuration = jSummary.getInt("total_time"); } catch (JSONException e) { road.mStatus = Road.STATUS_TECHNICAL_ISSUE; e.printStackTrace(); } if (road.mStatus != Road.STATUS_OK) { // Create default road: int status = road.mStatus; road = new Road(waypoints); road.mStatus = status; } else { road.buildLegs(waypoints); road.mBoundingBox = BoundingBoxE6.fromGeoPoints(road.mRouteHigh); road.mStatus = Road.STATUS_OK; } Log.d(BonusPackHelper.LOG_TAG, "OSRMRoadManager.getRoad - finished"); return road; }
@Override public void endElement(String uri, String localName, String name) throws SAXException { if (localName.equals("lat")) { mLat = Double.parseDouble(mStringBuilder.toString()); } else if (localName.equals("lng")) { mLng = Double.parseDouble(mStringBuilder.toString()); } else if (localName.equals("shapePoints")) { mRoad.mRouteHigh = PolylineEncoder.decode(mStringBuilder.toString(), 10); // Log.d("DD", "High="+mRoad.mRouteHigh.size()); } else if (localName.equals("generalizedShape")) { mRoad.setRouteLow(PolylineEncoder.decode(mStringBuilder.toString(), 10)); // Log.d("DD", "Low="+mRoad.mRouteLow.size()); } else if (localName.equals("length")) { mLink.mLength = Double.parseDouble(mStringBuilder.toString()); } else if (localName.equals("speed")) { mLink.mSpeed = Double.parseDouble(mStringBuilder.toString()); } else if (localName.equals("shapeIndex")) { mLink.mShapeIndex = Integer.parseInt(mStringBuilder.toString()); } else if (localName.equals("link")) { // End of a link: update road attributes: // GuidanceLinkCollection could in theory contain additional unused links, // but normally not with fishbone set to false. mLink.mDuration = mLink.mLength / mLink.mSpeed * 3600.0; mLinks.add(mLink); mRoad.mLength += mLink.mLength; mRoad.mDuration += mLink.mDuration; mLink = null; } else if (localName.equals("turnCost")) { int turnCost = Integer.parseInt(mStringBuilder.toString()); mNode.mDuration += turnCost; mRoad.mDuration += turnCost; } else if (localName.equals("maneuverType")) { mNode.mManeuverType = Integer.parseInt(mStringBuilder.toString()); } else if (localName.equals("info")) { if (isGuidanceNodeCollection) { if (mNode.mInstructions == null) // this is first "info" value for this node, keep it: mNode.mInstructions = mStringBuilder.toString(); } } else if (localName.equals("linkId")) { if (isGuidanceNodeCollection) mNode.mNextRoadLink = Integer.parseInt(mStringBuilder.toString()); } else if (localName.equals("node")) { mRoad.mNodes.add(mNode); mNode = null; } else if (localName.equals("GuidanceNodeCollection")) { isGuidanceNodeCollection = false; } else if (localName.equals("ul")) { if (isBB) { mNorth = mLat; mWest = mLng; } } else if (localName.equals("lr")) { if (isBB) { mSouth = mLat; mEast = mLng; } } else if (localName.equals("boundingBox")) { mRoad.mBoundingBox = new BoundingBoxE6(mNorth, mEast, mSouth, mWest); isBB = false; } }