HTML Logo by World Wide Web Consortium (www.w3.org). Click to learn more about our commitment to accessibility and standards.

Moving forward with Composr

ocPortal has been relaunched as Composr CMS, which is now in beta. ocPortal 9 will be superseded by Composr 10.

Head over to compo.sr for our new site, and to our migration roadmap. Existing ocPortal member accounts have been mirrored.


Record users lat/long when they join

Login / Search

 [ Join | More ]
 Add topic 
Posted
Rating:
#68161 (In Topic #14497)
Avatar

Community saint

Hi everyone,

Thought that I'd share some code that might be of interest to some of you.

I'm using the ocUsermap addon to show the locations of my users, but in order for the location to appear on map I first need to get the users to manually record their latitude/longitude.

Rather then relying on just the users, I've put together some code to give all new members a default latitude/longitude based on the country they are connecting from. In addition to using ocUsermap to display the users location, this code requires that the "stats" module that comes with ocPortal is installed in order to derive the users latitude/longitude from their IP address.

Insert the following code in your "/pages/modules_custom/join.php" file just before the "// Send confirm mail" comment:

Code (php)


                /**
                 * Add default latitude/longitude infromation to custom profile fields of new accounts.
                 *
                 * Use the users IP address to determin which country they are connecting from, and then Google maps API v3 to resolve to latitude/longitude.
                */

                if((file_exists(get_file_base().'/sources_custom/blocks/main_google_map_users.php')) && (addon_installed('stats'))) {  // Only proceed if ocUsermap and stats addons are installed.
                        $lat_field_name='ocp_latitude';         // Custom profile field name to store latitude in.
                        $lng_field_name='ocp_longitude';        // Custom profile field name to store longitude in.
       
                        $address = geolocate_ip();
                        define ( "MAPS_HOST", "maps.google.com" );
                        $base_url = "http://" . MAPS_HOST . "/maps/api/geocode/xml";
                       
                        $request_url = $base_url . "?address=" . urlencode($address) ."&sensor=false";
                        if($xml = simplexml_load_file($request_url)) {
                                if ($xml->status == "OK") {
                                        $lat = $xml->result->geometry->location->lat;
                                        $lng = $xml->result->geometry->location->lng;
                                   
                                        $latitude_cpf_id=$GLOBALS['FORUM_DB']->query_value_null_ok('f_custom_fields f LEFT JOIN '.$GLOBALS['FORUM_DB']->get_table_prefix().'translate t ON f.cf_name=t.id','f.id',array('text_original'=>$lat_field_name));
                                        $longitude_cpf_id=$GLOBALS['FORUM_DB']->query_value_null_ok('f_custom_fields f LEFT JOIN '.$GLOBALS['FORUM_DB']->get_table_prefix().'translate t ON f.cf_name=t.id','f.id',array('text_original'=>$lng_field_name));
               
                                        $GLOBALS['FORUM_DB']->query_update('f_member_custom_fields',array('field_'.$latitude_cpf_id=>$lat,'field_'.$longitude_cpf_id=>$lng ),array('mf_member_id'=>$member_id),'',1);
                                }
                        }      
                }
 

Hope this is of use to some of you out there.


Last edit: by temp1024

Do you have a Samsung Galaxy S / Galaxy S II ? If so, why not check out my ScreenFree FM Radio .
Back to the top
 
Posted
Rating:
#68217
Avatar

Community saint

I just made a minor tweak so that no two default locations are the same. Now members will circle their country centre in a series of concentric circles.

