I run the following on the server (PHP), where I loop my posts and I grab some coordinates I have in a gmap field:
$location = get_field('location');
$lat = $location['lat'];
$lng = $location['lng'];
I then create a single pair of lat and lng coordinate like this:
$coordinates = $lat.", ".$lng;
echo $coordinates;
And then on the client in JavaScript ajax success I push each of those pair in an array var coords = []; which I have in the footer.
But I get a weird result in console:
["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"]...
So this is the whole code:
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);
};
}
});
}
});
console.log(data)on the first line of the success handler. You are not getting back what you think ("number,numner").49.302473949.2832529$coordinates = $lat+", "+$lng;as it's php instead of$coordinates = $lat.", ".$lng;as I had40.7272074, 8.57526649999999840.7197406, 8.563512299999957which is correct but wrongly written, they are 4 coordinates and two pairs, should be40.7272074, 8.575266499999998, 40.7197406, 8.563512299999957so it is not splitting a single pairs