yet another blog about computer, technology, programming, and internet

Tuesday, December 21, 2010

Decoding Polylines from Google Maps Direction API

Tuesday, December 21, 2010 Posted by Ismail Habib , , 13 comments
The Google Maps Directions API provides a way to retrieve directions data from the back-end as opposed to the original JavaScript version when one should retrieve the data from browser. Google provides the routing results as an encoded polylines format. as explained in the Encoded Polyline Algorithm Format. The JavaScript version contains a library for both Encoding and Decoding, which is not the case for a back-end solution written in another language.

Since I'm more interested in the decoding part for Java, here is a code that can be used:

import java.util.ArrayList;

public class PolylineDecoder {
 public static ArrayList decodePoly(String encoded) {
  ArrayList poly = new ArrayList();
  int index = 0, len = encoded.length();
  int lat = 0, lng = 0;
  while (index < len) {
   int b, shift = 0, result = 0;
   do {
    b = encoded.charAt(index++) - 63;
    result |= (b & 0x1f) << shift;
    shift += 5;
   } while (b >= 0x20);
   int dlat = ((result & 1) != 0 ? ~(result >> 1) : (result >> 1));
   lat += dlat;
   shift = 0;
   result = 0;
   do {
    b = encoded.charAt(index++) - 63;
    result |= (b & 0x1f) << shift;
    shift += 5;
   } while (b >= 0x20);
   int dlng = ((result & 1) != 0 ? ~(result >> 1) : (result >> 1));
   lng += dlng;
   Location p = new Location((((double) lat / 1E5)),
     (((double) lng / 1E5)));
   poly.add(p);
  }
  return poly;
 }
}

"Location" is a self-defined, simple class that I use to store coordinate (Latitude and Longitude). Google Map's LatLng is not used because this is for backend.

public class Location implements IsSerializable {

 private double latitude;

 private double longitude;

 public Location() {

 }

 public Location(Double latitude, Double longitude) {
  this.latitude = latitude;
  this.longitude = longitude;
 }
 
 /**
  * @return the latitude
  */
 public double getLatitude() {
  return latitude;
 }

 /**
  * @return the longitude
  */
 public double getLongitude() {
  return longitude;
 }
}

This PolylineEncoder class code is taken from JeffreySambels.com with several modifications.

13 comments:

  1. This is article nice information to share for us.. I really appreciate to share your knowledge . Thanks a lot

    ReplyDelete
  2. Thanks for your contribution. it helped me a lot. Can you help me to decode polyline:levels as well. Please help me. thanks again.

    ReplyDelete
  3. Anonymous6:13 PM

    if u use too much polylines while retrieving a map it gives error

    ReplyDelete
  4. hi friend, very very very thanks by your code, i'm Brazilian, sorry my english, but you help me with it.
    thanks again

    ReplyDelete
  5. That's really good job. This is not easy. You done a such a difficult job. Thanks for sharing this.

    ReplyDelete
  6. Great post! I'm going to write a review about Google Maps and I was looking for information about everything that have to do with this useful tool. Keep sharing please.

    ReplyDelete
  7. Thanks for the post, it was very helpful

    ReplyDelete
  8. implements IsSerializable was error

    ReplyDelete
  9. It's really good work.if u use too much polylines while retrieving a map it gives error.is there are any solution to this?

    ReplyDelete
  10. We are a leading software development services in delhi,which works as per the client requirements and give provide software.

    ReplyDelete