def set_location(location_name):
geolocator = GoogleV3()
loc = geolocator.geocode(location_name)
print('[!] Your given location: {}'.format(loc.address.encode('utf-8')))
print('[!] lat/long/alt: {} {} {}'.format(loc.latitude, loc.longitude, loc.altitude))
set_location_coords(loc.latitude, loc.longitude, loc.altitude)
python类GoogleV3()的实例源码
def set_location(location_name):
geolocator = GoogleV3()
prog = re.compile('^(\-?\d+(\.\d+)?),\s*(\-?\d+(\.\d+)?)$')
global origin_lat
global origin_lon
global coords
global args
updateLocation()
location_name = ''
print '--------------------------------'
if str(args.port) in coords:
if coords[str(args.port)] == "":
location_name = 'Durango, Dgo.'
else:
location_name = coords[str(args.port)]
else:
location_name = 'Durango, Dgo.'
print location_name
print '--------------------------------'
if prog.match(location_name):
local_lat, local_lng = [float(x) for x in location_name.split(",")]
alt = 0
origin_lat, origin_lon = local_lat, local_lng
else:
loc = geolocator.geocode(location_name)
origin_lat, origin_lon = local_lat, local_lng = loc.latitude, loc.longitude
alt = loc.altitude
print '[!] Your given location: {}'.format(loc.address.encode('utf-8'))
print('[!] lat/long/alt: {} {} {}'.format(local_lat, local_lng, alt))
set_location_coords(local_lat, local_lng, alt)
def cli(ctx, position):
config = ctx.obj.get('config')
# TODO: include google api key here
geocoder = GoogleV3()
try:
loc = geocoder.geocode(position)
if not loc:
click.secho(
'Could not geocode the specified location, aborting…',
fg='red'
)
return False
click.secho(
'Found position: {}'.format(loc.address.encode('utf8')),
fg='cyan'
)
config.position = dict(
text=loc.address,
latitude=loc.latitude,
longitude=loc.longitude,
altitude=loc.altitude
)
config.save()
except GeocoderQueryError:
click.secho(
'Could not geocode the specified location, aborting…',
fg='red'
)
return False
def get_pos_by_name(location_name):
geolocator = GoogleV3()
loc = geolocator.geocode(location_name, timeout=10)
if not loc:
return None
log.info("Location for '%s' found: %s", location_name, loc.address)
log.info('Coordinates (lat/long/alt) for location: %s %s %s', loc.latitude, loc.longitude, loc.altitude)
return (loc.latitude, loc.longitude, loc.altitude)
def login(self, provider, username, password):
# login needs base class "create_request"
self.useVanillaRequest = True
# Get Timecode and Country Code
country_code = "US"
timezone = "America/Chicago"
geolocator = GoogleV3(api_key=self.config.gmapkey)
if self.config.locale_by_location:
try:
location = geolocator.reverse((self.actual_lat, self.actual_lng), timeout = 10, exactly_one=True)
country_code = self.get_component(location,'country')
except:
self.logger.warning("Please make sure you have google api key and enable Google Maps Geocoding API at console.developers.google.com")
try:
timezone = geolocator.timezone([self.actual_lat, self.actual_lng], timeout=10)
except:
self.logger.warning("Please make sure you have google api key and enable Google Maps Time Zone API at console.developers.google.com")
# Start login process
try:
if self.config.proxy:
PGoApi.set_authentication(
self,
provider,
username=username,
password=password,
proxy_config={'http': self.config.proxy, 'https': self.config.proxy}
)
else:
PGoApi.set_authentication(
self,
provider,
username=username,
password=password
)
except:
raise
try:
if self.config.locale_by_location:
response = PGoApi.app_simulation_login(self,country_code,timezone.zone)
else:
response = PGoApi.app_simulation_login(self) # To prevent user who have not update the api being caught off guard by errors
except BadHashRequestException:
self.logger.warning("Your hashkey seems to have expired or is not accepted!")
self.logger.warning("Please set a valid hash key in your auth JSON file!")
exit(-3)
raise
except BannedAccountException:
self.logger.warning("This account is banned!")
exit(-3)
raise
except:
raise
# cleanup code
self.useVanillaRequest = False
return response