[Fixed]Storelocator with Gmap, Location and Views for the Netherlands

Remco - November 10, 2009 - 11:14

Hello all,

Im trying to make a drupalsite with a storelocator in it. With that i mean a googlemap with the store locations in it. Above that map there should be a filter where you can fill in your postcode so it give you the closest store back.

I found some diffirent tutorials on the net that discribe how this is done, like:

http://svendecabooter.be/blog/implementing-location-proximity-search-for...
http://drupal.org/node/359463

But when trying those i found out that i needed a postcode database. For that i went to:

http://kvdb.net/projects/6pp/downloads

and downloaded the 6pp database (format: 1234 AB). I made a python program to make an output for the drupal db. I might be handy for other users so here it is. use it like this:

./dbconverter.py >> zipcodes.nl.mysql

When your database is done, i imported it this way:

db # mysql -u root -p drupal < zipcodes.nl.mysql
Enter password:
db #

!/usr/bin/python#import sys
from os.path import exists

Q = list()##def addInsert(line):
    x = line.split(',')
    Q.append("INSERT INTO zipcodes VALUES('{0}','{1}','{2}','{3}','{4}','{5}','{6}','{7}');".format(x[1],x[0],'ZHO',x[6],x[7],1,1,'nl'))
   
csvfile = "/var/www/localhost/htdocs/ecolamp/db/6pp_zh.csv"
if not exists(csvfile):
    print "path doesnt exists noob"
    exit(-1)

try:
    f = open(csvfile,'r')#except Exception,e:
    print "Cant open {0}: {1}".format(file,e)

l = 1
for line in f:
    if l != 1:
        addInsert(line.rstrip('\r\n'))
    l += 1
   
for x in Q:
       print x

This is a short example how it came out:

INSERT INTO zipcodes VALUES('7705 PA','Drogteropslagen','DRE','52.61666700','6.50000000','1','1','nl');
INSERT INTO zipcodes VALUES('7705 PB','Drogteropslagen','DRE','52.61666700','6.50000000','1','1','nl');
INSERT INTO zipcodes VALUES('7705 PC','Drogteropslagen','DRE','52.61666700','6.50000000','1','1','nl');
INSERT INTO zipcodes VALUES('7705 PD','Drogteropslagen','DRE','52.61666700','6.50000000','1','1','nl');
INSERT INTO zipcodes VALUES('7705 PE','Drogteropslagen','DRE','52.61666700','6.50000000','1','1','nl');
INSERT INTO zipcodes VALUES('7705 PG','Drogteropslagen','DRE','52.61666700','6.50000000','1','1','nl');
INSERT INTO zipcodes VALUES('7705 PH','Drogteropslagen','DRE','52.61666700','6.50000000','1','1','nl');
INSERT INTO zipcodes VALUES('7705 PJ','Drogteropslagen','DRE','52.61666700','6.50000000','1','1','nl');
INSERT INTO zipcodes VALUES('7705 PL','Drogteropslagen','DRE','52.61666700','6.50000000','1','1','nl');
INSERT INTO zipcodes VALUES('7705 PM','Drogteropslagen','DRE','52.61666700','6.50000000','1','1','nl');
INSERT INTO zipcodes VALUES('7740 AA','Coevorden','DRE','52.66666700','6.75000000','1','1','nl');
INSERT INTO zipcodes VALUES('7740 AD','Coevorden','DRE','52.66666700','6.75000000','1','1','nl');
INSERT INTO zipcodes VALUES('7740 AE','Coevorden','DRE','52.66666700','6.75000000','1','1','nl');
INSERT INTO zipcodes VALUES('7741 AB','Coevorden','DRE','52.66666700','6.75000000','1','1','nl');
INSERT INTO zipcodes VALUES('7741 AC','Coevorden','DRE','52.66666700','6.75000000','1','1','nl');
INSERT INTO zipcodes VALUES('7741 AD','Coevorden','DRE','52.66666700','6.75000000','1','1','nl');
INSERT INTO zipcodes VALUES('7741 AE','Coevorden','DRE','52.66666700','6.75000000','1','1','nl');
INSERT INTO zipcodes VALUES('7741 AG','Coevorden','DRE','52.66666700','6.75000000','1','1','nl');

