duan6832168 2014-04-17 07:36
浏览 29
已采纳

使用json_encode从PHP数组填充Google Map Markers

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.

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

The hard coded markers are:

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']
       ];

and they work.

The php array is:

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

which produces the array,

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
        )
)

so no problem as yet.

Now when I json_encode the above array

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

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

document.write(locations);

it shows up as

[["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.

  • 写回答

1条回答 默认 最新

  • dongqian6484 2014-04-17 08:19
    关注

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

    var locations = '<?php echo json_encode($locations); ?>';
    //--------------^--------------------------------------^
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用
  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教