如何将值添加到 javascript 中的 json?
how to add values into json in javascript?
我正在为无人机课程创建地图,我正在使用 mapbox gl js 并绘制我能够创建航路点并获取坐标,但我试图用 json 创建多个课程,id 课程,坐标等等
谁能帮帮我谢谢。
这个功能帮助我创建课程:
map.on('draw.modechange', function(e) {
map.on('click', function(e) {
if (draw.getMode() == 'draw_polygon') {
lon = e.lngLat.lng;
lat = e.lngLat.lat;
gps[nbPoints][0] = lon;
gps[nbPoints][1] = lat;
nbPoints = nbPoints+1;
console.log("Longitude : "+lon + " - " + "Latitude : "+lat);
};
});
});
我使用这个函数将数据发送到我的服务器:
$("#vol").click(function(e)
{
for (var i = 0 ; i < nbPoints; i++) {
posLon = gps[i][0];
posLat = gps[i][1];
lon = posLon;
lat = posLat;
}
const obj ={
"numero":"3",
"r_name":"parcours2",
"speed":"6",
"actionFinished":"home",
"coordinate":
{
"waypoint":[
{
"longitude":lon,
"latitude":lat,
"EnablePicture":"false",
"PositionBatiment":"gauche",
"altitude":"30"
}
]
}
}
const jsonString = JSON.stringify(obj);
const xhr = new XMLHttpRequest();
xhr.open("POST", "Api.php", true);
xhr.setRequestHeader('Content-Type', 'application/json; charset=UTF-8');
xhr.send(jsonString);
console.log(jsonString);
});
也许是这样的?
$("#vol").click(function (e) {
const obj = {
"numero": "3",
"r_name": "parcours2",
"speed": "6",
"actionFinished": "home",
"coordinate":
{
"waypoint": []
}
}
for (var i = 0; i < nbPoints; i++) {
obj.coordinate.waypoint.push(
{
"longitude": gps[i][0],
"latitude": gps[i][1],
"EnablePicture": "false",
"PositionBatiment": "gauche",
"altitude": "30"
}
)
}
const jsonString = JSON.stringify(obj);
const xhr = new XMLHttpRequest();
xhr.open("POST", "Api.php", true);
xhr.setRequestHeader('Content-Type', 'application/json; charset=UTF-8');
xhr.send(jsonString);
console.log(jsonString);
});
如果我理解你正在尝试做的事情是正确的
我正在为无人机课程创建地图,我正在使用 mapbox gl js 并绘制我能够创建航路点并获取坐标,但我试图用 json 创建多个课程,id 课程,坐标等等
谁能帮帮我谢谢。
这个功能帮助我创建课程:
map.on('draw.modechange', function(e) {
map.on('click', function(e) {
if (draw.getMode() == 'draw_polygon') {
lon = e.lngLat.lng;
lat = e.lngLat.lat;
gps[nbPoints][0] = lon;
gps[nbPoints][1] = lat;
nbPoints = nbPoints+1;
console.log("Longitude : "+lon + " - " + "Latitude : "+lat);
};
});
});
我使用这个函数将数据发送到我的服务器:
$("#vol").click(function(e)
{
for (var i = 0 ; i < nbPoints; i++) {
posLon = gps[i][0];
posLat = gps[i][1];
lon = posLon;
lat = posLat;
}
const obj ={
"numero":"3",
"r_name":"parcours2",
"speed":"6",
"actionFinished":"home",
"coordinate":
{
"waypoint":[
{
"longitude":lon,
"latitude":lat,
"EnablePicture":"false",
"PositionBatiment":"gauche",
"altitude":"30"
}
]
}
}
const jsonString = JSON.stringify(obj);
const xhr = new XMLHttpRequest();
xhr.open("POST", "Api.php", true);
xhr.setRequestHeader('Content-Type', 'application/json; charset=UTF-8');
xhr.send(jsonString);
console.log(jsonString);
});
也许是这样的?
$("#vol").click(function (e) {
const obj = {
"numero": "3",
"r_name": "parcours2",
"speed": "6",
"actionFinished": "home",
"coordinate":
{
"waypoint": []
}
}
for (var i = 0; i < nbPoints; i++) {
obj.coordinate.waypoint.push(
{
"longitude": gps[i][0],
"latitude": gps[i][1],
"EnablePicture": "false",
"PositionBatiment": "gauche",
"altitude": "30"
}
)
}
const jsonString = JSON.stringify(obj);
const xhr = new XMLHttpRequest();
xhr.open("POST", "Api.php", true);
xhr.setRequestHeader('Content-Type', 'application/json; charset=UTF-8');
xhr.send(jsonString);
console.log(jsonString);
});
如果我理解你正在尝试做的事情是正确的