-3

当我在页面加载时将 JSON 数据视为警报时,我看到:

[
    "Ankida Ridge Vineyards – The Vrooman Family Vineyard & Winery",
    37.69603499999999,
    -79.17580859999998,
    "<div id=\"post-45\"><a href=\"http://localhost/jhtwp/business/ankida-ridge-vineyards-the-vrooman-family-vineyard-winery/\">Ankida Ridge Vineyards &#8211; The Vrooman Family Vineyard &#038; Winery</a></div>"
]

它通过 JSONlint。然而,在我的源数据中,我看到了这一点:

/* <![CDATA[ */
var busipress_map_vars = [];
busipress_map_vars = {"default_map_icon":"http:\/\/localhost\/jhtwp\/wp-content\/plugins\/busipress\/img\/red-dot.png","default_map_icon_width":32,"default_map_icon_height":32,"active_map_icon":"http:\/\/localhost\/jhtwp\/wp-content\/plugins\/busipress\/img\/blue-dot.png","active_map_icon_width":32,"active_map_icon_height":32,"map_icon_shadow":"http:\/\/localhost\/jhtwp\/wp-content\/plugins\/busipress\/img\/msmarker.shadow.png","map_icon_shadow_width":59,"map_icon_shadow_height":32,"center_lat":-34.397,"center_long":150.644,"path_start":"35.7719444, -78.6388889","path_end":"32.7763889,  -79.9311111","locations":"[\"Ankida Ridge Vineyards &#8211; The Vrooman Family Vineyard &#038; Winery\",37.69603499999999, -79.17580859999998, \"<div id=\\\"post-45\\\"><a href=\\\"http:\/\/localhost\/jhtwp\/business\/ankida-ridge-vineyards-the-vrooman-family-vineyard-winery\/\\\">Ankida Ridge Vineyards &#8211; The Vrooman Family Vineyard &#038; Winery<\/a><\/div>\"]"};;
/* ]]> */
</script>

这是转义双引号(那里还有一些其他数据可以正常工作)。我有以下生成每个位置的函数:

function busipress_business_teaser($echo = true, $zindex) {

    $location = get_field('address');

    $location_array = explode(',',$location['coordinates']);

    ob_start();
    echo '["';
    echo get_the_title();
    echo '",';
    echo $location_array[0] . ', ';
    echo $location_array[1] . ', ';
    echo '"<div ';
    echo 'id=\"post-'.get_the_ID().'\">';
    echo '<a href=\"'.get_permalink().'\">';
    echo get_the_title();
    echo '</a>';
    echo '</div>"';
    echo ']';
    $display = ob_get_contents();

    ob_end_clean();

    if($echo) {
        echo $display;
    }
    else {
        return $display;
    }

}

我是否以错误的方式创建这些数据?将此与 Google Maps API v3 一起使用。谢谢!

4

1 回答 1

0

我认为你的功能应该是这样的

function busipress_business_teaser($echo = true, $zindex) {
    $location = get_field('address');
    list($lat,$log) = explode(',',$location['coordinates']);

    $json = array();
    $json[]=  get_the_title();
    $json[] = $lat ;
    $json[] = $log ;
    $json[] = sprintf('<div id=\"post-%d\"><a href=\"%s\">%s</a></div>',get_the_ID(),get_permalink(),get_the_title());
    return json_encode($json);
}

如果您想使用 Goolge MAP,那么 PHP 类就足以做到这一点

于 2012-10-06T18:12:58.210 回答