# geeky.blogger

## Decoding Polylines from Google Maps Direction API

Tuesday, December 21, 2010 Posted by Ismail Habib
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

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.

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.

3. Anonymous6:13 PM

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

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

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

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.

7. Thanks for the post, it was very helpful

8. implements IsSerializable was error

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?

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

1. Nice information That's Good, thanks for sharing. Microsoft Customer Service

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

12. great sharing, thank alot for your post
Jio

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

14. Nice article .

Packers And Movers Bangalore to Chennai

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.

16. Nice post http://www.selfcarinsurance.com

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

18. Nice Post.
AOL Support

19. Amazing post.Thanks for sharing among us.

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

Dell Customer Care

21. 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

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

23. Great Post.
Epson Support

24. Amazing post.Thanks for sharing among us. Norton customer support

25. 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

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

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

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

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

Gmail Customer Support

30. amazing post Tinder Openers

31. 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.

32. 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.

33. 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

34. 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

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

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

37. 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

38. Much obliged to you for exceptionally usefull data.. AirShou

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

40. 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

41. 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

42. I read this article. I think You put a lot of effort to create this article. I appreciate your work.
thesis Writing Service

43. 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

44. 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.

45. 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.

46. 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

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

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

49. 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

50. 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.

51. Interesting Post. Thanx for sharing.

lenderslists

review for loans

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

53. 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.

54. 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

55. 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.

56. 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

57. 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

58. 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

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

everyhourloans

60. Amazing Post! very interesting and useful post.

mobile repairing course karol bagh