android – Map查看绘制方向使用Google Directions API – 解码折线

前端之家收集整理的这篇文章主要介绍了android – Map查看绘制方向使用Google Directions API – 解码折线前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我试图使用Google directions API来显示我的地图视图中的路线,但是我无法从 JSON响应中获取数据.我可以得到“水平”和“点”字符串,但无法解决如何将它们解码到地图上的点.

任何帮助将不胜感激.

解决方法

我有一个类可以为你解码,添加下面的类,然后调用你的代码,如下所示:
  1. int[] decodedZoomLevels = PolylineDecoder.decodeZoomLevels(levels);
  2. GeoPoint[] gPts = PolylineDecoder.decodePoints(points,decodedZoomLevels.length);

点数和级别是您从JSON响应中提取的数据.然后,您可以浏览一系列地理点,在它们之间绘制一条线,以显示您的路线.

希望这可以帮助!肯尼

编辑:似乎google directions API不再返回缩放级别字符串作为JSON响应的一部分,不用担心,但是我们所使用的都是检查点数,所以我们可以简单地将它们放入列表如下:

  1. public static List <GeoPoint> decodePoints(String encoded_points){
  2. int index = 0;
  3. int lat = 0;
  4. int lng = 0;
  5. List <GeoPoint> out = new ArrayList<GeoPoint>();
  6.  
  7. try {
  8. int shift;
  9. int result;
  10. while (index < encoded_points.length()) {
  11. shift = 0;
  12. result = 0;
  13. while (true) {
  14. int b = encoded_points.charAt(index++) - '?';
  15. result |= ((b & 31) << shift);
  16. shift += 5;
  17. if (b < 32)
  18. break;
  19. }
  20. lat += ((result & 1) != 0 ? ~(result >> 1) : result >> 1);
  21.  
  22. shift = 0;
  23. result = 0;
  24. while (true) {
  25. int b = encoded_points.charAt(index++) - '?';
  26. result |= ((b & 31) << shift);
  27. shift += 5;
  28. if (b < 32)
  29. break;
  30. }
  31. lng += ((result & 1) != 0 ? ~(result >> 1) : result >> 1);
  32. /* Add the new Lat/Lng to the Array. */
  33. out.add(new GeoPoint((lat*10),(lng*10)));
  34. }
  35. return out;
  36. }catch(Exception e) {
  37. e.printStackTrace();
  38. }
  39. return out;
  40. }

编辑:旧版

  1. public class PolylineDecoder {
  2. /**
  3. * Transform a encoded PolyLine to a Array of GeoPoints.
  4. * Java implementation of the original Google JS code.
  5. * @see Original encoding part: <a href="http://code.google.com/apis/maps/documentation/polylinealgorithm.html">http://code.google.com/apis/maps/documentation/polylinealgorithm.html</a>
  6. * @return Array of all GeoPoints decoded from the PolyLine-String.
  7. * @param encoded_points String containing the encoded PolyLine.
  8. * @param countExpected Number of points that are encoded in the PolyLine. Easiest way is to use the length of the ZoomLevels-String.
  9. * @throws DecodingException
  10. */
  11. public static GeoPoint[] decodePoints(String encoded_points,int countExpected){
  12. int index = 0;
  13. int lat = 0;
  14. int lng = 0;
  15. int cnt = 0;
  16. GeoPoint[] out = new GeoPoint[countExpected];
  17.  
  18. try {
  19. int shift;
  20. int result;
  21. while (index < encoded_points.length()) {
  22. shift = 0;
  23. result = 0;
  24. while (true) {
  25. int b = encoded_points.charAt(index++) - '?';
  26. result |= ((b & 31) << shift);
  27. shift += 5;
  28. if (b < 32)
  29. break;
  30. }
  31. lat += ((result & 1) != 0 ? ~(result >> 1) : result >> 1);
  32.  
  33. shift = 0;
  34. result = 0;
  35. while (true) {
  36. int b = encoded_points.charAt(index++) - '?';
  37. result |= ((b & 31) << shift);
  38. shift += 5;
  39. if (b < 32)
  40. break;
  41. }
  42. lng += ((result & 1) != 0 ? ~(result >> 1) : result >> 1);
  43. /* Add the new Lat/Lng to the Array. */
  44. out[cnt++] = new GeoPoint((lat*10),(lng*10));
  45. }
  46. return out;
  47. }catch(Exception e) {
  48. e.printStackTrace();
  49. }
  50. return out;
  51. }
  52.  
  53. public static int[] decodeZoomLevels(String encodedZoomLevels){
  54. int[] out = new int[encodedZoomLevels.length()];
  55. int index = 0;
  56.  
  57. for(char c : encodedZoomLevels.tocharArray())
  58. out[index++] = c - '?';
  59. return out;
  60.  
  61. }
  62. }

猜你在找的Android相关文章