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 , , 63 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.

63 comments:

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

    ReplyDelete
    Replies
    1. Thanks for this article. I have always wished that google maps were little more open. They have no api and everytime you have manipulate via browser based javascript. Nice article anyways.

      Delete
  2. Anonymous11:51 AM

    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. Anonymous8:09 PM

    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. This blog is so nice to me. I will continue to come here again and again. Visit my link as well.
    Good luck obat aborsi

    ReplyDelete
  11. great sharing, thank alot for your post
    Jio

    ReplyDelete
  12. Nice article .

    Packers And Movers Bangalore to Chennai

    ReplyDelete
  13. Tubemate is an application that allows you to watch and download video from Youtube to your smartphone for free. This is an essential application designed for those who love to watch videos and listen to music in their free time.

    ReplyDelete
  14. Anonymous11:33 AM

    Amazing post.Thanks for sharing among us.

    ReplyDelete
  15. Amazing article and please keep posting like this...
     Lenovo Support Number

    ReplyDelete
  16. nice post I enjoyed it a lot keep sharing like this Hp printer Support

    ReplyDelete
  17. You have done a great job on this article. It’s very readable and highly intelligent. You have even managed to make it understandable and easy to read. You have some real writing talent. Thank you. Emus4U

    ReplyDelete
  18. Thank you for taking the time to publish this information very useful! TweakBox

    ReplyDelete
  19. Pleasant online journal and totally exceptional. You can improve however despite everything I say this perfect.Keep striving generally advantageous. App Valley

    ReplyDelete
  20. I truly welcome the sort of points you post here. A debt of gratitude is in order for sharing us an incredible data that is really useful. Great day! iOS 11.2.2 Jailbreak

    ReplyDelete
  21. Much obliged to you for exceptionally usefull data.. AirShou

    ReplyDelete
  22. Anonymous10:42 AM

    Apple users face this quite often when using Safari browser. It could be due to many reasons. To know for sure call us at our toll-free Safari customer support number for help. Our top-notch technical service team will resolve your problem in no time. chrome customer service

    ReplyDelete
  23. 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.
    Epson error code 0x69

    ReplyDelete
  24. Good information , this is very helpful for me ,we provide dell tablet support service , if you have any problem you can contact with toll free number +44 800 014 8212 dell tablet support.

    ReplyDelete
  25. Nice information. Desktop support is ready to help you. Free call us any time on Desktop customer support number +44 800 098 8380 for PC or Computer security. Desktop customer support phone number is available 24×7 at your service.

    ReplyDelete
  26. Anonymous6:50 AM

    Very interesting Post... But how we integrate this Api in Salesforce tool During the Searches i find this salesforce data integration tools but it is not working will you please suggest some example

    ReplyDelete
  27. Guides for online gambling can be found only on BGAOC! You can find casino reviews, games and much more! Visit us!

    ReplyDelete
  28. Hiring packers and movers in Gurgaon is not too hard if you take help of the best moving professionals. Best movers and packers Gurgaon assist with their skilled and trained team to pack, load, unload and unpack your belongings.
    So be aware and hire the best mover packer to save your valuables.
    packers and movers Gurgaon
    packers and movers Gurgaon Charges

    ReplyDelete
  29. Magento designing involves various development services like theme designing, layout updation and much more. Hire the team of professional magento designers to create a highly optimized ecommerce store. Here is the list of top magento development experts to optimize the website according to the business requirement.

    ReplyDelete
  30. This is helpful to improve my conversion rate through SEO technique in OpenCart Development.
    Hire Opencart Developer
    Mobile App Development India

    ReplyDelete
  31. Really an impressive post shared by you!

    I am a teacher in the PTE/IELTS coaching centre. We provide IELTS exam programs and classes with experts in Australia. We are successfully training our students in the test. We try to utilize the best teaching models as per student’s requirements to make him gain more from our experienced trainers.

    ReplyDelete
  32. The blog you have shared is incredibly helpful. The tips you have shared related to web development is really informative. Thanks for sharing this blog!

    Hire Magento developer India
    Hire Magento Developer
    Hire Wordpress Developer
    Hire Wordpress Programmer
    Hire wordpress developer India

    ReplyDelete
  33. Thanks ISMAIL HABIB for the code.

    We have been passionate about crafting iconic Eurasian circuits specially Caucasus (Armenia, Azerbaijan & Georgia), Central Asia (Kazakhstan, Kyrgyzstan, Tajikistan, Turkmenistan and Uzbekistan) and Eastern Europe (Russia, Serbia, and Ukraine) for more than 8 years.

    Adventure Tour Packages | Ukraine Tour Package | Trekking Tour Packages | Eastern Europe Tours| Silk Route Tour Packages| Russia Tour Package | Almaty Tour Package | Tashkent Group Packages

    ReplyDelete
  34. I am really so happy to take service from Everyhourloans.It's one of the best loan lenders in Ireland.

    everyhourloans

    ReplyDelete
  35. Amazing Post! very interesting and useful post.

    mobile repairing course karol bagh

    ReplyDelete
  36. This blog is really cool and great. I really appreciate your working on that blog. Before that i wasn't seen such kind information on any website. Thanks for sharing this blog with us.
    Vehicle tracking system

    ReplyDelete
  37. This is my first visit to this website.You have shared content which is unique and easy to understand.I have learned a lot which will help me in future.Talk to Astrologer

    ReplyDelete