Home > Java > javaTutorial > body text

Why Did Google Directions API Stop Providing KML Data?

Susan Sarandon
Release: 2024-11-25 15:30:16
Original
300 people have browsed it

Why Did Google Directions API Stop Providing KML Data?

Why Google Directions API no longer provides KML data?

Google Directions API stopped providing KML data since July 27th, 2012. Therefore, developers need to migrate to JSON or XML formats instead.

Alternatives:

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.

Code Implementation:

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;
}
Copy after login

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();
Copy after login

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!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template