Trouble Retrieving Google Directions Using KML Data
Google has discontinued retrieving Google Directions using KML data since July 27, 2012. This means the code used to extract directions from Google by parsing the KML file is no longer functional.
Solution:
Migrate your code to use JSON instead of KML. To facilitate this transition, I created the following classes:
Implementation:
private Route directions(final GeoPoint start, final GeoPoint dest) { Parser parser; String jsonURL = "https://developers.google.com/maps/documentation/directions/#JSON"; final 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; }
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();
Note: It's recommended to use the directions() function within an AsyncTask to avoid network operations on the UI thread.
The above is the detailed content of How to Migrate from KML to JSON for Retrieving Google Directions Data?. For more information, please follow other related articles on the PHP Chinese website!