Mapbar POI 转 经纬度坐标的各个版本

参考 这个的php和java版本:https://code.google.com/p/freebsd-help/source/browse/Mapbar%E7%BB%8F%E7%BA%AC%E5%9D%90%E6%A0%87%E5%81%8F%E7%A7%BB%E7%9A%84%E5%8A%A0%E8%A7%A3%E5%AF%86%E7%AE%97%E6%B3%95

出来C# 版,后面需要服务端的JAVA版本再贴上来,先上C#版

PHP版本

View Code
 1 function pos_decode($pos) {
 2     $index = -1;
 3     $count = 0;
 4     $code = "";
 5     $len = strlen($pos);
 6     $apiKey = ord($pos[$len - 1]);
 7     $pos = substr($pos, 0, $len - 1);
 8     $len--;
 9     for ($i = 0; $i < $len; $i++) {
10         $hash = intval($pos[$i], 36) - 10;
11         if ($hash >= 10) {
12             $hash = $hash - 7;
13         }
14         $code .= base_convert($hash, 10, 36);
15         if ($hash > $count) {
16             $index = $i;
17             $count = $hash;
18         }
19     }
20     $subLL = intval(substr($code, 0, $index), 16);
21     $addLL = intval(substr($code, $index + 1), 16);
22     $lng = ($subLL + $addLL - intval($apiKey)) / 2;
23     $lat = ($addLL - $lng) / 100000.0;
24     $lng /= 100000.0;
25 
26     return array($lat, $lng);
27 }

JavaScript版本

View Code
 1 function (poi) {
 2         if (DP.isObject(poi)) {
 3             return poi;
 4         };
 5         var _options = this.options,
 6             index = -1,
 7             count = 0,
 8             code = "",
 9             len = poi.length,
10             apiKey = poi.charCodeAt(len - 1),
11             hash,
12             subLL, addLL,
13             lng, lat;
14         poi = poi.substring(0, len - 1);
15         len--;
16         for (var i = 0; i < len; i++) {
17             hash = parseInt(poi.charAt(i), _options.settings.cha) - _options.settings.add;
18             if (hash >= _options.settings.add) {
19                 hash = hash - _options.settings.plus;
20             };
21             code += (hash).toString(_options.settings.cha);
22             if (hash > count) {
23                 index = i;
24                 count = hash;
25             };
26         }
27         subLL = parseInt(code.substring(0, index), _options.settings.digi);
28         addLL = parseInt(code.substring(index + 1), _options.settings.digi);
29         lng = (subLL + addLL - parseInt(apiKey)) / 2;
30         lat = (addLL - lng) / 100000.0;
31         lng /= 100000.0;
32         return { "lat": lat, "lng": lng };
33     }

增加一个C#版本

        static int ConvertToInt32(char c)
        {
            if (c >= 48 && c <= 57) { return c - 48; }
            if (c >= 97 && c <= 122) { return c - 87; }
            if (c >= 65 && c <= 90) { return c - 55; }
            return 0;
        }

        static string ConvertToString(int v, int fromBase)
        {
            if (fromBase < 2 || fromBase > 36) throw new ArgumentException();
            List<char> cs = new List<char>(36);
            while (v > 0)
            {
                int x = v % fromBase;
                int c = 48;
                if (x >= 10) { c = 87; }
                cs.Add((char)(x + c));
                v /= fromBase;
            }
            cs.Reverse();
            string s = new string(cs.ToArray());
            return s;
        }
        private Location Decode(string poi)
        {
            int index = -1;
            int count = 0;
            string code = "";
            int len = poi.Length;
            char apiKey = poi[len - 1];
            int hash = 0;
            int subLL = 0;
            int addLL = 0;
            double lng = 0;
            double lat = 0;
            poi = poi.Substring(0, len - 1);
            len--;
            for (int i = 0; i < len; i++)
            {
                hash = ConvertToInt32(poi[i]) - 10;
                if (hash >= 10)
                {
                    hash = hash - 7;
                }
                code += ConvertToString(hash, 36);
                if (hash > count)
                {
                    index = i;
                    count = hash;
                }
            }
            subLL = Convert.ToInt32(code.Substring(0, index), 16);
            addLL = Convert.ToInt32(code.Substring(index + 1), 16);
            lng = (subLL + addLL - Convert.ToInt32(apiKey + "", 16)) / 2.0d;
            lat = (addLL - lng) / 100000.0d;
            lng /= 100000.0;
            return new Location()
            {
                lat = lat,
                lng = lng
            };
        }

 

Java版本请听下回分解

posted @ 2013-01-03 22:39  shareach  阅读(684)  评论(0编辑  收藏  举报