我在服務器 (PHP) 上運行以下命令,在其中循環(huán)我的帖子并獲取我在 gmap 字段中的一些坐標: $location = get_field('location'); $lat = $location['lat']; $lng = $location['lng'];然后我像這樣創(chuàng)建一對 lat 和 lng 坐標: $coordinates = $lat.", ".$lng; echo $coordinates;然后在 JavaScript ajax 客戶端上成功,我將這些對中的每一個都推送到var coords = [];我在頁腳中的數(shù)組中。但是我在控制臺中得到了一個奇怪的結(jié)果:["4"](index):148 (2) ["4", "0"](index):148 (3) ["4", "0", "."](index):148 (4) ["4", "0", ".", "7"](index):148 (5) ["4", "0", ".", "7", "2"](index):148 (6) ["4", "0", ".", "7", "2", "7"](index):148 (7) ["4", "0", ".", "7", "2", "7", "2"](index):148 (8) ["4", "0", ".", "7", "2", "7", "2", "0"]...所以這是整個代碼:PHP function data_fetch(){ $dates = $_POST['dates']; $dates = explode(',', $dates); $args = array( 'meta_query' => array( array( 'key' => 'anno', 'value' => array($dates[0], $dates[1]), 'compare' => 'BETWEEN', 'type' => 'NUMERIC' ), ) ); $query = new WP_Query( $args ); if( $query->have_posts() ): while( $query->have_posts() ) : $query->the_post(); $location = get_field('location'); $lat = $location['lat']; $lng = $location['lng']; $coordinates = $lat.", ".$lng; echo $coordinates; endwhile; endif; die(); }JavaScript$(document).ready(function() { $("#searchNations").on("click", function() { //clearOverlays(); fetch(datesSearch); }); fetch(datesSearch); function fetch(datesSearch) { $.ajax({ url: '<?php echo admin_url(' admin - ajax.php '); ?>', type: 'post', dataType: 'json', data: { action: 'data_fetch', dates: datesSearch }, success: function(data) { var data = $.parseJSON(data); for (var i = 0; i < data.length - 1; i++) { coords.push(data[i]); console.log(coords); }; } }); }});
1 回答

Qyouu
TA貢獻1786條經(jīng)驗 獲得超11個贊
在 php 中,您將坐標作為字符串輸出,但在 javascript 中將它們作為 json 處理。您必須將坐標推入數(shù)組并對其進行編碼:
if( $query->have_posts() ):
$coordinates = [];
while( $query->have_posts() ) : $query->the_post();
$location = get_field('location');
$lat = $location['lat'];
$lng = $location['lng'];
$coordinates[] = $lat.", ".$lng;
endwhile;
echo json_encode($coordinates);
die;
endif;
- 1 回答
- 0 關注
- 129 瀏覽
添加回答
舉報
0/150
提交
取消