0

Here, I have a tough one : I have a wordpress running with two plugins : - Buddypress - Wordpress Accurate Geolocation (store the longitude, latitude, altitude, time of position and id of the user in the database).

Now, on my member directory page, I am displaying the users of the website. However, I would like to show the users of the website that are around the one looking at that page.

Each time we access this page, for your information, the location of the viewer is loaded and stored in the database too.

The plugins stores : - Last location in wp_usermeta - All location visited in wp_options - Current location in cookies

Here is a value of a random location in the database of a tester :

a:13:{s:8:"latitude";s:10:"40.6576022";s:9:"longitude";s:18:"-73.58318349999999";s:8:"altitude";s:1:"0";s:8:"accuracy";s:4:"2859";s:16:"altitudeAccuracy";s:1:"0";s:7:"heading";s:3:"NaN";s:5:"speed";s:3:"NaN";s:10:"error_code";s:0:"";s:13:"error_message";s:0:"";s:8:"php_time";i:1449777359;s:8:"php_date";s:19:"2015-12-10 19:55:59";s:15:"php_date_format";s:11:"Y-m-d H:i:s";s:7:"user_id";i:4;}

I would like to to be able to display the users around the user looking at the page when he access the page. So basically : Show the IDs having a location at 100 meters (radius) to the ID's location of the viewer.

However, I don't know how to go grab anything in the database.

Here is the code of the page that displays the members, normally :

<?php

/**
 * BuddyPress - Members Loop
 *
 * Querystring is set via AJAX in _inc/ajax.php - bp_legacy_theme_object_filter()
 *
 * @package Boss
 * @subpackage bp-legacy
 */

?>

<?php do_action( 'bp_before_members_loop' ); ?>

<?php if ( bp_has_members( bp_ajax_querystring( 'members' ) ) ) : ?>

    <div id="pag-top" class="pagination">

        <div class="pag-count" id="member-dir-count-top">

            <?php bp_members_pagination_count(); ?>

        </div>

        <div class="pagination-links" id="member-dir-pag-top">

            <?php bp_members_pagination_links(); ?>

        </div>

    </div>

    <?php do_action( 'bp_before_directory_members_list' ); ?>

    <ul id="members-list" class="item-list" role="main">

    <?php while ( bp_members() ) : bp_the_member(); ?>

        <li>
            <div class="item-avatar">
                <a href="<?php bp_member_permalink(); ?>"><?php bp_member_avatar('type=full&width=70&height=70'); ?></a>
            </div>

            <div class="item">
                <div class="item-title">
                    <a href="<?php bp_member_permalink(); ?>"><?php bp_member_name(); ?></a>
                </div>

                <?php
                $showing = null;
                //if bp-followers activated then show it.
                if(function_exists("bp_follow_add_follow_button")) {
                    $showing = "follows";
                    $followers  = bp_follow_total_follow_counts(array("user_id"=>bp_displayed_user_id()));
                } elseif (function_exists("bp_add_friend_button")) {
                    $showing = "friends";
                }

                ?>

                <div class="item-meta">
                    <div class="activity">
                        <?php bp_member_last_active(); ?>
                    </div>

                    <?php if($showing == "friends"): ?>
                    <span class="count"><?php echo friends_get_total_friend_count(bp_get_member_user_id()); ?></span>
                        <?php if ( friends_get_total_friend_count(bp_get_member_user_id()) > 1 ) { ?>
                            <span><?php _e("Friends","boss"); ?></span>
                        <?php } else { ?>
                            <span><?php _e("Friend","boss"); ?></span>
                        <?php } ?>
                    <?php endif; ?>

                    <?php if($showing == "follows"): ?>
                    <span class="count"><?php $followers = bp_follow_total_follow_counts(array("user_id"=>bp_get_member_user_id())); echo $followers["followers"]; ?></span><span><?php _e("Followers","boss"); ?></span>
                    <?php endif; ?>
                </div>

                <div class="item-desc">
                    <p>
                        <?php if ( bp_get_member_latest_update() ) : ?>
                            <?php bp_member_latest_update( array( 'view_link' => true ) ); ?>
                        <?php endif; ?>
                    </p>
                </div>

                <?php do_action( 'bp_directory_members_item' ); ?>

                <?php
                 /***
                  * If you want to show specific profile fields here you can,
                  * but it'll add an extra query for each member in the loop
                  * (only one regardless of the number of fields you show):
                  *
                  * bp_member_profile_data( 'field=the field name' );
                  */
                ?>
            </div>

            <div class="action">
                <div class="action-wrap">
                    <?php do_action( 'bp_directory_members_actions' ); ?>
                </div>
            </div>

            <div class="clear"></div>
        </li>

    <?php endwhile; ?>

    </ul>

    <?php do_action( 'bp_after_directory_members_list' ); ?>

    <?php bp_member_hidden_fields(); ?>

    <div id="pag-bottom" class="pagination">

        <div class="pag-count" id="member-dir-count-bottom">

            <?php bp_members_pagination_count(); ?>

        </div>

        <div class="pagination-links" id="member-dir-pag-bottom">

            <?php bp_members_pagination_links(); ?>

        </div>

    </div>

