首页 > 其他 > 详细

坐标转化为最简单的字符串

时间:2014-01-21 19:32:23      阅读:409      评论:0      收藏:0      [点我收藏+]

    曾经参加高德一道面试题,怎么把类似(175.46,37.876)坐标转化为简单的字符串来表示,当时还认为这是一道非常牛逼的题目,非常考察一个人的思维能力。回来的路上,想了一下,不就是考察了String的两个方法吗!fromCharCode和charCodeAt。

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
<script type="text/javascript">
            function coordinateToStr(x,y){
                x=x.toFixed(4);
                y=y.toFixed(4);
                x=x.toString().split(".");
                y=y.toString().split(".");
                return String.fromCharCode(x[0])+String.fromCharCode(x[1])+String.fromCharCode(y[0])+String.fromCharCode(y[1]);
            }
            function strToCoordinate(str){
                var t=[];
                t.push(str.charCodeAt(0));
                t.push(str.charCodeAt(1));
                t.push(str.charCodeAt(2));
                t.push(str.charCodeAt(3));
                return {
                    x:Number(t[0]+"."+t[1]),
                    y:Number(t[2]+"."+t[3])
                }
            }
            var str=coordinateToStr(175.46,37.876);
            console.log(str);
            var coo=strToCoordinate(str);
            console.log(coo);
 
        </script>

 

  

坐标转化为最简单的字符串

原文:http://www.cnblogs.com/HPhone/p/3528428.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!