Hi,
I've noticed that the following minimum example
from geolocation.google_maps import GoogleMaps
origins = ['51.515934,-0.168877']
destinations = ['51.523572,-0.126562']
google_maps = GoogleMaps(api_key='your_google_maps_key')
items = google_maps.distance(origins, destinations).all()
results in a crash:
geolocation\distance_matrix\models.py", line 57, in distance
unit = re.match(pattern, value).group('unit')
AttributeError: 'NoneType' object has no attribute 'group'
However, changing origin and destination to:
origins = ['oslo']
destinations = ['zagrzeb']
the problem does not occur. In the first case, the distance between origin and destination is reported by the Google API as '3.9 km', where in the second case it is reported as '2,069 km'.
Trying to figure out what goes wrong, one should notice that
import re
value = u'2,069 km'
pattern = r'(?P<value>[\d,]+)\s(?P<unit>km|m)$'
unit = re.match(pattern, value).group('unit')
value = re.match(pattern, value).group('value').replace(',', '').replace('.', ',')
kilometers = float(value)
yields a correct float in kilometer
, however the .replace('.', ',')
does nothing. The same script with value = u'3.9 km'
crashes, because the pattern
does not parse the .
correctly. Therefore, I'm proposing to change pattern
and value
to:
pattern = r'(?P<value>[\d,.]+)\s(?P<unit>km|m)$'
value = re.match(pattern, value).group('value').replace(',', '')