Reputation: 135
I am trying to build a wordpress plugin that displays user locations on a googlemap. The address is geocoded and saved as a lat/long string to geo_address.
I'm then trying to retrieve that data and pass it to the Markers array used by googlemap.
The code to get the geo data is:
$blogusers = get_users_of_blog();
if ($blogusers) {
foreach ($blogusers as $bloguser) {
$user = get_userdata($bloguser->user_id);
$user_location = get_the_author_meta('geo_address', $bloguser->ID );
$gLocations = '[User ID ' . $user->ID . ', ' . $user_location .'],';
I then try to add it to the marker array with:
var markers = "[<?php $gLocations ?>]";
If I echo $gLocations it displays like a perfectly formatted marker array, but in gmaps it displays nothing.
The full gmap code is here http://jsfiddle.net/nE2Ge/
Upvotes: 0
Views: 335
Reputation: 1847
First thing, your javascript 'markers' variable should be an array and not a string as you are creating like in your fiddle where you are using quote marks around your php to output the value of the markers variable is wrong. Secondly, what you commented is not a valid javascript array:
[User ID 1, ], [User ID 43, 51.5784897,-0.02016619999994873], [User ID 44, 58.3498003,11.935649000000012], [User ID 45, 58.2234382,11.920179500000017],
That will cause errors as strings must be quoted, where as you don't have quote marks around User ID 1 and such. So your php needs to be:
$gLocations = '["User ID ' . $user->ID . '", ' . $user_location .'],';
Or perhaps a dotEquals ( .= ) after $gLocations
Then when you output $gLocations to the view, inside the javascript code,
var markers = [<?php echo $gLocations ?>];
Also, you should probably add some checking whether markers[i][1] and markers[i][2] actually exist or not. Should also refine insertion of commas to $gLocations within the php code based upon whether or not $user_location is valid.
Upvotes: 1
Reputation: 657
It looks like you're missing an echo when you create your JavaScript variable. Without the echo nothing gets added to the markers var. Try
var markers = "[<?php echo $gLocations ?>]";
You could also add a console.log(markers) to verify markers contains the correct markup.
console.log(markers);
Upvotes: 0