<?php else: ?>

    <div id="message" class="info">
        <p><?php _e( "Sorry, no members were found.", 'boss' ); ?></p>
    </div>

<?php endif; ?>

<?php do_action( 'bp_after_members_loop' ); ?>

Thank you for your help !

Best regards

FrenchyNYC
  • 337
  • 1
  • 6
  • 23

1 Answers1

1

You need to get their coordinates and compare them with each user using this answer

Replace <?php while ( bp_members() ) : bp_the_member(); ?> with:

<?php 
$current_user = wp_get_current_user();
$user_location = get_user_meta($current_user->ID, 'agl_data', true);
$user_lat = floatval($user_location['latitude']); //right now it's a string, you need a float/double.
$user_lng = floatval($user_location['longitude']); //right now it's a string, you need a float/double.
while ( bp_members() ) : bp_the_member();
    $bp_location = get_user_meta(bp_get_member_user_id(), 'agl_data', true);
    $bp_member_lat = floatval($bp_location['latitude']);
    $bp_member_lng = floatval($bp_location['longitude']);

    $distance_in_meters = vincentyGreatCircleDistance($user_lat , $user_lng , $bp_member_lat, $bp_member_lng);
    if($distance_in_meters < 100){
 ?>

Then replace your <?php endwhile; ?> with:

<?php } ?>
<?php endwhile; ?>
Community
  • 1
  • 1
