如何在javascript

时间:2018-02-01 16:36:46

标签: javascript json ajax google-maps

这是我的代码,用于标记我在谷歌地图中从我的数据库中提取的所有地方。

 $.ajax({
        url:"http://localhost/church_finder/index.php/MapController/search_church",
                type:'POST',
                data:{coordinates:coordinates}
                 success: receiver               
               });

这里我用函数接收器调用我从数据库得到的结果。

function receiver(data, textStatus, XMLHttpRequest) {

var json = JSON.parse(data);

var features = [
            for( var i=0; i<json.length; i++) {
            var lat=json[0]["lat"];
            var lng=json[0]["lng"];
                {
                    position: new google.maps.LatLng(lat,lng),
                },
            }
            ];

     features.forEach(function(feature) {
                var marker1 = new google.maps.Marker({
                    position: feature.position,
                    map: map
                });
            });     
} 

这是我从ajax获得的结果数据

  

[{ “LAT”: “10.526800731337735”, “LNG”: “76.20941162109375”},   { “LAT”: “10.622100079463674”, “LNG”: “76.1424207687378”},{ “LAT”: “10.604004340704408”, “LNG”: “76.14100456237793”},{ “LAT”: “10.608644375798574”, “LNG”:” 76.13735675811768 “},{” LAT “:” 10.624419968495433" , “LNG”: “76.13675594329834”},{ “LAT”: “10.62436724394038”, “LNG”: “76.13685250282288”},{ “LAT”: “10.624377788852131”,” LNG “:” 76.13693833351135 “},{” LAT “:” 10.615815200680679" , “LNG”: “76.1367130279541”},{ “LAT”: “10.601726479547619”, “LNG”: “76.13688468933105”},{ “LAT”:” 10.610500370131295" , “LNG”: “76.13244295120239”},{ “LAT”: “10.631991120088175”, “LNG”: “76.13566160202026”}]

但是当我在var功能中使用forloop时,我收到了类似“Uncaught SyntaxError:Unexpected token for”的错误。如何在谷歌地图中循环并标记所有这些坐标。

2 个答案:

答案 0 :(得分:2)

您无法将for关键字放在数组中,正如您在features = [ for (...) {} ]中尝试的那样。试试这个:

function receiver(data, textStatus, XMLHttpRequest) {

  var json = JSON.parse(data);

  var features = [];
  for (var i = 0; i < json.length; i++) {
    var lat = json[i]["lat"];
    var lng = json[i]["lng"]; 
    // push object into features array
    features.push({ position: new google.maps.LatLng(lat, lng) });
  }

  features.forEach(function(feature) {
    var marker1 = new google.maps.Marker({
      position: feature.position,
      map: map
    });
  });
}

或者,为了使其更简洁,您可以将ES6 Object destructuringArray.map一起使用,如下所示:

var features = json.map(({lat, lng}) => ({position: new google.maps.LatLng(lat, lng)}));

答案 1 :(得分:1)

这是你的方法

var json = JSON.parse(data);

var features = [
    for( var i=0; i<json.length; i++) {
    var lat=json[0]["lat"];
    var lng=json[0]["lng"];
        {
            position: new google.maps.LatLng(lat,lng),
        },
    }
    ];

features.forEach(function(feature) {
        var marker1 = new google.maps.Marker({
            position: feature.position,
            map: map
        });
    });     
} 

您的代码中存在一些问题

您尝试在两个方括号for-loop

中执行[]
var features = [  
    for( var i=0; i<json.length; i++) { <- Here
    var lat=json[0]["lat"];
    var lng=json[0]["lng"];
        {
            position: new google.maps.LatLng(lat,lng),
        },
    }
    ];
    ^

您总是与位置0

进行交流
var lat=json[0]["lat"];
var lng=json[0]["lng"];
             ^

您的表达式从未分配给变量:

{
    position: new google.maps.LatLng(lat,lng),
},
^

使用这些修补程序查看此代码段

var json = [{"lat":"10.526800731337735","lng":"76.20941162109375"}, {"lat":"10.622100079463674","lng":"76.1424207687378"},{"lat":"10.604004340704408","lng":"76.14100456237793"},{"lat":"10.608644375798574","lng":"76.13735675811768"},{"lat":"10.624419968495433","lng":"76.13675594329834"},{"lat":"10.62436724394038","lng":"76.13685250282288"},{"lat":"10.624377788852131","lng":"76.13693833351135"},{"lat":"10.615815200680679","lng":"76.1367130279541"},{"lat":"10.601726479547619","lng":"76.13688468933105"},{"lat":"10.610500370131295","lng":"76.13244295120239"},{"lat":"10.631991120088175","lng":"76.13566160202026"}];

var features = [];

for (var i = 0; i < json.length; i++) {
  var lat = json[i]["lat"];
  var lng = json[i]["lng"]; 

  features.push({
    position: new google.maps.LatLng(lat, lng),
  });
}

features.forEach(function(feature) {
  var marker1 = new google.maps.Marker({
    position: feature.position,
    map: map
  });
});

资源