在 javascript 中将 Sting 转换为 [x, y] 坐标
<script type="text/javascript">
var src = [40.6976, -74.2598];
function convertToDMS(src) {
function toDMS(n) {
n = Math.abs(n);
var d = Math.floor(n);
n = n - d;
n *= 60;
var m = Math.floor(n);
n = n - m;
n *= 60;
var s = Math.floor(n);
return "" + d + " " + m + " " + s;
}
var dir0 = src[0] > 0 ? "N" : "S";
var dir1 = src[1] > 0 ? "E" : "W";
console.log(toDMS(src[0]) + dir0);
console.log(toDMS(src[1]) + dir1);
}
convertToDMS(src);
</script>
<div id="locationdiv">[40.6976,-74.2598]</div>
使用上面的脚本,我可以获得度、分和秒,例如 40°41'46.0"N+74°18'08.0"W,
However if instead of using
var src = [40.6976,-74.2598];
I use
var src = document.getElementById("locationdiv").innerHTML;
该脚本在控制台中不起作用且没有错误。如何将作为字符串出现的innerHTML转换为可以使用的形式[40.6976,-74.2598]。
<script type="text/javascript">
var src = [40.6976, -74.2598];
function convertToDMS(src) {
function toDMS(n) {
n = Math.abs(n);
var d = Math.floor(n);
n = n - d;
n *= 60;
var m = Math.floor(n);
n = n - m;
n *= 60;
var s = Math.floor(n);
return "" + d + " " + m + " " + s;
}
var dir0 = src[0] > 0 ? "N" : "S";
var dir1 = src[1] > 0 ? "E" : "W";
console.log(toDMS(src[0]) + dir0);
console.log(toDMS(src[1]) + dir1);
}
convertToDMS(src);
</script>
<div id="locationdiv">[40.6976,-74.2598]</div>
Using the script above I am able to get degree, minute and second like 40°41'46.0"N+74°18'08.0"W
However if instead of using
var src = [40.6976,-74.2598];
I use
var src = document.getElementById("locationdiv").innerHTML;
the script does not work with no error in console. How do I convert the innerHTML which comes as a string to the form [40.6976,-74.2598] which can be used.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
检查
typeof
是否为"string"
如果为 true — 使用 将参数值转换为对象JSON.解析
Check for
typeof
is"string"
If true — convert the argument value to Object using JSON.parse