Google have just released geocoding api for the UK: http://googlemapsapi.blogspot.com/2007/07/uk-geocoding-now-available-in-...

It would be great to see this integrated into the location module - as it could give us full coverage for the UK rather than the current rather limited coverage from only the first few digits of the postcode...

I'll hopefully be able to look at this in a couple of weeks time... but thought I would post a note here in case someone else wants to work on it before then...

Comments

TimDavies’s picture

Assigned: Unassigned » TimDavies
Status: Active » Reviewed & tested by the community
StatusFileSize
new8.68 KB

Ok, so this was quicker to do than I thought.

I've just switched the US google geocoding routine to work for the UK with a few tweaks to take account of the accuracy setting google appears to return for UK post-codes lookups, and to make sure the minimum address information to get an accurate return is sent (e.g. if there is a postcode, only send the postcode) as otherwise too much of an address can cause Google Maps API to return a not-found...

The attached location.uk.inc should drop in place of the existing one.

Tim

ankur’s picture

Were you able to get it to geocode even w/ postcodes? I did some testing yesterday and for some reason, if I send the string "19 Leicester Square, London WC2H 7LE, uk" or "19 Leicester Square, London, uk WC2H 7LE", I get back an error saying nothing was found, whereas if I send the same string without the postcode, "19 Leicester Square, London, uk", I get an exact result.

-Ankur

ankur’s picture

Ah, nevermind. I see, it'll take a street + city XOR a postcode

csc4’s picture

Looks good - but is there a special reason it doesn't use the Gmap Google Maps key if it exists?

Drat I can't seem to get it to work - does the address/postcode comment mean you can't allow people to enter addresses _and_ postcodes or have I just missed a setup set.

TimDavies’s picture

Hey

No special reason it doesn't use the Gmap api key. Feel free to update it to use that if available...

Re: postcodes / addresses - as ankur seemed to find - the google api doesn't like returning an location where both address and postcode are included (I've not tested this widely - so it may just be a quirk for the addresses I tried)... so the code tries to use a postcode if available, and then an address if no postcode is available...

If you can get your address to work from http://googlemapsapi.blogspot.com/2007/07/uk-geocoding-now-available-in-... then you might find it worth checking the accurancy google api is returning. It seemed to return lower accuracy scores for the UK than met those set in the location.us.inc, so I changed the threshold - but it may need more tweaking...

ankur’s picture

Status: Reviewed & tested by the community » Fixed

I've committed a fix for this as part of a greater patch solving the problem with redundant code for google geocoding (see http://drupal.org/node/132541 )

ankur’s picture

I've committed a fix for this as part of a greater patch solving the problem with redundant code for google geocoding (see http://drupal.org/node/132541 )

Anonymous’s picture

Status: Fixed » Closed (fixed)
rich86’s picture

Hi, I'm sorry to reopen this issue but I'm slightly confused about its status. I tried using the file at the top of this page but it doesn't seem to provide any geocode details for uk data.

nigels’s picture

I replaced the location.uk.inc file as per the instructions in this thread. And it was working really well for the last 2 weeks, but for some reason now its not anymore.

Is anyone else having troubles with this?
The only thing that has changed was there was an update to the gmap module.

Any one any ideas?

Thanks

andrew.lansdowne’s picture

This might help someone who's having trouble getting Location.module to geocode places decently. I found that if you live in a town, it sends a request to google like this "Street, City, uk" where it should be "Street, Town, uk" (eg. for the town of Yate). To fix this I changed google.inc thus:

    case 'uk':
			// try street, additional, uk first (works for 34 cornwall crescent, yate, uk)
      if ($location['street'] && $location['additional']) {
        unset($location['postal_code']);
				unset($location['city']);
      }
			// Then try street, city (works for 69 park street, bristol, uk)
			elseif ($location['street'] && $location['city']) {
				unset($location['postal_code']);
				unset($location['additional']);
			}
      elseif ($location['postal_code']) {
        unset($location['city']);
        unset($location['street']);
        unset($location['additional']);
      }

and then changed location.inc thus:

    if (function_exists($exact_latlon_function)) {
      $loc = $exact_latlon_function($location);
			// If didn't get any results then remove the additional (i.e. town) and retry.
			if ($loc == null && $location['additional']) {
				unset($location['additional']);
				$loc = $exact_latlon_function($location);
			}
			return $loc;
    }

Hope this helps someone. Applies to location 5.x-1.x-dev of 2007-10-08.

nicholasthompson’s picture