Code (php)

                /**
                 * Add default latitude/longitude infromation to custom profile fields of new accounts.
                 *
                 * Use the users IP address to determin which country they are connecting from, and then Google maps API v3 to resolve to latitude/longitude.
                */

                if((file_exists(get_file_base().'/sources_custom/blocks/main_google_map_users.php')) && (addon_installed('stats'))) {  // Only proceed if ocUsermap and stats addons are installed.
                        $lat_field_name='ocp_latitude';         // Custom profile field name to store latitude in.
                        $lng_field_name='ocp_longitude';        // Custom profile field name to store longitude in.
       
                        $address = geolocate_ip();
                        define ( "MAPS_HOST", "maps.google.com" );
                        $base_url = "http://" . MAPS_HOST . "/maps/api/geocode/xml";
                       
                        $request_url = $base_url . "?address=" . urlencode($address) ."&sensor=false";
                        if($xml = simplexml_load_file($request_url)) {
                                if ($xml->status == "OK") {
                                        $lat = $xml->result->geometry->location->lat;
                                        $lng = $xml->result->geometry->location->lng;
                                       
                                        // Adjust the lat/long so that no two members default to the exact same location. Members will circle their country centre in a series of concentric circles.
                                        $radius=((int)($member_id/360))*.0001 +.001;
                                        $lat=$lat+$radius*sin($member_id*2*M_PI/360);
                                        $lng=$lng+$radius*cos($member_id*2*M_PI/360);

                                        $latitude_cpf_id=$GLOBALS['FORUM_DB']->query_value_null_ok('f_custom_fields f LEFT JOIN '.$GLOBALS['FORUM_DB']->get_table_prefix().'translate t ON f.cf_name=t.id','f.id',array('text_original'=>$lat_field_name));
                                        $longitude_cpf_id=$GLOBALS['FORUM_DB']->query_value_null_ok('f_custom_fields f LEFT JOIN '.$GLOBALS['FORUM_DB']->get_table_prefix().'translate t ON f.cf_name=t.id','f.id',array('text_original'=>$lng_field_name));
               
                                        $GLOBALS['FORUM_DB']->query_update('f_member_custom_fields',array('field_'.$latitude_cpf_id=>$lat,'field_'.$longitude_cpf_id=>$lng ),array('mf_member_id'=>$member_id),'',1);
                                }
                        }      
                }
 


Last edit: by temp1024

Do you have a Samsung Galaxy S / Galaxy S II ? If so, why not check out my ScreenFree FM Radio .
Back to the top
 
Posted
Rating:
#70570
Avatar

Community saint

temp-

I'm getting ready to install ocUsermap and then make your mod.

I just wanted to confirm that your installation went smoothly and that CPFs were created for lat and long.

Did you have any issues?

Bob
Back to the top
 
Posted
Rating:
#70572
Avatar

Community saint

It was quite a while ago, but I don't recall having any problems.

I was a bit confused about CPF's where they are accessed etc. but no install problems as such that I can recall.

Do you have a Samsung Galaxy S / Galaxy S II ? If so, why not check out my ScreenFree FM Radio .
Back to the top
 
Posted
Rating:
#70574
Avatar

Community saint

Thanks, temp.

Gonna push the button on a test site and see if anything untoward happens.

Got to be gentle on my non-live but real site as I have just managed to get it mostly where I want it. Just backing it up as we speak.

Bob
Back to the top
 
Posted
Rating:
#70577
Avatar

Community saint

That's a wise thing to do. I cloned my production site just before it went live and use that for testing. As it is an exact clone, and not just any other ocPortal installation I will instantly see what does and does not play nice with my exact configuration.

Do you have a Samsung Galaxy S / Galaxy S II ? If so, why not check out my ScreenFree FM Radio .
Back to the top
 
Posted
Rating:
#70629
Avatar

Community saint

Hey temp-

I tried installing your mod on my test site but it doesn't seem to make an appearance. The long/lat fields are being populated but I thin that is just the ocUsermap working. There is no evidence of your refining location mod. Here's the code from the relevant section of join.php:

Code

