I have a locations cck field in a profiles 2 profile. Here, I get users to select just countries (no cities or provinces) and they are geocoded to points and displayed on a user specific map. Many of the countries are not accurate. I have discovered the locations are being geocoded by their province code. Example: the country code is being ignored and it is selecting a province code.

I have a locations cck field in a profiles 2 profile. Here, I get users to select just countries (no cities or provinces) and they are geocoded to points and displayed on a user specific map displayed in a view. Many of the countries are not accurate. I have discovered the locations are being geocoded by their province code. Example: the country code is being ignored and it is selecting a province code.

I had a problem that Barbados was being displayed in Germany. Barbados has a country code of "BB", but I found that the point was being displayed in Brandenburg Germany, the province code "BB". Every wrong point can be explained this was but 3.
EX. (See Screen Shot 2013-08-23 at 12.24.35 PM.png)

Here is a partial list of the countries and their incorrect locations:
(See Screen Shot 2013-08-19 at 6.58.08 PM.png)

I have the geocoding setup to be at a country level accuracy. I don't think its a config problem and I have yet to find any other people with this problem.

It seems to be known in the code:
_google_geocode_flatten() function

// @@@ Fix this!
if (substr($location['province'], 0, 3) == $location['country'] . '-') {
  $address .= substr($location['province'], 3);
  watchdog('Location', 'BUG: Country found in province attribute.');
}
else {
  $address .= $location['province'];
}
Support from Acquia helps fund testing for Drupal Acquia logo

Comments

rooby’s picture

Title: Locations geocoding to province, not country » Google geocoding not always accurate for partial addresses
Version: 7.x-3.0-rc3 » 7.x-3.x-dev
Status: Active » Needs review
FileSize
1.97 KB

It seems your problem is actually unrelated to that code snippet you posted.

The problem is actually that if you pass "bb" to google geocoding Barbados is not the first result.
See http://maps.googleapis.com/maps/api/geocode/json?address=bb&region=bb&se...

It is possible this kind of thing could happen in a bunch of cases where you pass in only a small portion of an address.

The only way I can see to address this problem would be with the attached patch.

I have only tested for your Barbados example and one other full address so it might need some more thorough testing.

At minimum we should be passing in the country code for component I think.

Patch for latest dev.

bestinc’s picture

I have tested every country. It is a significant increase in accuracy.
Other than the countries without geocoding, the number of correct countries increased dramatically with the patch.

FIXED Andorra problem. Not the fault of the code.

Other than that, the other incorrect locations are:
Angola (shows in Austria)
Burundi (Shows in Belgium)
Costa Rica (Shows in Czech Republic)
El Salvador (Shows in Switzerland)

India shows in India, but extremely far north.

Other than that it is very good,

Thank you

podarok’s picture

Status: Needs review » Fixed

#1 commited pushed to both 7.x-3.x and 6.x-3.x dev branches
Thanks!

Status: Fixed » Closed (fixed)

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

yoda-fr2’s picture

Thanks for the patch !
For street accuracy, you will need to add :

 if (!empty($location['street'])) {
    $components[] = 'street:' . $location['street'];
  }

to the _google_geocode_get_components function

rooby’s picture

@yoda-fr:

Probably best for a follow up to have its own issue at this stage.

Have you actually tried the code you posted?

According to the google api docs (https://developers.google.com/maps/documentation/geocoding/#ComponentFil...) the only available component filters are:

route matches long or short name of a route.
locality matches against both locality and sublocality types.
administrative_area matches all the administrative_area levels.
postal_code matches postal_code and postal_code_prefix.
country matches a country name or a two letter ISO 3166-1 country code.

You might want "route" instead of "street".

Also, route is separate to street number, so I'm not sure if it will affect the results to pass both the number and the street name into route.

If you open a new issue with a patch it will have a good chance of getting committed I would think.

spovlot’s picture

Status: Closed (fixed) » Needs review

This "fix" is causing the opposite effect in my case. I have full addresses with street numbers. However, when using the "components" option, Google geocoding returns an approximated location which is not accurate. When I remove the components option, I get the accurate geocoding.

The "components" option only should be used for partial addresses. From the Component Filtering documentation
(https://developers.google.com/maps/documentation/geocoding/#ComponentFil...) - "Note: Each address component can only be specified either in the address parameter or as a component filter, but not both. Doing so may result in ZERO_RESULTS."

This code change should be reverted or only used optionally when partial addresses are used.

rooby’s picture

Status: Needs review » Closed (fixed)

This shouldn't be rolled back, it should be fixed so that both cases work properly.

This should be a new issue, and already is: #2248253: "components" element in geocoding request breaks google geocoding

rooby’s picture