Google Directions API 中路线编码解析

 1 public List<Location> GetGeoPoints(string encoded)
 2         {
 3             List<Location> poly = new List<Location>();
 4             int index = 0, len = encoded.Length;
 5             int lat = 0, lng = 0;
 6             while (index < len)
 7             {
 8                 int b, shift = 0, result = 0;
 9                 do
10                 {
11                     b = encoded.ToCharArray()[index++] - 63;
12                     result |= (b & 0x1f) << shift;
13                     shift += 5;
14                 } while (b >= 0x20);
15                 int dlat = ((result & 1) != 0 ? ~(result >> 1) : (result >> 1));
16                 lat += dlat;
17 
18                 shift = 0;
19                 result = 0;
20                 do
21                 {
22                     b = encoded.ToCharArray()[index++] - 63;
23                     result |= (b & 0x1f) << shift;
24                     shift += 5;
25                 } while (b >= 0x20);
26                 int dlng = ((result & 1) != 0 ? ~(result >> 1) : (result >> 1));
27                 lng += dlng;
28 
29                 double latResult = ((double)lat / 1E5) * 1E6 * Math.Pow(10, -6);
30                 double lngResult = ((double)lng / 1E5) * 1E6 * Math.Pow(10, -6);
31                 Location p = new Location(latResult, lngResult);
32                 poly.Add(p);
33             }
34             return poly;
35         }
View Code
原文地址:https://www.cnblogs.com/tianciliangen/p/3497828.html