The distance query works well, but I wanted to get the visitor's location from their IP address. Instead of writing a views_query_alter or something, I thought it would be nice to have this functionality within location itself. So, I modified the filter handler for proximity, and got it to support the geoip api module with a checkbox. A patch is attached - no simpletests yet but I hope it's useful anyhow.

The only thing that I wanted to get working but didn't was a way to expose the zipcode filter but still have the map come up with the user's location by default.

Oh, also there's some stuff you have to do to make the City-level database work with geoip module - described at http://drupal.org/node/813228#comment-3057436

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

jkdaza’s picture

This is great! Thanks.

Would it be too much of a problem adding the same feature to the "Sort Criteria"?

dnotes’s picture

If you mean sorting by proximity to a visitor's location, you should already be able to do that: add a "Location: Distance / Proximity" sort, and on the extra settings screen (the gear on the right side) select "Use Distance / Proximity filter". According to the documentation, sorts were kind of touchy to set up, but I didn't experience any of the problems described.

SeanBannister’s picture

Awesome, I was just about to code this. I'll give you my review soon.

dnotes’s picture

Hey, it's a little off topic for location module alone, but I just rediscovered that if you're using a gmap view this patch alone will not center a map on the visitor's IP address - for that something else is needed. What I've done is included an id of "iploc_block" in the gmap macro for that view display (click on the gear to the right of gmap style, and in the gmap macro, between the brackets, enter " |id=iploc_block"). Then I include a hook_gmap somewhere in my module:

function ccd_loc_gmap($op, &$map) {
  if ($op == 'pre_theme_map') {
    if (stripos($map['id'], 'iploc_') !== FALSE && module_exists('geoip')) {
      if ($loc = geoip_city()) { // intentional assignment of $location
	$map['latitude'] = $loc->latitude;
        $map['longitude'] = $loc->longitude;
      }
    }
  }
}

-et viola, the map is centered on a visitor's ip address. This is a hack, but I'll bet there's a way this could get into location.module as well, and it would be *awesome*.

gamelodge’s picture

FileSize
1.35 KB

Hi Guys

I could not get the patch above to work on my install.
So I created a separate patch that does the job for me.

You need the GeoIP module for this to work.

All this does is add a an extra option to the views proximity filter to allow you to set the origin as the IP address as referenced from the GeoIP module.

dnotes’s picture

Title: Searching near a visitor's ip address, using the geoip api module » Proximity filter - Searching near a visitor's ip address, using the geoip api module
Version: 6.x-3.x-dev » 6.x-3.1

Updated patch against version 3.1 - nice job on the changes to proximity filter handler! I like the new options.

Oh, and this patch should be in the right format to apply with drush make this time.... =oops=

dnotes’s picture

right, and here's that patch.

dnotes’s picture

Wow, posted at exactly the same time... weird. The v3.1 patches above are nearly identical except that the latter should fall back to default coordinates if they were entered. I'm okay with either, but I prefer the latter functionality.

YesCT’s picture

gamelodge, up for a review of #7?

jusyjim’s picture

Davidhunt,
Regarding #4 above...
This is exactly what I need but which module did you put that code in? location module? I tried that and it didn't work using this gmap macro:
[gmap zoom=9 |id=iploc_block |width=99% |height=300px |control=Small |type=Map]

Thanks!

dnotes’s picture

Jusyjim,
You should be able to put the code from #4 in whatever custom module you are using; you just have to adjust the function name accordingly (my custom module was ccd_loc). I wouldn't put it in Location though, because it's better not to change code on contrib modules for custom functionality.

Cheers!
David

izmeez’s picture

subscribing

BeaPower’s picture

Im a newbie and dont understand how to apply the patch, help plz. How to create custom module?

shopdogg’s picture

subscribe

dnotes’s picture

BeaPower - I just noticed your post - for patches, the page is http://drupal.org/patch/apply, and for creating a custom module, you could start at http://drupal.org/developing/modules. You won't need everything in that handbook for the present task; all you'll really need is the .info file, the .module file, and an understanding of how to implement a hook. It's hard to know where to find this stuff when you're just starting, isn't it? I wonder if there's a documentation page for the documentation pages... :) As I recall, a while back I found a page that helped explain drupal.org a little, so you might click around for that. Good luck!

barry769’s picture

Is there any plan to get the exposing of the zip code filter to work? The functionality sounds good, but won't be very effective when the database returns a bad value.

deggertsen’s picture

I applied the patch in #7 successfully. It would be nice if this could be committed with some of the other things discussed in this thread. Subscribing.

dnotes’s picture

deggertsen, is there any way you could do a thorough review of this patch? it's really not appropriate to mark it as rtbc on my own.

deggertsen’s picture

I don't know if my review would be adequate. I'm not very familiar with geo coding and don't have time to familiarize myself with it. All I really know is that the patch applied correctly and works as far as I can tell at this point. I will likely be doing more testing with it later but the site I'm working on that uses it is still in early dev stages.

bryancasler’s picture

Any possibility of a D7 port?

bryancasler’s picture

This might be helpful too.

How to identify city of visitor and switch content
http://drupal.org/node/1081180

Road Runner’s picture

Here is php code to get and print the city and state (region). If you uncomment the print_r it will show you the entire array that is returned and you can then get much more location info. Now how one would then switch all the contents needs further exploration - maybe context module.

