Use multiple fields to set marker location of members

I would like to use multiple fields to accurately display each member's location. I am currently using City, State/Region, and Country on sign up.

  • Vladislav

    Hi,

    Thanks! The attached update allows for using multiple profile fields in address creation. There are two ways to make it work, depending on how complex your fields are and how much processing you want applied.

    The simplest way would be to just list your fields as a comma-separated string in a define statement in your wp-config.php. So, for an example, if your fields are called "City", "Country" and "Street Address", you could add a line like this:

    define('AGM_BP_PROFILE_MAP_USE_ADDRESS_FIELDS', 'Country,City,Street Address', true);

    Please, note that the ordering of the fields in the statement matters - this is the order in which the fields will be processed and used in geocoding request.

    Another way is to hook up to the address filter and allow your processing function to process the fields for you. E.g.:

    function agm_multiple_bp_profile_fields ($old_address, $user_id) {
    	$country = bp_get_profile_field_data(array(
    		'field' => 'Country', // Field name or ID
    		'user_id' => $user_id,
    	));
    	$city = bp_get_profile_field_data(array(
    		'field' => 'City', // Field name or ID
    		'user_id' => $user_id,
    	));
    	$street = bp_get_profile_field_data(array(
    		'field' => 'Street Address', // Field name or ID
    		'user_id' => $user_id,
    	));
    	$address = sprintf("%s %s, %s", $street, $city, $country);
    	return $address;
    }
    add_filter("agm_google_maps-bp_profile_map-user_address", "agm_multiple_bp_profile_fields", 10, 2);

    One last caveat - please, note that the location requests are cached for performance reasons. The cache is reset and refreshed when an user saves his profile, so you'll possibly want to keep resaving your profile while you're tuning all of this in. Once you're satisfied with how it works, you can just enable the "Fix and Repair" add-on in plugin settings, and run the action titled "Rebuild BuddyPress profile maps" - this will walk through your members and reset their location cache for you.

  • tfullen

    Vlad,

    This worked great! Thank you for your help and especially your quick response. I have a couple more questions that I can't find an answer to...

    1. I would like to hide admin accounts from showing on the map. Any idea how I can do that?

    2. There seems to be a glitch in the popups. If you click on a marker the popup appears, but if you don't close it and click on another marker, the first popup doesn't go away. I've looked at other google maps, and they seem to work as expected concerning this. Any guidance would be appreciated!

    Thanks again for all your help. You can see it in action here: http://theglobalgamechangers.com/members

    Tony

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.