开发者

Convert array of latLng values so I can POST using jQuery-Ajax

开发者 https://www.devze.com 2023-03-27 11:49 出处:网络
I got an array of latLng values and want to post this via jQuery Ajax. How did I have to convert the array?

I got an array of latLng values and want to post this via jQuery Ajax. How did I have to convert the array?

Best re开发者_如何学Pythongards ...


You could do this:

 var coords = ['(52.37253609047836, 4.868355474853502)', '(52.37253609047836, 4.868355474853502)', '(52.37253609047836, 4.868355474853502)'];

var coordConverted = [];

for (var i = 0; i < coords.length; i++) {
    var values = coords[i].match(/\d+\.\d+/g);
    coordConverted.push({
        latitude: values[0],
        longitude: values[1]
    });
}

$.ajax({
    type: 'POST',
    data: {coords: coordConverted},
    url: 'server'
})

If, and only if, the server is expecting post variable array coords. The output looks like this:

{coords: [{"latitude":"52.37253609047836","longitude":"4.868355474853502"},{"latitude":"52.37253609047836","longitude":"4.868355474853502"},{"latitude":"52.37253609047836","longitude":"4.868355474853502"}]}


Joey was almost there.

lonLatArray = [....prepopulated...];
coords = [];

for(var i=0;i<latLonArray.length;i++){
    coords.push({
        lat: lonLatArray[i].lat(),
        lon: lonLatArray[i].lon()
    });
}

$.post("ajax.php",{data: coords},function(data){
    console.log("success");
});
0

精彩评论

暂无评论...
验证码 换一张
取 消