Reputation: 9705
Is there a contributed / core module to detect the physical location of the user (based on IP may be?).
What I want to do eventually is: 1. Detect User location 2. Calculate distance from user's current location to location data attached to nodes (my nodes have location data) 3. Filter results (i.e. show selective nodes) based on distance
Any direction will be helpful.
I'm using D7.
Upvotes: 0
Views: 4113
Reputation: 146
The module you are looking for is IP geolocation views https://drupal.org/project/ip_geoloc You might also want to check out this question and answer: https://drupal.stackexchange.com/questions/54873/geofield-proximity-in-views
Upvotes: 0
Reputation: 857
The Smart IP module will convert the user's IP address into latitude and longitude coordinates. The following function will return the spherical distance in miles between two pairs of coordinates.
function _get_spherical_distance($s_pd, $s_ld, $f_pd, $f_ld) {
$s_pr = deg2rad($s_pd);
$f_pr = deg2rad($f_pd);
$lr = deg2rad($s_ld - $f_ld);
return 3960 * acos(sin($s_pr) * sin($f_pr) + cos($s_pr) * cos($f_pr) * cos($lr));
}
E.g. Atlanta, GA (33.7489, -84.3879) to Redmond, WA (47.6739, -122.1214):
print _get_spherical_distance(33.7489, -84.3879, 47.6739, -122.1214);
Outputs:
2170.4293537864
That's 2,170 miles. If you need to convert an address into latitude and longitude coordinates, you can use the Google Geocoding API.
$address_string = "1600 Pennsylvania Avenue, Washington, DC 20500";
$geo_html = file_get_contents('http://maps.google.com/maps/geo?sensor=false&oe=utf8&output=csv&q='. urlencode($address_string));
print_r($geo_html);
Outputs:
200,8,38.8987149,-77.0376555
This is a comma-separated string of four values. The first is an HTTP status code. You should see 200. The second value is an approximation of the conversion accuracy. You'll probably want an accuracy value of at least 6 if you're going to filter content based on a user's distance from an address. The third and fourth values are, obviously, the latitude and longitude.
Upvotes: 1
Reputation: 27023
1- You can retrieve the use location using jQuery and this awesome script.
Example of usage:
jQuery.getScript('http://www.geoplugin.net/javascript.gp', function()
{
$('#results').html("Your location is: " + geoplugin_countryName() + ",
" + geoplugin_region() + ", " + geoplugin_city());
});
2- You can calculate the distance between the user location and another location knowing each location latitude and longitude.
Example:
jQuery.getScript('http://www.geoplugin.net/javascript.gp', function()
{
var source_longitude = 30.049999;
var source_latitude = 31.250000;
var user_longitude = geoplugin_longitude();
var user_latitude = geoplugin_latitude();
var earth_radius = 6371; // in Kilo Meters
var dLat = (source_latitude - user_latitude).toRad();
var dLon = (source_longitude - user_longitude).toRad();
source_latitude = source_latitude.toRad();
user_latitude = user_latitude.toRad();
var x = Math.sin(dLat/2) * Math.sin(dLat/2) + Math.sin(dLon/2) * Math.sin(dLon/2) * Math.cos(source_latitude ) * Math.cos(user_latitude );
var y = 2 * Math.atan2(Math.sqrt(x), Math.sqrt(1 - x));
var distance = earth_radius * y;
});
3- To filter the results, you can use the distance and hook_node_access() to change the node view permission based on the distance.
Hope this helps... Muhammad.
Upvotes: 1
Reputation: 36956
There are two modules (that I know of) offering the IP - location detection for Drupal 7:
I think the Location module will be your next stop after that:
At present, it is the only module that provides the views integration necessary for implementing location-based searches and is designed for the purpose of finding points on a map within a vicinity of a user-supplied location.
Upvotes: 1