I used split and join to swap latitude and longitude of the coordinates. I am using leaflet to get the coordinates and as polyline, swapping coordinates works just fine but when I do it using marker, the last digit of the latitude and first digit of longitude is gone
This is the code, as you can see they’re just the same code
if (type === 'polyline') {
const res = ["[" +
tmp.map(
x => '[' +
swapLatLon(
stripFirstLast(x).split(', ')
).join(', ') +
']'
).join(', ') +
"]"
];
document.getElementById("polyline").value = res;
} else if (type === 'marker') {
const res = ["[" +
tmp.map(
x => '[' +
swapLatLon(
stripFirstLast(x).split(', ')
).join(', ') +
']'
).join(', ') +
"]"
];
document.getElementById("marker").value = res;
} else {
console.log('__undefined__');
}
This is a sample result for res
Original: [37.0902, 95.7129]
Expectation: [[95.7129, 37.0902]]
Reality: [[95.712, 7.0902]]
>Solution :
If you only want to swap places of two variables in an array, then use deconstructing.
const tmp = [37.0902, 95.7129]
let [long, lat] = tmp;
let res = [[lat, long]];
let type;
if (type === 'polyline') {
document.getElementById("polyline").value = res;
} else if (type === 'marker') {
document.getElementById("marker").value = res;
} else {
console.log('__undefined__');
}
console.log({tmp})
console.log({res})