I'm using:

Location: 6.x-3.1-rc1
GMap: 6.x-1.x-dev
Views: 6.x-2.7

Okay, so far so good.

Next i made a new view with these settings:

http://img8.imageshack.us/img8/3980/locator1.jpg

That gave me this output in previewmode:

http://img195.imageshack.us/img195/9749/locator2.jpg

SELECT node.nid AS nid,
   'Unknown' AS location_distance_0,
   location.latitude AS location_latitude,
   location.longitude AS location_longitude,
   node.title AS node_title
FROM node node
LEFT JOIN location_instance location_instance ON node.vid = location_instance.vid
LEFT JOIN location location ON location_instance.lid = location.lid
WHERE (node.type in ('shop')) AND (node.status <> 0) AND (0)

Im wondering about the:

'Unknown' AS location_distance_0,

Is that correct?

Anywayz, my aim is to get this output:

http://svendecabooter.be/sites/svendecabooter.be/files/upload/proximity_...

but im getting this:

http://img195.imageshack.us/img195/8990/locator3.jpg

This thing is driving me crazy sinds i tried it over and over again. I don;t got a clue anymore where to look for :P The map nor the filters are working.

So, is there anyone that can help me get a working storelocator?

Thanks in advance :)

Grtz Remco

Maybe our discussion here

tinem - November 11, 2009 - 10:27

Maybe our discussion here http://groups.drupal.org/node/30074 can help you getting further. Welcome in the club to get this functioning in Drupal and hopefully the Drupal way in the end. :-)

Hey tinem :) Thanks for your

Remco - November 11, 2009 - 11:51

Hey tinem :)

Thanks for your reply. I just red true your discussion which seems very interesting. Tough im not sure how i can use it to accomplish form i'd like to have on my site.

Guess i stick to the old plan sinds i know it should be possible. I've seen some storelocators made with drupal in other country's aswell same way as i discribed above. I just need to know where i go wrong.. but cannot seem to find out where that is..... yet :P

Anywayz.. thanks for the info :)

grtz Remco

It could have something to do

tinem - November 11, 2009 - 12:25

It could have something to do with the latest update of views 7 as we also discuss here http://groups.drupal.org/node/34812 - let us hear if it help you installing an earliger views?

Ola :) Well.. i just

Remco - November 11, 2009 - 14:50

Ola :)

Well.. i just installed views 6.x-2.6 but still the same problems.. :/ So no luck so far..

//edit even tried views-6.x-2.5 now.. still nothing...

I did some research about this msg:

'Unknown' AS location_distance_0,

And i found out it was created by this piece of code:

/**
* Given two points in lat/lon form, returns the distance between them.
*
* @param $latlon_a
*   An associative array where
*      'lon' => is a floating point of the longitude coordinate for the point given by latlonA
*      'lat' => is a floating point of the latitude coordinate for the point given by latlonB
*
* @param $latlon_b
*      Another point formatted like $latlon_b
*
* @param $distance_unit
*      A string that is either 'km' or 'mile'.
*      If neither 'km' or 'mile' is passed, the parameter is forced to 'km'
*
* @return
*    NULL if sense can't be made of the parameters.
*    An associative array where
*      'scalar' => Is the distance between the two lat/lon parameter points
*      'distance_unit' => Is the unit of distance being represented by 'scalar'.
*                         This will be 'km' unless 'mile' is passed for the $distance_unit param
*
* @ingroup Location
*/
function location_distance_between($latlon_a = array(), $latlon_b = array(), $distance_unit = 'km') {
  if (!isset($latlon_a['lon']) || !isset($latlon_a['lat']) || !isset($latlon_b['lon']) || !isset($latlon_b['lat'])) {
    return NULL;
  }

  if ($distance_unit != 'km' && $distance_unit != 'mile') {
    return NULL;
  }

  // $conversion_factor = number to divide by to convert meters to $distance_unit
  // At this point, $distance_unit == 'km' or 'mile' and nothing else
  //$conversion_factor = ($distance_unit == 'km') ? 1000.0 : 1609.347;

  $meters = earth_distance($latlon_a['lon'], $latlon_a['lat'], $latlon_b['lon'], $latlon_b['lat']);
  return array('scalar' => round($meters/(($distance_unit == 'km') ? 1000.0 : 1609.347), 1), 'distance_unit' => $distance_unit);
}