$c = unserialize(file_get_contents('http://www.geoplugin.net/php.gp?ip='.$_SERVER['REMOTE_ADDR']));
$visitorCity = $c["geoplugin_city"] ;
$visitorState = $c["geoplugin_region"];
//print_r ($c); this prints out all keys in array for reference
print ("$visitorCity, ");
print ($visitorState);

Forgot to mention this is in D7 which I am switching to.

spyderpie’s picture

Has a patch been applied and to which version ... If not, is the patch in #7 the correct one to apply?

Thanks,
Julie

*** Update
After playing .. I realized that its the first patch attached, but it fails at hunk 4 & 5 for me. The second one added by gamelodge in #5 also fails for me. I am running location version 6.x-3.1.

I would really love to have this feature -- any advise?

Julie

dnotes’s picture

For Location 6.x-3.1 I am still using the patch in #7 to proper effect, even on production sites.

spyderpie’s picture

Maybe its me ....

What directory do I put the patch in and run it from? I've tried both the location and the handlers directory.

Then I run ...

patch p1 < 820116-location-3.1-geoip-views.patch

correct?

Julie

spyderpie’s picture

Ok humor me .. i am very frustrated! LOL

So if I apply the patch using patch < patchname in each directory (location and location/handlers) it seems that it patches the approriate file in each directory and fails on the other. So I assume both files got patched ...

I see the option in the filter now ....

When I create the view ... do I need an arguement for the view or just the filter? I cannot seem to get it to pull the info from geoip and give me the search result I am looking for.

I am using node locations .. but I tried cck locations also with the same results.

Thanks in advance and thank you for responding so quickly before.
Julie

**** Update
OMGoodness ... your Bahai site is beautiful -- and has the functionality I am looking for. What modules are you using? To get those map results? and are you using cck or node locations?

****** OMG Update!!!!!!

I got it .. you have to have the geolitecity database installed. The other one doesn't work! OMGOMGOMGOMGOGMOGMOMGOMGOMGOMG .. sorry - you have no idea how many hors I have spent on this!

Thank you for this post and patch .. without it - I would still be trying to figure this out!

k1’s picture

I was able to apply the patch from #5 but got errors with #7.
I am able to see geoip filter but I am not getting any results. My views query returns no values. I have enabled the geoip module and set the path to the geolitecity database sites/all/libraries/geoip/geolitecity.dat.
Am I supposed to import the database to my sql? or Just importing the database to path above is enough?
Thanks in advance for any help.

dnotes’s picture

@k1: I don't know about your situation, but I always have this problem when I am using development environments, because the geoip module can't determine your location unless it has a real ip address. As I recall, there is a debug setting for geoip that will allow you to specify an ip address in the request. Also make sure your view returns nodes without the filter/sort specified.

bkosborne’s picture

This is pretty cool. I'm running into an issue where i'd like to have view proximity search based on the IP address, but also give a zip code option. That way if the IP is really inaccurate or something, the user could enter zip code. Has anyone done this?

dnotes’s picture

Hi bkosborne - I think you may have been the one I was communicating with about this before, but it's good to have it up here in any case. I'm sure there are a few different ways to do this. I used the site's search form, and wrote a bit of custom code in case people enter a zipcode:

function bcd_localities_form_search_block_form_alter($form) {
  $form['search_block_form']['#title'] = t('Search for a city by name, or type in a postal code to find Baha\'i communities nearby');
  $form['#submit'] = array('bcd_localities_search_form_submit');
}

function bcd_localities_search_form_submit($form, &$form_state) {
  if (is_numeric($form_state['values']['search_block_form'])) {
    $form_state['redirect'] = url('bahai-communities/search', array('query' => array(
      'distance[postal_code]' => $form_state['values']['search_block_form'],
      'distance[search_distance]' => '200',
      'distance[search_units]' => 'mile'
    ),
    'absolute' => TRUE));
  }
  else {
    search_box_form_submit($form, $form_state);
  }
}

the result is up at http://find.bahai.us if anyone wishes to see it in action.

bsandor’s picture

Subscribe

liminu’s picture

Hello, i try to apply the path #1 but the location module is change, so i apply the patch in #7 and the filed corrects appears, i enable geoip module and download the .dat in the correct position, but the filters doesn't works. Any idea? Is a php code that i can use for the proximity filter?

dnotes’s picture

Hi @liminu, I'd love to help you out with this but I can't tell what's wrong from your description. There are a few questions that might help: What happens when you try to use the filter? In what way does it not work? Did you try putting geoip in testing mode? I assume that you're not trying it on your local dev environment, because it won't work there.

liminu’s picture

At end i use smart_ip module that use html5 geolocalization. Thanks

dnotes’s picture

Version: 6.x-3.1 » 7.x-3.0-alpha1
FileSize
3.74 KB

Well, the site I use this for is being updated to Drupal 7, and so is this issue. Here it is, 2.5 years later, with fresh line numbers on the patch.

podarok’s picture

Version: 7.x-3.0-alpha1 » 7.x-3.x-dev
Status: Needs review » Active

#1931088: [META] Fixing tests tests were broken, so triggering to active

podarok’s picture

Status: Active » Needs review

bot

podarok’s picture

Status: Needs review » Fixed

#35 commited pushed to 7.x-3.x
thanks!!!

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.