如何为谷歌地图的位置数组赋值?

时间:2013-06-11 05:12:59

标签: javascript jquery google-maps google-maps-api-3

我有一个数组{$address = array(..........);} 我把它改成了 {

foreach ($address as $key => $val){
    $points[] = "['{$val}', '{$val}']";
}
$output = join ("," , $points);
$req_format = strip_tags($output);

}

输出: ['30 South Wacker Drive Floor 22 Chicago IL 60606','30 South Wacker Drive Floor 22 Chicago IL 60606',['288 Bishopsgate London,EC2M 4QP United Kingdom','288 Bishopsgate London,EC2M 4QP United Kingdom'], ['260麦迪逊大街8楼纽约纽约10016','麦迪逊大街260号纽约纽约100街100号']

我需要将此值赋给js变量:

var locationsArray = [
['30 South Wacker Drive Floor 22 Chicago IL 60606', '30 South Wacker Drive Floor 22 Chicago IL 60606'],
['30 South Wacker Drive Floor 22 Chicago IL 60606', '30 South Wacker Drive Floor 22 Chicago IL 60606']
];

如何指定php变量$ req_format等于js变量locationArray = [??????];

2 个答案:

答案 0 :(得分:1)

你可以使用json,比如

foreach ($address as $key => $val){
    $points[] = "['{$val}', '{$val}']";
}
$jsoned = json_encode($points);
//pass it to your js
var js_data = "<?php echo $jsoned; ?>";

答案 1 :(得分:0)

foreach ($address as $key => $val){
    $val = strip_tags($val);
    $points[] = array($val, $val);
}
$json = json_encode($points);
$json = str_replace('\n',' ',$json);
$json = str_replace('\r',' ',$json);

var locations = '<?php echo $json;?>';
var locations_array = JSON.parse(locations);

var locationsArray = locations_array;