【发布时间】:2016-02-19 10:25:26
【问题描述】:
这是我拥有的已被 json 编码的数组,我正在尝试使其与 google maps api 一起使用:
Array (
[0] => Array
(
[title] => Something
[lat] => -25.364
[long] => 131.044
[id] => 7
)
[1] => Array
(
[title] => Hello world!
[lat] => 53.4315768
[long] => -2.9634503
[id] => 1
)
)
用这个构建:(其中很多是特定于 wordpress 的)
function wp_cat_map_wp_query() {
$args = array(
'post_type' => 'post',
'orderby' => 'title',
'order' => 'desc',
'meta_query' => array (
array (
'key' => '_wp_cat_map_long',
'value' => '',
'compare' => '!='
),
array (
'key' => '_wp_cat_map_lat',
'value' => '',
'compare' => '!='
)
),
);
$mapdata = get_posts($args);
if( is_array($mapdata) ) {
foreach($mapdata as $map) {
// Cook up the listing data
$permalink = get_permalink($map->ID);
$long = get_post_meta($map->ID,'_wp_cat_map_long',true);
$lat = get_post_meta($map->ID,'_wp_cat_map_lat',true);
$image = get_the_post_thumbnail( $map->ID );
// Build json array
$json[] = array(
"title" => strip_tags(str_replace("'","",substr($map->post_title,0,20))),
"lat" => $lat,
"long" => $long,
"id" => $map->ID
);
}
}
//echo '<pre>';print_r($json);echo '</pre>';
// Return json output
if(empty($json)){ return ""; }else{ return json_encode($json); }
}
如何将其转换为如下所示的数组以使用 google maps api?
var beaches = [
['Bondi Beach', -33.890542, 151.274856, 4],
['Coogee Beach', -33.923036, 151.259052, 5],
['Cronulla Beach', -34.028249, 151.157507, 3],
['Manly Beach', -33.80010128657071, 151.28747820854187, 2],
['Maroubra Beach', -33.950198, 151.259302, 1]
];
【问题讨论】:
标签: javascript php arrays wordpress google-maps-api-3