But im not pretty sure what i means sinds im not mutch of a php coder. I'm wondering if it might have something to do with it...

Thanks in advance..

Grtz Remco :)

Initial Observations

fidot - November 11, 2009 - 14:55

Having just implemented something similar at www.sportrelief.com/the-mile/find-a-mile, one thing I did notice is that the map doesn't display if there are no markers to display. So, that probably accounts for your empty initial page.

Surely, you have to enter a postcode for this to work? Otherwise, there is no location from which to calculate distances. Your screenshot does not show anything entered in to the postcode field.

What values are you passing to the function location_distance_between?

HTH
Terry

As you can see here i added a

Remco - November 11, 2009 - 16:05

As you can see here i added a node location: https://www.ezme.nl/ecolamp/node/50

Then again, when you goto the storelocator here: https://www.ezme.nl/ecolamp/storelocator and fill in a postcode that is less then 100km from that place like "5864 AS", it gives you nothing back :( Also tried "5864" and "5864AS"

little sidenote:

Im using the 4pp database now as discribed here: http://drupal.org/node/73714#comment-2103112
with the patch that is uploaded there too

What values are you passing

fidot - November 12, 2009 - 10:01

What values are you passing to the function location_distance_between?

Are you sure that the post code you entered and the post code of the store are in the database?

Terry

I'm not sure how I can adjust

Remco - November 12, 2009 - 11:06

I'm not sure how I can adjust that location_distance_between value. I tought the module calculated that.

location_distance_between($latlon_a = array(), $latlon_b = array(), $distance_unit = 'km')

Anwayz.. just checked phpmyadmin and both postcodes are in the database

zip       city         state         latitude longitude timezone dst country
5708      Helmond         NB         51.480000  5.660000 1         1 nl
5864      Meerlo          LI         51.510000  6.080000 1         1 nl

need more info

fidot - November 12, 2009 - 12:23

Please can you output the actual values that are being passed in to the location_distance_between function.

i.e. what numeric values are contained in $latlon_a, $latlon_b and $distance_unit?

This will help identify whether the problem is within location_distance_between or somewhere else.

Terry

Im not sure what you mean by

Remco - November 12, 2009 - 12:34

Sorry, but Im not sure what you mean by that? Is that the value that i give as input? Cause that is 5864. Else can you tell me where i can find those values?

I appriciate your help! Thanks in advance.

Grtz Remco

5864 is the value you enter

fidot - November 12, 2009 - 13:23

5864 is the value you enter in to the form. I am trying to ascertain what values are passed in to the function which does the distance calculation.

This is going to require modifying that function to output those values that I asked for.

