如何将地图坐标数组存储到php变量中?

问题描述 投票:-1回答:1

我正在尝试从数组中获取坐标以添加到Google地图中以输出多边形。

而且我在尝试将我的数组($ ArrayCoords)作为字符串存储到变量中时遇到一些困难。

我有这个数组,其中包含多个lat和lng坐标以及一个缩放:

$ArrayCoords = get_field('Array_Coords');
print_r($ArrayCoords); 

/*   Print out as:

 [draw_map] => Array
            (
                [coords] => Array
                    (
                        [0] => Array
                            (
                                [lat] => 38.928713698662
                                [lng] => -94.537423253059
                            )

                        [1] => Array
                            (
                                [lat] => 38.927679421508
                                [lng] => -94.537782669067
                            )

                        [2] => Array
                            (
                                [lat] => 38.928038315258
                                [lng] => -94.53973531723
                            )

                        [3] => Array
                            (
                                [lat] => 38.928171856655
                                [lng] => -94.54069018364
                            )

                        [4] => Array
                            (
                                [lat] => 38.928215341202
                                [lng] => -94.541194438934
                            )

                        [5] => Array
                            (
                                [lat] => 38.929258624074
                                [lng] => -94.541215896606
                            )

                    )

                    [zoom] => 13
         )
*/

我正在尝试将此数组存储为变量($ latLngCoords和$ Zoom),该变量将作为:

echo $latLngCoords;

/*
38.928713698662, -94.537423253059
38.927679421508, -94.537782669067
....and so on..
*/

echo $Zoom;

// 13

我不确定该怎么做。

php arrays loops google-maps multidimensional-array
1个回答
0
投票

感谢所有人,如果我的问题不清楚,我很抱歉:(但我想我用while循环弄清楚了答案:

$Zoom = $ArrayCoords[zoom];
$array = $ArrayCoords[coords];
$arrayCount = count($array) - 1;
//arrayCount is 6, but because of array starting at 0, we need to minus -1

$i=0;
while($i <= $arrayCount)
  {
  $latLngCoords .= $array[$i][lat] . ", " . $array[$i][lng] . "<br>";

  $i++;

  }

echo $latLngCoords;

echo $Zoom;

 //print_r($array);
© www.soinside.com 2019 - 2024. All rights reserved.