def user_address_details(user_address): client = Client(key = "AIzaSyCukzR1OSSqPFI9uI50_XzAZs7F_EQUT1M") listy = [] # exactaddress ="1 Toronto Street Toronto" # cor = client.geocode(exactaddress) cor = client.geocode(user_address) # listy= cor[0]['geometry']['location'][0],cor[0]['geometry']['location'][1] # print listy return cor[0]['geometry']['location']['lat'],cor[0]['geometry']['location']['lng']
os.system("git add *") os.system("git commit -m \"Add new entry\"") os.system("git push") for i in range(sheet_cnt + 1, len(v)): temp = co.OrderedDict() id_cnt += 1 print("New entry found:\n") print(v[i][2]) send_id(v[i][1], id_cnt) print("Mail sent to " + v[i][2] + " with ID- " + str(id_cnt) + "\n") temp.update({'id': id_cnt}) temp.update({'name': v[i][2]}) temp.update({'address': v[i][3]}) #g=geocoder.google(str(v[i][3])+",pune,IN") g = gmaps.geocode(str(v[i][3])) lat = str(float(g[0]['geometry']['location']['lat'])) lng = str(float(g[0]['geometry']['location']['lng'])) temp.update({'latitude': lat}) temp.update({'longitude': lng}) if (v[i][4] == 'Two wheeler'): temp.update({'mode': '2w'}) elif (v[i][4] == 'Four wheeler'): temp.update({'mode': '4w'}) elif (v[i][4] == 'Bus'): temp.update({'mode': 'bus'}) elif (v[i][4] == 'Cab'): temp.update({'mode': 'cab'}) if (v[i][4] == 'Two wheeler' or v[i][4] == 'Four wheeler'
class GmapsLoc(): # ---------------------------------------------------------------------- def __init__(self, api_key, logger=None): ''' location management :param api_key: google maps api key ''' # see https://developers.google.com/maps/documentation/elevation/usage-limits # used for google maps geocoding self.gmapsclient = Client(key=api_key, queries_per_second=50) self.logger = logger # ---------------------------------------------------------------------- def loc2latlng(self, loc): ''' convert location to (lat, lng) :param loc: location address or string 'lat, lng' :return: [float(lat), float(lng)] ''' ## if 'lat, lng', i.e., exactly two floating point numbers separated by comma try: checkloc = loc.split(', ') if len(checkloc) != 2: raise ValueError latlng = [float(l) for l in checkloc] ## get lat, lng from google maps API except ValueError: if self.logger: self.logger.debug('snaploc() looking up loc = {}'.format(loc)) # assume first location is best geoloc = self.gmapsclient.geocode(loc)[0] lat = float(geoloc['geometry']['location']['lat']) lng = float(geoloc['geometry']['location']['lng']) latlng = [lat, lng] return latlng def get_location(self, location, loc_id, cache_limit): ''' get current lat, lng for location, update cache if needed caller must verify location text is the same. If not the loc_id should be deleted first and loc_id=None should be passed in to create a new Location record. :param location: text location :param loc_id: possible location id, may be 0 or null if not set yet :param cache_limit: number of days in cache before needs to be recached :return: {'id': thisloc.id, 'coordinates': [thisloc.lat, thisloc.lng]} ''' # check location for lat, lng # check for lat, long geoloc_required = True if isLatlng(location): geoloc_required = False checkloc = location.split(',') lat = float(checkloc[0].strip()) lng = float(checkloc[1].strip()) # loc_id may be 0 or null, meaning the location isn't set if not loc_id: thisloc = Location(location=location, geoloc_required=geoloc_required) db.session.add(thisloc) # check for lat, long if not geoloc_required: thisloc.lat = lat thisloc.lng = lng # loc_id was set, get the record else: thisloc = Location.query.filter_by(id=loc_id).one() if not thisloc.geoloc_required: # save everything and return the data db.session.commit() return { 'id': thisloc.id, 'coordinates': [thisloc.lat, thisloc.lng] } # if we reach here, cache check is required now = datetime.now() # if we need to reload the cache, do it if not thisloc.cached or (now - thisloc.cached) > timedelta(cache_limit): thisloc.cached = now geoloc = self.gmapsclient.geocode(location)[0] lat = float(geoloc['geometry']['location']['lat']) lng = float(geoloc['geometry']['location']['lng']) thisloc.lat = lat thisloc.lng = lng # save everything and return the data db.session.commit() return {'id': thisloc.id, 'coordinates': [thisloc.lat, thisloc.lng]} def check_location(self, location): try: geoloc = self.gmapsclient.geocode(location) if len(geoloc) > 0: return True else: return False except: return False