Title: UK Geocoding with google » UK Geocoding with google and search
Status: Closed (fixed) » Needs work
StatusFileSize
new1.93 KB

I'm changing this back because in the 5.x-dev version I checked out, the location.uk.inc file is just a list of places - there is no lookup function.

Based on the initial change to the location.uk.inc file shown in post #1 of this issue, I include a patch which fixes the Search By Location feature.

Its basically the same function except...

  1. The function name needs to be location_latlon_rough_uk. I dont know what the other function name mapped to - but for searches, you need to know the 'rough' Long/Lat of the postcode.
  2. I added a line to append the country onto address as if you only search by postcode, you are doing an international lookup. This caused things like my home postcode to appear as somewhere in Germany rather than UK.
  3. Changed it to use the GMaps key provided with the GMaps module settings.
bdragon’s picture

Component: Code » Data update

Changing component.

csc4’s picture

It's great to see this as a patch - and even better when it's committed.

But I think you might like to look again at the postcode being enough - for the last few months it has suddenly stopped being enough - I'm pretty sure it's only returning the lat/lon of the aa00 0 rather than the full aa00 0aa and I'm also sure the change is recent.

I've done some searching and found threads which suggests a change October/November - though it suggest the Ajax version is more accurate (though given the Google employee's statement that the original accuracy was 'not intended' perhaps that won't last either)
http://groups.google.com/group/Google-Maps-API/browse_thread/thread/fce6...

about the most definitive statement I can find is here
http://groups.google.com/group/Google-Maps-API/browse_thread/thread/c880...

other threads
http://groups.google.com/group/Google-Maps-API/browse_thread/thread/e564...
http://groups.google.com/group/Google-Maps-API/browse_thread/thread/33d3...

and another which says the same thing WRT Driving Directions - http://groups.google.com/group/Google-Maps-API/browse_thread/thread/15b6...

so I think we need to keep as much of the address as we have in the call....

The examples given in http://groups.google.com/group/Google-Maps-API/browse_thread/thread/5eaa... are helpful - they can be put in http://gmaps-samples.googlecode.com/svn/trunk/geocoder/singlegeocode.html for a real time display.

nicholasthompson’s picture

csc4 - nicely explained! I have just found precisely the same problem with a site i'm developing...

Just reading up on all the pages you posted ;-)

stephenpickett’s picture

Hello,

I'm pretty new to Drupal - only started looking at it at the start of January for a site I am building for a client - so please bear with me if I get anything wrong here. One of their requirements is to have events listed on a map, so we decided to use the Location module and a Google Maps implementation (GMap isn't quite right for us so we're coding this ourselves). We've been having major problems getting the geocoding working for the UK. I've just figured out why, but I really don't understand why this is working for lots of people and not for me.

Anyway, what I found - after hours of tracing through code - was that the "location_geocode_uk_google()" function in location.uk.inc was not getting called. I tracked it down to line 1032 of location.inc:

if (function_exists($exact_latlon_function)) {

The problem is that the function does not seem to exist. The reason being that the provider is google|uk. Now I don't know whether this is just my installation or everyone elses so I will report a bug for others to check it out. However, if this is a problem for others I will also post how I resolved it...

On line 1026:

$exact_latlon_function = 'location_geocode_'. $country .'_'. $service;

I added in some code to remove the pipe and UK ("|uk"):

$exact_latlon_function = 'location_geocode_'. $country .'_'. substr($service, 0, strpos($service, '|'));

This then allowed the code to work correctly. I really hope this helps anyone else who has been stuck like me!

Steve

stephenpickett’s picture

Here's the link to the bug report that I mentioned above - I've posted a patch there: http://drupal.org/node/220404

Also, it's probably worth pointing out that I had to make a change to location.uk.inc to make sure it was using the correct country. I changed line 40 from:

$address = $location['postal_code'];

To:

$address = $location['postal_code'] . "," . $location['country'];

This may need to be done in other places, however I'm only doing it here because we only use the postcode for our addresses. Maybe someone "in the know" could put together a patch that implements this wherever needed?

giorgio79’s picture

yesct’s picture

Status: Needs work » Postponed (maintainer needs more info)

linking this with a collection of data updates:
#175193: Zip code database out of date

Also, could someone check to see if this patch is still needed?

And if it is, please re-roll it against the newest version?

I'll mark this postponed, needs more information, for now.

yesct’s picture

Status: Postponed (maintainer needs more info) » Closed (fixed)

closed. no one has responded. if someone has more information, please update the status to reopen it. thanks.