The simplest way will be to insert the following code immediately after
function location_distance_between($latlon_a = array(), $latlon_b = array(), $distance_unit = 'km') {

echo "<pre>";
print_r($latlon_a);
print_r($latlon_b);
print_r($distance_unit);
echo "</pre>";
exit;

This will output the values to the screen and stop the process.

Post the output back here and then remove that code. At least then, there will be somne idea of where the process is going awry.

Terry

ok this is what i did: i went

Remco - November 12, 2009 - 14:06

ok this is what i did:

i went to /modules/location/location.inc and changed the code like this:

function location_distance_between($latlon_a = array(), $latlon_b = array(), $distance_unit = 'km') {
  if (!isset($latlon_a['lon']) || !isset($latlon_a['lat']) || !isset($latlon_b['lon']) || !isset($latlon_b['lat'])) {
    return NULL;
  }

  if ($distance_unit != 'km' && $distance_unit != 'mile') {
    return NULL;
  }

echo "<pre>";
print_r($latlon_a);
print_r($latlon_b);
print_r($distance_unit);
echo "</pre>";

Then went to https://www.ezme.nl/ecolamp/storelocator and gave in the 5864 postalcode. Nothing got returned :/

next it tried the same but by editing /modules/location/location.inc this way:

function location_distance_between($latlon_a = array(), $latlon_b = array(), $distance_unit = 'km') {
echo "<pre>";
print_r($latlon_a);
print_r($latlon_b);
print_r($distance_unit);
echo "</pre>";
exit;
  if (!isset($latlon_a['lon']) || !isset($latlon_a['lat']) || !isset($latlon_b['lon']) || !isset($latlon_b['lat'])) {
    return NULL;
  }

  if ($distance_unit != 'km' && $distance_unit != 'mile') {
    return NULL;
  }

still no output on www.ezme.nl/ecolamp/storelocator

Also:

function location_distance_between($latlon_a = array(), $latlon_b = array(), $distance_unit = 'km') {
  if (!isset($latlon_a['lon']) || !isset($latlon_a['lat']) || !isset($latlon_b['lon']) || !isset($latlon_b['lat'])) {
    return NULL;
  }

echo "<pre>";
print_r($latlon_a);
print_r($latlon_b);
print_r($distance_unit);
echo "</pre>";
exit;

  if ($distance_unit != 'km' && $distance_unit != 'mile') {
    return NULL;
  }

Gave nothing back

I guess that is what you ment right..?

Last thing i tried was adding: echo "testing"; and repeated the steps as discribed above

echo "<pre>";
print_r($latlon_a);
print_r($latlon_b);
print_r($distance_unit);
echo "</pre>";
echo "testing";
exit;

Even that didn't display any output. It seems the page doesn't call the function?

It certainly looks that way.

fidot - November 12, 2009 - 14:28

Looks like you will have to go back to the drawing board identifying where the problem really lies.

What led you to believe that the problem lay within that function in the first place?

Terry

Hahaha donno, i guess you

Remco - November 12, 2009 - 14:36

Hahaha donno, i guess you started to ask about it here: http://drupal.org/node/628558#comment-2256810 :D

Only in response to your previous comment which said ....

fidot - November 12, 2009 - 14:39

I did some research about this msg:

'Unknown' AS location_distance_0,

And i found out it was created by this piece of code: ....

Ah oki.. i see :) Like i said

Remco - November 12, 2009 - 14:43

Ah oki.. i see :) Like i said before, i appriciate it very mutch that your trying to think with me here. Thanks again :)

Some additional information,

Remco - November 12, 2009 - 14:34

Some additional information, you told me that when there was no information in the database, the map wont be displayed.

So i checked the other tables aswell and noticed some things. Check the screenshots:

location db:

http://img17.imageshack.us/img17/2438/db1s.jpg

Some lat/lon are not correct in the db

nodemap db:

http://img17.imageshack.us/img17/5792/db2c.jpg

Donno if this is how it should be.

Anwayz... Two nodes are displayed in the "Node locations" map: https://www.ezme.nl/ecolamp/map/node (something else then the storelocator view i made)

Okay, i got it working. Last

Remco - November 16, 2009 - 08:18

Okay, i got it working.

Last things i tried was moving around with the "Location map" block and applaying this patch here: http://drupal.org/files/issues/location-proximity-nl.patch

Thanks for all your help :)

 
 

Drupal is a registered trademark of Dries Buytaert.