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:
PHP code
/** * 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.
$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));
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.
PHP code
/** * 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.
// 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));
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 .
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:
/** * 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.
// 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));
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?
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 .
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.
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 .
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.
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 .