Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/440.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sqlite/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在javascript中使用变量_Javascript_Json_Google Maps Api 3 - Fatal编程技术网

在javascript中使用变量

在javascript中使用变量,javascript,json,google-maps-api-3,Javascript,Json,Google Maps Api 3,从google maps API文档中,我得到了以下信息: var flightPlanCoordinates = [ new google.maps.LatLng(37.772323, -122.214897), new google.maps.LatLng(21.291982, -157.821856), new google.maps.LatLng(-18.142599, 178.431), new google.maps.LatLng(-27.46758,

从google maps API文档中,我得到了以下信息:

var flightPlanCoordinates = [
    new google.maps.LatLng(37.772323, -122.214897),
    new google.maps.LatLng(21.291982, -157.821856),
    new google.maps.LatLng(-18.142599, 178.431),
    new google.maps.LatLng(-27.46758, 153.027892)
  ];
我试着用我自己的循环复制这段代码,这样我就可以使用以前存储的co ORD,如下所示:

var output;
$.getJSON('DisplayMap.php',  function(data) {
        var output = "[ ";
        for (var i in data.b) {
            output+= "new google.maps.LatLng" + "(" + data.b[i].Ya + ", " + data.b[i].Za + ")," + " ";
        }

       output+= "]";
        document.getElementById("placeholder").innerHTML=output;
    alert(output);
  });


                output+= "]";
我检查了输出结果,结果完全符合我的要求。但是,当我这样替换它时,它不起作用:

var flightPlanCoordinates = output;

在新代码中,您正在构建字符串而不是数组。你应该有这样的东西:

var output = [];

for (var i in data.b) {
    output.push(new google.maps.LatLng(data.b[i].Ya, data.b[i].Za));
}

此外,我对您在
getJSON
的内部和外部创建名为
output
的变量感到有点困惑。请记住,
getJSON
是异步的,在调用
getJSON
变量后,您将无法立即使用该变量。

在新代码中,您正在构建字符串而不是数组。你应该有这样的东西:

var output = [];

for (var i in data.b) {
    output.push(new google.maps.LatLng(data.b[i].Ya, data.b[i].Za));
}

此外,我对您在
getJSON
的内部和外部创建名为
output
的变量感到有点困惑。请记住,
getJSON
是异步的,在调用
getJSON
后,您将无法立即使用该变量。

您必须设置正确的javascript
数组,而不是字符串。像这样:

var output = [];
$.getJSON('DisplayMap.php',  function(data) {
        for (var i in data.b) {
            output.push(new google.maps.LatLng(data.b[i].Ya, data.b[i].Za));
        }
  });

您必须设置正确的javascript
数组,而不是字符串。像这样:

var output = [];
$.getJSON('DisplayMap.php',  function(data) {
        for (var i in data.b) {
            output.push(new google.maps.LatLng(data.b[i].Ya, data.b[i].Za));
        }
  });

欢迎来到异步的精彩世界!你不能那样做。欢迎来到异步的奇妙世界!你不能那样做,谢谢。现在我有了您提供的代码,我将如何布局var flightPlanCoordinates=[new google.maps.LatLng(37.772323,-122.214897),new google.maps.LatLng(21.291982,-157.821856),new google.maps.LatLng(-18.142599178.431),new google.maps.LatLng(-27.46758153.027892)];我得到了它。在那里输入int。干杯,谢谢。现在我有了您提供的代码,我将如何布局var flightPlanCoordinates=[new google.maps.LatLng(37.772323,-122.214897),new google.maps.LatLng(21.291982,-157.821856),new google.maps.LatLng(-18.142599178.431),new google.maps.LatLng(-27.46758153.027892)];我得到了它。在那里输入int。大家好