【问题标题】:How to change this array to work with google maps api如何更改此数组以使用 google maps api
【发布时间】: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


    【解决方案1】:

    您需要更改密钥名称,但我认为这会起作用:

    var beaches = <?= json_encode($php_array);?>;
    

    【讨论】:

    • 哦,是的,对不起,我在编码之前的 pre 中重复了它
    【解决方案2】:

    您所拥有的是 javascript 中的列表列表。您必须将 php 中的原始数据转换为等效数据,然后将其作为 json 返回。我会选择一个函数来做到这一点。

    <?php
    function convertMapDataInfo($mapData) {
        // defining the outer list
        $newArray = array();
        if(is_array($mapData) && count($mapData)>0) {
            // iterate each list
            foreach($mapData as $key => $value) {
                // convert it to the structure equivalent to the
                // javascript 
                $currentInfo = array(
                                     $value['title'],
                                     $value['lat'],
                                     $value['long'],
                                     $value['id']
                               );
                // then appending the inside list to the outer list
                $newArray[] = $currentInfo;
            }
        }
        // return the outer list with all the lists inside as a json. 
        // Which is what google api expects
        return json_encode($newArray);
    }
    

    然后调用它并将其传递给javascript变量:

    <?php $googleMapData = convertMapDataInfo($mapdata);  ?>
    
    <script>
    var beaches = <?= $googleMapData; ?>
    </script>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-10-07
      • 2016-10-29
      • 1970-01-01
      • 2019-01-23
      • 1970-01-01
      • 2018-11-09
      • 2021-03-26
      • 2013-08-31
      相关资源
      最近更新 更多