Home > Java > javaTutorial > How to Migrate from KML to JSON for Retrieving Google Directions Data?

How to Migrate from KML to JSON for Retrieving Google Directions Data?

Barbara Streisand
Release: 2024-11-25 11:36:45
Original
982 people have browsed it

How to Migrate from KML to JSON for Retrieving Google Directions Data?

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:

  • Parser.java: Defines an interface for parsing Google Directions data.
  • XMLParser.java: Implements an abstract class for parsing XML data from a provided URL.
  • Segment.java: Represents a segment of a route, including points, turn instructions, length, and distance.
  • Route.java: Represents a route, including points, turn instructions, length, polyline, and other metadata.
  • GoogleParser.java: Implements a concrete parser for Google JSON data.
  • RouteOverlay.java: Draws the route on a map.

Implementation:

  1. Replace your code for extracting directions from KML with the following function:
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;
}
Copy after login
  1. In the onCreate() method, add the following code:
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

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!

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