本文介绍了使用json_encode从PHP数组填充Google Map Markers的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我使用硬编码的javascript数组作为地图标记的输入时,标记显示得很好,所以我知道我用来显示标记的代码是很好的.

When I use the hard coded javascript array as input for the map markers the markers show up just fine, so I know that the code I'm using to display the markers is good.

我的问题是,当我尝试使用json_encode转换php多数组时,地图上没有任何显示.

My problem is when I try and convert a php multi-array using json_encode, nothing shows up on the map.

硬编码标记是:

var locations = [
           ['Sausalito', 37.8590937, -122.4852507,'url'],
           ['Sacramento', 38.5815719, -121.4943996,'url'],
           ['Soledad', 36.424687, -121.3263187,'url'],
           ['Shingletown', 40.4923784, -121.8891586,'url']
       ];

他们起作用了.

php数组是:

$locations = array(array(Sausalito, 37.8590937, -122.4852507,'url'),array(Sacramento, 38.5815719, -121.4943996,'url'));

产生数组,

Array
(
    [0] => Array
        (
            [0] => Sausalito
            [1] => 37.8590937
            [2] => -122.4852507
            [3] => url
        )
    [1] => Array
        (
            [0] => Sacramento
            [1] => 38.5815719
            [2] => -121.4943996
            [3] => url
        )
)

所以没有问题.

现在,当我对上面的数组进行json_encode

Now when I json_encode the above array

var locations = '<?php echo json_encode($locations); ?>';

它不会被javascript映射代码读取.如果我打印变量

it does not get read by the javascript map code. and if I print the variable

document.write(locations);

它显示为

[["Sausalito",37.8590937,-122.4852507,"url"],["Sacramento",38.5815719,-121.4943996,"url"]]

看起来有点像上面的硬编码,但是不会被用于硬编码数据的地图代码读取.

which kinda looks like the hard-coded above, but it does not get read by the map code which works with the hard-coded data.

任何人都可以帮助我,谢谢.

Can anyone assist me, please, much appreciated.

推荐答案

删除单引号,否则locations将是字符串而不是数组:

Remove the single-quotes, otherwise locations will be a string and not an array:

var locations = '<?php echo json_encode($locations); ?>';
//--------------^--------------------------------------^

这篇关于使用json_encode从PHP数组填充Google Map Markers的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-27 02:45