Google Directions API stopped providing KML data since July 27th, 2012. Therefore, developers need to migrate to JSON or XML formats instead.
JSON:
To parse JSON data, you can create six classes as follows:
Parser.java: Defines an interface for parsing.
XMLParser.java: Provides a base class for XML parsing.
Segment.java: Represents a segment of the route.
Route.java: Represents the entire route.
GoogleParser.java: Parses Google JSON data.
RouteOverlay.java: Draws the route on a map overlay.
XML:
Alternatively, you can use XML for parsing, replacing GoogleParser.java with the following:
XMLParser.java: Parses Google XML data.
To use these classes, you can create the following code:
private Route directions(GeoPoint start, GeoPoint dest) { Parser parser; String jsonURL = "https://developers.google.com/maps/documentation/directions/#JSON"; // API URL StringBuffer sBuf = new StringBuffer(jsonURL); sBuf.append("?origin="); sBuf.append(start.getLatitudeE6()/1E6); sBuf.append(','); sBuf.append(start.getLongitudeE6()/1E6); sBuf.append("&destination="); sBuf.append(dest.getLatitudeE6()/1E6); sBuf.append(','); sBuf.append(dest.getLongitudeE6()/1E6); sBuf.append("&sensor=true&mode=driving"); parser = new GoogleParser(sBuf.toString()); Route r = parser.parse(); return r; }
Then, add the following code to your onCreate() function:
MapView mapView = (MapView) findViewById(R.id.mapview); Route route = directions(new GeoPoint((int)(26.2*1E6),(int)(50.6*1E6)), new GeoPoint((int)(26.3*1E6),(int)(50.7*1E6))); RouteOverlay routeOverlay = new RouteOverlay(route, Color.BLUE); mapView.getOverlays().add(routeOverlay); mapView.invalidate();
The above is the detailed content of Why Did Google Directions API Stop Providing KML Data?. For more information, please follow other related articles on the PHP Chinese website!