这是我的代码,用于标记我在谷歌地图中从我的数据库中提取的所有地方。
$.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”的错误。如何在谷歌地图中循环并标记所有这些坐标。
答案 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 destructuring与Array.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
});
});