Antony Thompson
  • 1,608
  • 1
  • 13
  • 22
  • Thank you very much, could you show me how you integrate it in the page I posted as a code ? I don't undertsand what to put in "location_metakey_goes_here" ? I gave an example of how it looks like in the database, but maybe I don't get it ! Thank you so much for your time – FrenchyNYC Dec 10 '15 at 20:49
  • Thank you for your fast answer and great explanations again ! I updated my file, so far there is no PHP error or else. However it doesn't show anybody in the list. I am testing two accounts that show up obviously at the same address (my house) Maybe it is because it doesn't know what table going to pick up the information of the meta key ? What do you thinK ? THANKS ! – FrenchyNYC Dec 10 '15 at 21:10
  • Yes if your not sure what the meta key is, you will definitely need to find that out first. In phpmyadmin search wp_usermeta user_id with the user. Then find the row with the location data and use that key. Also maybe add `print_r($bp_location); print_r($location); print_r($distance_in_meters);` just before the if statement to check you have the correct information – Antony Thompson Dec 10 '15 at 21:20
  • I have the metakey, I found it, except the meta key I have nothing else to change in your code right ? The table doesn't have the prefix "wp" but "franto", does it change anything ? – FrenchyNYC Dec 10 '15 at 21:23
  • Sorry for the double post, in fact I think it just doesn't work because the file in which I replace this is included in the page's template and it seems that the "frame" where the code goes is just displaying nothing. I tried the print and it doesn't show up on screen and when i just insert some code, it doesn't show anything either. So maybe the whole block is just not working... – FrenchyNYC Dec 10 '15 at 21:41
  • Oh I see, yes you will need to find the correct page template to put this in then :-) – Antony Thompson Dec 10 '15 at 21:44
  • Well, my main page is calling for the file "members-loop.php" where the code I showed you is appearing. So I paste it there. By doing so, it removes the normal content of the users of the website, without destroying the page. So I guess it is the right place to put it, but I feel like it is doing an error without showing the error... – FrenchyNYC Dec 10 '15 at 21:49
  • Ho ! It shows an error in the error log " mod_fcgid: stderr: PHP Parse error: syntax error, unexpected ';' in ... line 41" The line is empty right after the $current_user->ID, 'agl_data', true; – FrenchyNYC Dec 10 '15 at 21:54
  • ahh I see change to `'agl_data', true);` I also updated answer to fix the errors. That's what happens when I code without testing :-P – Antony Thompson Dec 10 '15 at 22:13
  • Yes, I changed it there and on line 43 and now the error is : PHP Fatal error: Call to undefined function haversineGreatCircleDistance() Thanks for the help ! Almost there !!! (I don't know how to solve this) – FrenchyNYC Dec 10 '15 at 22:15
  • you need to copy that function from the other answer in your functions.php file (http://stackoverflow.com/questions/10053358/measuring-the-distance-between-two-coordinates-in-php) – Antony Thompson Dec 10 '15 at 22:16
  • I copied the "edit" version of the solving answer of the post you showed me in my function.php file of my theme but it creates an error 500 now... Should I change the way I paste it ? Or change elements in it maybe ? I just copied / Paste – FrenchyNYC Dec 10 '15 at 22:31
  • if you want to use the edit version make sure your reference the correct function. `vincentyGreatCircleDistance()` not `haversineGreatCircleDistance()`. There could be many reasons you have an error 500. A straight copy and paste should be fine. – Antony Thompson Dec 10 '15 at 23:00
  • I used the original version, it now displays users, however it seems to show it wherever they are... Which is strange. I can't do a test with somebody that would live somewhere else anyway.. I will mark this a solved because I am using your time enough and at the end it displays something regarding the long and lat. Thank you very much – FrenchyNYC Dec 10 '15 at 23:05
  • I am just bothering you a few again, I am trying... It seems to display people only when the line "if($distance_in_meters < 100){" is changed to < 1000000 or > 1000000... Only in these two cases it shows the people... Otherwise it display the "print" command you asked me to show up but no members... Any idea ? – FrenchyNYC Dec 10 '15 at 23:56
  • It could be because the accuracy of the location isn't good enough. If that's the case then the distance could be really big in which case there isn't much you can do. You could try doing this: http://www.labnol.org/internet/geo-location/27878/ just to test it's working correctly. If you're unsure how to get lat long coordinates use this: http://www.latlong.net/ – Antony Thompson Dec 11 '15 at 00:23
  • if http://www.labnol.org/internet/geo-location/27878/ doesn't work. You could also just change the coordinates in the database, find the coords to use with latlong.net – Antony Thompson Dec 11 '15 at 00:25
  • Thanks, I just checked my actual coordinates and it matches what is in the database, it points exactly where I live. So I don't see it coming from here... However, it seems to have an error in the log : "deg2rad() expects parameter 1 to be double", any thoughts ? – FrenchyNYC Dec 11 '15 at 00:36
  • HA ! I found out that even if the two are at the same place, the functions thinks they are 8635296.7917067 meters apart... The coordinates are perfect but the result of the distance between both is pretty... inaccurate ! Here is the result : Array ( [latitude] => 40.5863735 [longitude] => -73.6542188 [altitude] => NaN [accuracy] => 50 [altitudeAccuracy] => NaN [heading] => NaN [speed] => NaN [error_code] => [error_message] => [php_time] => 1449792477 [php_date] => 2015-12-11 00:07:57 [php_date_format] => Y-m-d H:i:s [user_id] => 1 ) 8635296.7917067 – FrenchyNYC Dec 11 '15 at 01:13
  • Tell me if I am wrong but it looks like : 1) the code you wrote is not taking the coordinates of the user to refer to (the second user) and/or 2) at a moment it says that "$bp_member_lat = $bp_location['latitude'];" so isn't it doubling the numbers since it is the same points to send to the function ? I don't know how to solve it though... I am so desperate ! Thank you for your time ! – FrenchyNYC Dec 11 '15 at 02:44
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/97799/discussion-between-frenchynyc-and-antony-thompson). – FrenchyNYC Dec 14 '15 at 02:25
  • Code in the answer has been updated and confirmed working. – Antony Thompson Dec 16 '15 at 00:11