// Add member
      $skip_confirm=(get_option('skip_email_confirm_join')=='1');
      $validated_email_confirm_code=$skip_confirm?'':strval(mt_rand(1,32000));
      $require_new_member_validation=get_option('require_new_member_validation')=='1';
      $coppa=(get_option('is_on_coppa')=='1') && (servertime_to_usertime(time()-mktime(0,0,0,$dob_month,$dob_day,$dob_year))/31536000.0<13.0);
      $validated=($require_new_member_validation || $coppa)?0:1;
      if (is_null($member_id)) $member_id=ocf_make_member($username,$password,$email_address,$groups,$dob_day,$dob_month,$dob_year,$actual_custom_fields,$timezone,($additional_group!=-1)?$additional_group:NULL,$validated,time(),time(),'',NULL,'',0,(get_option('default_preview_guests')=='1')?1:0,$reveal_age,'','','',1,1,$language,$allow_emails,'',get_ip_address(),$validated_email_confirm_code,true,'','');
      /*if ($additional_group!=-1)
      {
         require_code('ocf_groups_action');
         require_code('ocf_groups_action2');
         ocf_add_member_to_group($member_id,$additional_group,1);
      }*/
      
       /**
       * Add default latitude/longitude infromation to custom profile fields of new accounts.
       *
       * Use the users IP address to determin which country they are connecting from, and then Google maps API v3 to resolve to latitude/longitude.
       */
         if((file_exists(get_file_base().'/sources_custom/blocks/main_google_map_users.php')) && (addon_installed('stats'))) {  // Only proceed if ocUsermap and stats addons are installed.
         $lat_field_name='ocp_latitude';      // Custom profile field name to store latitude in.
         $lng_field_name='ocp_longitude';   // Custom profile field name to store longitude in.
   
         $address = geolocate_ip();
         define ( "MAPS_HOST", "maps.google.com" );
         $base_url = "http://" . MAPS_HOST . "/maps/api/geocode/xml";
         
         $request_url = $base_url . "?address=" . urlencode($address) ."&sensor=false";
         if($xml = simplexml_load_file($request_url)) {
            if ($xml->status == "OK") {
               $lat = $xml->result->geometry->location->lat;
               $lng = $xml->result->geometry->location->lng;
               
               // Adjust the lat/long so that no two members default to the exact same location. Members will circle their country centre in a series of concentric circles.
               $radius=((int)($member_id/360))*.0001 +.001;
               $lat=$lat+$radius*sin($member_id*2*M_PI/360);
               $lng=$lng+$radius*cos($member_id*2*M_PI/360);

               $latitude_cpf_id=$GLOBALS['FORUM_DB']->query_value_null_ok('f_custom_fields f LEFT JOIN '.$GLOBALS['FORUM_DB']->get_table_prefix().'translate t ON f.cf_name=t.id','f.id',array('text_original'=>$lat_field_name));
               $longitude_cpf_id=$GLOBALS['FORUM_DB']->query_value_null_ok('f_custom_fields f LEFT JOIN '.$GLOBALS['FORUM_DB']->get_table_prefix().'translate t ON f.cf_name=t.id','f.id',array('text_original'=>$lng_field_name));
      
               $GLOBALS['FORUM_DB']->query_update('f_member_custom_fields',array('field_'.$latitude_cpf_id=>$lat,'field_'.$longitude_cpf_id=>$lng ),array('mf_member_id'=>$member_id),'',1);
            }
         }   
      }
      
      
   

      // Send confirm mail
      if (!$skip_confirm)
      {

Do I need to register at Google or something?

Thanks for your help.

Bob
Back to the top
 
Posted
Rating:
#70638
Avatar

Community saint

Yes, you need to get a fee Google maps API key from here: Sign Up for the Google Maps API - Google Maps API Family - Google Code .

Do you have a Samsung Galaxy S / Galaxy S II ? If so, why not check out my ScreenFree FM Radio .
Back to the top
 
Posted
Rating:
#70640
Avatar

Community saint

Okay, I got an API key but where do I plug it in?

Bob
Back to the top
 
Posted
Rating:
#70642
Avatar

Community saint

You can add it at the end of your block decleration, for example:

Code

{$BLOCK,block=main_google_map_users,...,api_key=1111111}

Do you have a Samsung Galaxy S / Galaxy S II ? If so, why not check out my ScreenFree FM Radio .
Back to the top
 
Posted
Rating:
#70643
Avatar

Community saint

Perhaps I misunderstood your mod. I thought it provided additional capabilities to set your location when you join. Do you need to have an active map block for this to work?

Thanks for your help.

Bob
Back to the top
 
Posted
Rating:
#70644
Avatar

Community saint

You don't need an active map to display it. When you said "Do I need to register at Google or something?" I though that the underlying problem was actually that you just could not see the map (as you also said that long/lat fields are being populated), which you would not be able to do without the API key.

If you look as the lat/longs of the accounts you created after using my tweak you should notice at least a 0.00001 difference in the lat and/or long field between users. If you get that then it means that its working.

If you don't see those subtle differences then I think the problem might be that the stats module that comes with ocPortal may not be installed. I'll edit the first post to make that dependency clear.

Do you have a Samsung Galaxy S / Galaxy S II ? If so, why not check out my ScreenFree FM Radio .
Back to the top
 
Posted
Rating:
#70645
Avatar

Community saint

I was just being dense.

When I first installed ocUsermap (and ocDatamap), the fields weren't populating. I realized that I needed to enable the GeoIP for it to work and the fields then populated.

After installing your code, I was expecting to see some change in interface based on this:
I just made a minor tweak so that no two default locations are the same. Now members will circle their country centre in a series of concentric circles.

I'm getting long and lat data, I guess that is all that really matters. I was planning on introducing the map to the users after I have had some time to collect data. The goal is to give users in the same area the opportunity to set up their own meet-and-greets to discuss/buy/sell the artist's work. I know there are a number of large clusters of collectors.

Once again, thanks for your help.

Bob


Back to the top
 
Posted
Rating:
#70649
Avatar

Community saint

Essentially this is what its doing:
Each pin would represent a member that joined from the same country.

You can see a very old prototype interactive demo here GeoCoding Script . The points in the demo are much denser then the code pasted here, which only have 360 points per circle.

When I get some more time down the track I will be implementing something more like this MarkerClusterer Example which has counts and groupings.


Do you have a Samsung Galaxy S / Galaxy S II ? If so, why not check out my ScreenFree FM Radio .
Back to the top
 
Posted
Rating:
#70650
Avatar

Community saint

Thanks for the explanation - things are much clearer now.

Looking forward to the MarkerClusterer system.

Bob
Back to the top
 
Posted
Rating:
#71357
Avatar

Community saint

Temp-

Something seems to be amiss with your code. When creating a new member using my IP address (Las Vegas, NV), the lat and long assigned are 37.00022 and -94.99903 which is over a thousand miles away from what other IP Geolocators are calculating.

Additionally, the timezone is being set as GMT-7 instead of GMT-8. The timezone was set correctly when I commented out your code.

Any thoughts?

Bob
Back to the top
 
Posted
Rating:
#71373
Avatar

Community saint

Thanks for the feedback Bob.

The lat/long are working as expected and as advertised, that is, a country based location and not city based.

I use the geolocate_ip() function (part of the ocPortal stats module) to return an ISO country code. I use that as the address that I send to google to return a lat/long.

If you know of free service/API I can plug into for city level location please let me know and I will see about integrating it. For me, country level was/is enough as I just want to get a feel for where people are coming from. City would be nice though as there are some very big countries out there. I wouldn't want to go below city level for privacy reasons.

As for time zone, I don't touch that at all. So that behaviour is very strange.

Do you have a Samsung Galaxy S / Galaxy S II ? If so, why not check out my ScreenFree FM Radio .
Back to the top
 
Posted
Rating:
#71375
Avatar

Community saint

I just tried joining with and without my code and timezone is wrong in both cases. Like for you, I'm loosing 1 hr, going from +10 to +9.

I'm using v7.01. Looks like I have to wait for v7.1.1 to get it fixed: View topic: "Enable regionalisation" setting - ocPortal

Do you have a Samsung Galaxy S / Galaxy S II ? If so, why not check out my ScreenFree FM Radio .
Back to the top
 
Posted
Rating:
#71378
Avatar

Community saint

I'm at 7.1 and having the problem with the time zones.

Maxmind offers GeoLite City (MaxMind - GeoLite City | Free Geolocation Database) for free.

Bob
Back to the top
 
Posted
Rating:
#71382
Avatar

Community saint

I might have a play with GeoLite City over the weekend.

Do you have a Samsung Galaxy S / Galaxy S II ? If so, why not check out my ScreenFree FM Radio .
Back to the top
 
There are too many online users to list.
Control functions:

Quick reply   Expand