I am trying to create a way to loop through a bunch of address and output a list of ones that are within a set distance from another address. So In WordPress, I have a custom post type I am looping through and creating an array of addresses like this,
<?php
$args = array( 'post_type' => 'friday_location', 'posts_per_page' => '-1' );
$arr = array();
$i=0;
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
$meta = get_post_meta( $post->ID);
$string = $meta['_cmb_address'][0] . '+' . $meta['_cmb_city'][0] . '+' .$meta['_cmb_state'][0] . '+' .$meta['_cmb_zip'][0];
$string = str_replace(' ', '+', $string);
$arr[$i] = $string;
$i++;
endwhile;
wp_reset_postdata();
?>
After this, inside of the script tag I am converting the array like this
var locations = <?php echo json_encode($arr ); ?>;
Now I have a JS array, I think, of addresses and I am wanting to pass this array into the Google Maps API Distance Matrix, https://developers.google.com/maps/documentation/javascript/distancematrix#distance_matrix_requests
The relevant code piece looks like,
function calculateDistances() {
var opts = {
center: new google.maps.LatLng(38.6,-98),
zoom: 4,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
map = new google.maps.Map(document.getElementById('map'), opts);
geocoder = new google.maps.Geocoder();
var service = new google.maps.DistanceMatrixService();
service.getDistanceMatrix(
{
origins: [origin2],
destinations: locations,
travelMode: google.maps.TravelMode.DRIVING,
unitSystem: google.maps.UnitSystem.IMPERIAL,
avoidHighways: false,
avoidTolls: false
}, callback);
}
I know the destinations can take an array, so I am trying to give it one, dynamically made, however I am not sure why it is not working as I see it in my head :)