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

浙公网安备 33010602011771号