Maps for BuddyPress - Show location of all registered users with custom fields

Hello WPMU Dev!!!
I'm am using bbPress 2.0 and the latest Wordpress.
I have a BP custom field declared for members - this is 'location'. This can be a postcode / location (e.g. m33 OR London).

I want to allow all my non-registered-members to be able to see the locations of all members on a maps (with a link to their profile page & a avatar). I do not want to have to cycle through all the posts.

I'm looking at the plugin code and thinking about building an array of locations through a query on all the users custom location fields. Then somehow attaching that to a 'map'...... perhaps inserting the array directly into the DB table. ......... BUT, I can't help thinking that I would be making life hard for myself. Is there an easier way of doing this!

Also - each of the locations/directions appear on the bottom of the maps - is there a way of hiding this?


  • jaseyb
    • WPMU DEV Initiate

    Hmmmm, looking at the plugin code this appears to be a little more complicated than I expected. It appears that the plugin checks the location before producing Lat and Long (This is sensible).
    Also, most the entries in the DB are 'blobby' - not straight locations (or arrays).

    So my second question (first on still stands) is this

    1) How would I go about creating a new map from pure code
    2) Loop through an array of locations and add them to the newly created map (and discarding any 'There was an error geocoding your location. Check the address and try again').

    I'm a little new to php - I'm finding it very hard to follow the control and logic flow of it (I'm C++/Java)


  • Mason
    • DEV MAN’s Sidekick

    Hiya Jason,

    I'll have to ask the developer to respond to your questions here. It's gonna be rather difficult as the plugin tries to do quite a bit.

    You can change show_posts and show marker lists to false either in the shortcode or plugin code and then only the map will display.

    Shortcode would look like this:
    [map id="1" show_posts="false" show_markers="false"]

  • jaseyb
    • WPMU DEV Initiate

    Thanks TonyRLA - Could you tell me which plugins without being cryptic? :slight_smile: I've looked around (unsuccessfully) and think I would prefer to have a plugin which it supported by WPMU - this will also benefit other users who have the same question.

    MasonJames - thankyou for getting back to me promptly.

    Looking around the plugin code (for this plugin) there is a function:

    autocreate_map ($post_id, $lat, $lon, $add).

    I'm pretty sure this could be simple - it appears that most of the functions are there. I just need to know

    1) How to create a map object (preference is from my function.php)
    2) Create an array of Google Map Locations based on location e.g. London, New York
    3) Pass that into the Map function - get out a map ID, that I can then use as a shortcode.

    Chris Richardson's MapPress Plugin looks like it may do the job - but I'll have to pay for that. Which isn't great for the rest of the users on this blog.

  • Vladislav
    • Dead Eye Dev


    If I understood correctly what you're trying to do from the initial post, we're actually working on a very similar add-on for Maps, but for BuddyPress members. The add-on should be released soon, with the new plugin version, but in the meanwhile I'll try to share some of my experiences in dealing with that, which will hopefully help you with what you're trying to achieve.

    Google Maps comes with code for transforming a) addresses and b) latitude/longitude pairs to map markers, so the first thing you have to know is the profile field that holds the address (or lat/lon coordinates) of your users (from now on, I'm assuming we're dealing with the address field).

    So, for each user you have you'll want to convert his address to a map marker. This can be done with AgmMapModel method named "_address_to_marker", like so:

    $address = "New York"; // or whatever
    $model = new AgmMapModel;
    $marker = $model->_address_to_marker($address);

    Note, this method will call Google's geocoding API which could be very time consuming if you're doing repeatedly, so you'll want to cache the results, perhaps into usermeta.

    Once that is done, you'll have a proper map marker for each of your users. That's pretty much all the important data we need to set up a map. The map is essentially an associative array, with "markers" (a list of markers that should be displayed) being the most important key. There are some other keys that need to be populated, but getting them is easy, e.g.:

    $model = new AgmMapModel;
    $map = $model->get_map_defaults(); // Or you can populate the fields manually
    $map['defaults'] = $model->get_map_defaults();
    $map['id'] = md5(time() . rand());
    $map['markers'] = $markers; // $markers is a list of your users' map markers
    $map['show_map'] = 1;
    $map['show_markers'] = 0;
    // ...

    That's pretty much it - all that's left is to actually output the map in a way the front end javascript understands so it can pick it up. You can do that easily by instantiating AgmMarkerReplacer and calling its "create_tag" method with your map as argument:

    $codec = new AgmMarkerReplacer;
    echo $codec->create_tag($map);

    Note, this will create a new dynamic map - it won't be saved to the maps table in the database, but that's probably better for a dynamic user map.

    Lastly, to actually use all that you can create a template tag and even bind your own shortcode. Once the new add-on is released I hope all this will be much clearer but hopefully this can get you started.

  • jaseyb
    • WPMU DEV Initiate

    Thankyou!! You solve one set of issues and along comes the next. So the most sensible way of doing this is to create and attach a map object to users meta when a user updates their x-profile-field (in my case 'location 1').
    I'm having difficulty creating a hook and checking to see if the field has been updated. I've created a separate forum thread as it would be useful to someone else. I will post all the code here when done!

    Question: How do I create a custom hook to check updated profile data - link here

  • Vladislav
    • Dead Eye Dev


    Actually, you can take the easy way out and regenerate user location meta in cache on every profile save. It'll make a few requests more, but the checking will be much easier. So, you don't really need to check if the field's value actually changed - all you need to know is that an user saved changes to his profile.

    In this scenario, you'd be clearing and regenerating the usermeta cache for an user on profile save. You could do this by e.g. hooking to "xprofile_data_after_save":

    function my_handler_function ($xprofile) {
    	// ...
    	// Suppose that $my_field_id holds the field ID of the field you wish to check
    	if ($xfield->field_id != $my_field_id) return false;
    	// ...
    	// ...
    	// Clear the usermeta cache
    	update_user_meta($xfield->user_id, 'my_usermeta_cache_key', '');
    	// ...
    	// Regenerate cache and update the usermeta table
    	// ...
    add_action('xprofile_data_after_save', 'my_handler_function');

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.