首页 > 解决方案 > How to make an appropriate array for coordinates?

问题描述

I have an array that returns 28 (latitude and longitude). I'll format to send to database, so here my questions:

  1. As you can see, the latitude and longitude comes always together, how can i separate? 2.the bold lines always return the same coordinate, how can I remove to send the minimal to database?

Code below:

var capturaCoordenada = [];

if (data.waypoints.length === 12) {
        for (var i = 0; i < capturaCoordenada.length; i++) {
        exibeCoordenada = exibeCoordenada + "\n" + capturaCoordenada[i] + capturaCoordenada[i++]; }
        exibeCoordenada = exibeCoordenada + "\n";

    window.alert(exibeCoordenada);

OBS: I receive the coordinates like this:
Data:

-49.3766505877158,-20.80796326493855-49.3766505877158,-20.80796326493855 -49.37949730565916,-20.80670816431558-49.37949730565916,-20.80670816431558 -49.38166453054308,-20.805324135912116-49.38166453054308,-20.805324135912116 -49.38413216283695,-20.806146531132384-49.38413216283695,-20.806146531132384 -49.38209368398577,-20.80891456013579-49.38209368398577,-20.80891456013579 -49.37981917074134,-20.81096046195445-49.37981917074134,-20.81096046195445 -49.37743736913569,-20.811140981370414-49.37743736913569,-20.811140981370414

标签: javascriptarrays

解决方案


在这部分代码中

exibeCoordenada = exibeCoordenada + "\n" + capturaCoordenada[i] + capturaCoordenada[i++];

您要添加两次相同的内容,尝试删除其中一个,如下所示:

exibeCoordenada = exibeCoordenada + "\n" + capturaCoordenada[i];

然后你应该有一个如下所示的结果

-49.3766505877158,-20.80796326493855
-49.37949730565916,-20.80670816431558
-49.38166453054308,-20.805324135912116

您应该能够将字符串转换为列表,如下所示:

exibeCoordenada.split("\n").map(x => x.split(","))

这给了你这个:

[["-49.3766505877158", "-20.80796326493855"], ["-49.37949730565916", "-20.80670816431558"], ["-49.38166453054308", "-20.805324135912116"]]

推荐阅读