Is it possible, inside the Google Maps javascript API, to query all WordPress posts for their latitude and longitude locations stored as custom fields, to display all markers on the same map?
var locations = [
<?php query_posts('post_type=property&showposts=9999999');?>
<?php if (have_posts()) : ?><?php while (have_posts()) : the_post(); ?>
['<?php the_title(); ?>', <?php echo get_post_meta( $post->ID, 'latitude', true ); ?>, <?php echo get_post_meta( $post->ID, 'longitude', true ); ?>, <?php the_ID(); ?>],
<?php endwhile; else: ?>
<?php endif; ?>
];
I tried this, but it cuts off after the initial bracket [
when loaded.
I’ve done something similar here. My approach was to use the WordPress wp_localize_script function to generate the parameters, which I think would be your best option.
I don’t have time to pull code out right now, but if you need more I can look later.
Edit
Ok, here you go. This is all based on the link above, so it may have a little more code than you need, but it should do the trick.
First, I created a custom post type called
property
to match your code, and created a couple of properties, giving each a latitude and longitude.Next I created a file called
so.maps.js
, with the following content (thebounds
code is just to make sure the map shows all my markers; you may not need it):The important part is the
php_args
variable – that’s what’s populated by thewp_localize_script
call I mentioned earlier.Then I created a page to show the map, populating the array I wanted to pass to the JavaScript and passing it with
wp_localize_script
:Finally, I made sure the map was visible by setting its size in the CSS:
Hope that helps. I’m no expert on the Google Maps API, but this did the trick for me.
Thanks for your continued help! We sort of combined your code with our previous code, below is the code we ultimately used to make it work properly. However, it seems to timeout if you raise the post query to an amount greater than 200 markers. I’m not sure how to avoid this from happening, because according to Google there is no limit on their end. PHP/Apache limits have been raised, but it didn’t help. A friend suggested its probably a javascript problem. If anyone has further suggestions on allowing larger queries, please comment. 🙂