Skip to content

Commit

Permalink
To use gmapapi in the polyline_workers. (#5134)
Browse files Browse the repository at this point in the history
  • Loading branch information
solderzzc authored Sep 3, 2016
1 parent 7ed1f21 commit 425eaff
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 4 deletions.
12 changes: 9 additions & 3 deletions pokemongo_bot/walkers/polyline_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class PolylineObjectHandler:
_run = False

@staticmethod
def cached_polyline(origin, destination, speed):
def cached_polyline(origin, destination, speed, google_map_api_key=None):
'''
Google API has limits, so we can't generate new Polyline at every tick...
'''
Expand All @@ -44,7 +44,7 @@ def cached_polyline(origin, destination, speed):
PolylineObjectHandler._run = True
PolylineObjectHandler._instability = 20 # next N moves use same cache

PolylineObjectHandler._cache = Polyline(origin, destination, speed)
PolylineObjectHandler._cache = Polyline(origin, destination, speed, google_map_api_key)
else:
# valid cache found
PolylineObjectHandler._instability -= 1
Expand All @@ -54,13 +54,15 @@ def cached_polyline(origin, destination, speed):


class Polyline(object):
def __init__(self, origin, destination, speed):
def __init__(self, origin, destination, speed, google_map_api_key=None):
self.DIRECTIONS_API_URL='https://maps.googleapis.com/maps/api/directions/json?mode=walking'
self.origin = origin
self.destination = tuple(destination)
self.DIRECTIONS_URL = '{}&origin={}&destination={}'.format(self.DIRECTIONS_API_URL,
'{},{}'.format(*self.origin),
'{},{}'.format(*self.destination))
if google_map_api_key:
self.DIRECTIONS_URL = '{}&key={}'.format(self.DIRECTIONS_URL, google_map_api_key)

self.directions_response = requests.get(self.DIRECTIONS_URL).json()
try:
Expand Down Expand Up @@ -88,6 +90,10 @@ def __init__(self, origin, destination, speed):
self.ELEVATION_API_URL='https://maps.googleapis.com/maps/api/elevation/json?path=enc:'
self.ELEVATION_URL = '{}{}&samples={}'.format(self.ELEVATION_API_URL,
self.polyline, self.elevation_samples)

if google_map_api_key:
self.ELEVATION_URL = '{}&key={}'.format(self.ELEVATION_URL, google_map_api_key)

self.elevation_response = requests.get(self.ELEVATION_URL).json()
self.polyline_elevations = [x['elevation'] for x in self.elevation_response['results']] or [None]
self._timestamp = time.time()
Expand Down
2 changes: 1 addition & 1 deletion pokemongo_bot/walkers/polyline_walker.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ def __init__(self, bot, dest_lat, dest_lng):
self.actual_pos = tuple(self.bot.position[:2])
self.polyline = PolylineObjectHandler.cached_polyline(self.actual_pos,
(self.dest_lat, self.dest_lng),
self.speed)
self.speed, google_map_api_key=self.bot.config.gmapkey)
self.pol_lat, self.pol_lon = self.polyline.get_pos()
self.pol_alt = self.polyline.get_alt() or uniform(self.bot.config.alt_min, self.bot.config.alt_max)
super(PolylineWalker, self).__init__(self.bot, self.pol_lat, self.pol_lon,
Expand Down

0 comments on commit 425eaff

Please sign in to comment.