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

115 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. We are a leading software development services in delhi,which works as per the client requirements and give provide software.

    ReplyDelete
  11. 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
  12. great sharing, thank alot for your post
    Jio

    ReplyDelete
  13. Packers-and-movers-bangalore.in provides all the type of #moving solutions, no matter where you want to shift into the city or shift another city or another country ..
    Packers And Movers Bangalore Charges

    ReplyDelete
  14. Nice article .

    Packers And Movers Bangalore to Chennai

    ReplyDelete
  15. 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
  16. Nice post http://www.selfcarinsurance.com

    ReplyDelete
  17. There are some highly useful products of Cisco that include Router, Networking software, Servers-Unified computing, wireless products among diversity of products. Cisco Support

    ReplyDelete
  18. Amazing post.Thanks for sharing among us.

    ReplyDelete
  19. The above shared article is really very good, it helped me a lot. Thanks for sharing.

    Dell Customer Care

    ReplyDelete
  20. Dell Customer care Number provide the support to their user if any one is having some problem then he or she can share your problems here.
    Dell Customer Care

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

    ReplyDelete
  22. Amazing post.Thanks for sharing among us. Norton customer support

    ReplyDelete
  23. Technical support may be delivered by the different technologies depending on the situation. For example, direct questions can be addressed using telephone calls, SMS, Online chat, Support Forums, E-mail or Fax.
    Windows tech support

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

    ReplyDelete
  25. If you really facing an issues with outlook mail, visit outlook customer support for more information.
    outlook Customer Support Number

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

    ReplyDelete
  27. If you are facing any problem regarding the Gmail, just visit the given link-

    Gmail Customer Support

    ReplyDelete
  28. Hi, Great.. Tutorial is just awesome..It is really helpful for a newbie like me.. I am a regular follower of your blog. Really very informative post you shared here. Kindly keep blogging. If anyone wants to become a .Net developer learn from Dot Net Training in Chennai. or learn thru ASP.NET Essential Training Online . Nowadays Dot Net has tons of job opportunities on various vertical industry.
    or Javascript Training in Chennai. Nowadays JavaScript has tons of job opportunities on various vertical industry.

    ReplyDelete
  29. Hi, Great.. Tutorial is just awesome..It is really helpful for a newbie like me.. I am a regular follower of your blog. Really very informative post you shared here. Kindly keep blogging. If anyone wants to become a Java developer learn from Java Training in Chennai. or learn thru Java Online Training in India . Nowadays Java has tons of job opportunities on various vertical industry.


    or Javascript Training in Chennai. Nowadays JavaScript has tons of job opportunities on various vertical industry.

    ReplyDelete
  30. if you have any problem related to the router then you can contact us we are here to provide you the complete solution to your problem.
    d link router customer support

    ReplyDelete
  31. 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
  32. Thank you for taking the time to publish this information very useful! TweakBox

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

    ReplyDelete
  34. 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
  35. Much obliged to you for exceptionally usefull data.. AirShou

    ReplyDelete
  36. very Help Full Post. Look Some Different here at UPSC Exam

    ReplyDelete
  37. It's a fact that your blog posts are so unique and interesting and I enjoys a lot while reading your posts because you explained your post very deeply in a very easy and clear language. Thanks for your support and Happy Blogging :D

    cbse board exam result 2018
    cbse 10th exam result 2018
    cbse 12th exam result 2018
    ap ssc exam results 2018
    ap inter exam results 2018
    seba hslc exam result 2018

    ReplyDelete
  38. 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
  39. I read this article. I think You put a lot of effort to create this article. I appreciate your work.
    thesis Writing Service

    ReplyDelete
  40. 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
  41. 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
  42. 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
  43. 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
  44. Guides for online gambling can be found only on BGAOC! You can find casino reviews, games and much more! Visit us!

    ReplyDelete
  45. Знаю где продается отличная rgb лента, покупал у них недавно, нареканий нет, компания называется Ekodio

    ReplyDelete
  46. 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
  47. 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
  48. This is helpful to improve my conversion rate through SEO technique in OpenCart Development.
    Hire Opencart Developer
    Mobile App Development India

    ReplyDelete
  49. 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
  50. 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
  51. Perfect Helpful Post On This Blog , Please Keep it up To Daily Latest Post ! Its Our Need ! You Can Also Download Printer Maintainance Reset Utility Adjustment Program For Free . NO Need Any Cost . Thank You So Much.

    ReplyDelete
  52. 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
  53. I am not aware of the common mistakes that drive online shoppers away from the E-commerce website. The checklist you have shared in the site is very much informative and helps to avoid the mistakes in E-commerce development.
    Hire Php Programmer
    Hire Wordpress Programmer
    Hire Web Developer
    Hire Magento Developer
    Hire Wordpress Developer

    ReplyDelete
  54. I hope this post of yours will be more appreciated by it really excellent, i enjoyed it, thanks for posting it. I do not feel sorry for taking the time to read this post, it is really nice and useful to me, thanks for posting it.
    Jogos live, cá koi mini, friv4school girl games, friv Games 2019

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

    everyhourloans

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

    mobile repairing course karol bagh

    ReplyDelete