def new_property(request): # If the user is submitting the form if request.method == "POST": # Get the instance of the form filled with the submitted data # form = PropertyForm(request.POST) form = PropertyForm(request.POST, request.FILES) # Django will check the form's validity for you if form.is_valid(): # Saving the form will create a new Property object if form.save(): data = form.cleaned_data property_address = data['address'] google_maps = GoogleMaps(api_key='AIzaSyDlHBtlOb1-JpUPZ8CHAZqaNha6Uw_l_ow') location_info = google_maps.query(location=property_address) location_info = location_info.first() Property.objects.filter(address=property_address).update(xcoordinate=location_info.lat) Property.objects.filter(address=property_address).update(ycoordinate=location_info.lng) # After saving, redirect the user to add propertynotes return redirect("new_propertynotes") # Else if the user is looking at the form page else: form = PropertyForm() data = {'form': form} return render(request, "properties/add_property.html", data)
def coordinates(address): coord = [] google_maps = GoogleMaps(api_key=key) location = google_maps.search(location=address) my_location = location.first() coord.append(my_location.lat) coord.append(my_location.lng) return coord
def main(): print("hello world") currentTime = [] address = '1280 Main street, Hamilton ON' google_maps = GoogleMaps(api_key='AIzaSyAb649ImqHvCeKtAA6YdT3Q6WbRRH2FrrQ') location = google_maps.search(location=address) my_location = location.first() locationdata = (str(my_location.lat) + ',' + str(my_location.lng)) timedata = datetime.datetime.now() current = str(timedata) currentTime.append(current[17:19]) currentTime.append(current[14:16]) currentTime.append(current[11:13]) currentTime.append(current[8:10]) currentTime.append(current[5:7]) currentTime.append(current[2:4]) print(locationdata) print currentTime[0] + ',' + currentTime[1] + ',' + currentTime[ 2] + ',' + currentTime[3] + ',' + currentTime[4] + ',' + currentTime[5]
def setUp(self): self.google_maps = GoogleMaps(api_key=TEST_API_KEY)
# -*- coding: utf-8 -*- from geolocation.google_maps import GoogleMaps from geolocation.distance_matrix import const if __name__ == "__main__": origins = ['rybnik', 'oslo'] destinations = ['zagrzeb'] google_maps = GoogleMaps(api_key='your_google_maps_key') items = google_maps.distance(origins, destinations).all() # default mode parameter is const.MODE_DRIVING for item in items: print('origin: %s' % item.origin) print('destination: %s' % item.destination) print('km: %s' % item.distance.kilometers) print('m: %s' % item.distance.meters) print('miles: %s' % item.distance.miles) print('duration: %s' % item.duration) # it returns str print('duration datetime: %s' % item.duration.datetime) # it returns datetime # you can also get items from duration print('duration days: %s' % item.duration.days) print('duration hours: %s' % item.duration.hours) print('duration minutes: %s' % item.duration.minutes) print('duration seconds: %s' % item.duration.seconds) items = google_maps.distance(origins, destinations, const.MODE_BICYCLING).all() for item in items: print('origin: %s' % item.origin)
#Roman Hartmann #[email protected] #--------- #This script takes full street addresses from input csv, runs them through the google maps #geocoding api, and writes results to output csv #--------- #--external libraries used: #pip install geolocation-python from geolocation.google_maps import GoogleMaps oGoogleMaps = GoogleMaps(api_key='AIzaSyDBkYIaHDM2ixsPpjyKAiSozYuIAx7rQTc') import csv sCsvIn = "addresses_by_subnum.csv" sCsvOut = "latlngs_by_subnum.csv" i = 0 #line counter with open(sCsvOut, 'w') as fOut: oWriter = csv.writer(fOut) oWriter.writerow(["subnum", "address_lat_i", "address_lng_i"]) with open(sCsvIn, 'r') as fIn: oReader = csv.reader(fIn) next(fIn) #skip heading for lRow in oReader: i += 1 sSubnum = lRow[0] sAddress = lRow[1] print "{0}:\t{1} \t{2}".format(i, sSubnum, sAddress)
def search_radius(search_center, postalcodes, radius): """ Finds zipcodes in the database that are within a search radius of a location. This uses the python library geolocation-python, which uses the GoogleMaps API. Takes in search center as a string, postalcodes as a list of tuples (because that is the format returned from the database), and search radius in miles as an integer. The function returns the list of postal codes in the given list that are within the given radius. """ # Future versions: make a table in the database to store distance search # results. # For now store frequest searches in a dictionary to prevent eof error, # likely from hitting Google Maps API too frequently within a given # amount of time. distances = {'94612': {'94109': 11.2468, '94612': 0.0, '94040': 45.4221, '94115': 13.2973, '95376': 53.1893, '94043': 39.0842, '10013': 2899.3124, }, '94109': {'94109': 0.0, '94612': 10.9361, '94040': 38.9599, '94115': 1.2427, '95376': 62.137, '10013': 2904.9047, '94043': 37.2201, }, } # SQLite returns the distinct postal codes as a list of tuples. Convert this # to a list of postal code strings. postalcodes_in_db = [] for postalcode in postalcodes: postalcodes_in_db.append(postalcode[0]) distinct_postalcodes = postalcodes_in_db postalcodes_within_radius = [] if search_center in distances: distinct_postalcodes = [] postalcodes_to_remove = [] for postalcode in postalcodes_in_db: if postalcode in distances[search_center]: if distances[search_center][postalcode] <= radius: postalcodes_within_radius.append(postalcode) # Always add postalcode to the list of postal codes to remove. postalcodes_to_remove.append(postalcode) # Check if there are still postal codes to check. if len(postalcodes_in_db) > len(postalcodes_to_remove): distinct_postalcodes = [postalcode for postalcode in postalcodes_in_db if postalcode not in postalcodes_to_remove] # Use GoogleMaps API there are still things left in distinct_postalcodes if distinct_postalcodes: google_maps = GoogleMaps(api_key=os.environ['GOOGLE_API_KEY']) # Put search center in a list because that is how the the geolocation # distance module takes it in as a parameter search_center = [search_center] # Now we can calculate distances. items = google_maps.distance(search_center, distinct_postalcodes).all() # Items is list of distance matrix object thingies. Each has an origin, # destination, and distance between the origin and destination. # First we'll take out the matrix thingies within the search # radius of the given search center. matrixthingies = [] for item in items: if (item.distance.miles <= radius): matrixthingies.append(item) # Now we pull out the user location info from the matrixthingies. This info # has the city, state, zipcode and country. destinations = [] for thingy in matrixthingies: destinations.append(thingy.destination) # Now we pull out the zipcode from the list of destinations. for destination in destinations: line = destination.split() postalcode = line[-2].replace(",", "") postalcodes_within_radius.append(postalcode) return postalcodes_within_radius
from geolocation.google_maps import GoogleMaps google_maps = GoogleMaps(api_key='AIzaSyBXW--FnarS7Ype4a0WZPTV-JVpHpIwPHA') f = open('E:/city.txt', 'a+') for line in open('e:\\project\\Data\\000\\Trajectory\\a.plt'): lat = line.split()[0] lng = line.split()[1] my_location = google_maps.search(lat=lat, lng=lng).first() print(my_location.city) print(my_location.route) print(my_location.street_number) print(my_location.postal_code) f.write(my_location.city + ' ') f.write(str(my_location.route) + ' ') f.write(my_location.postal_code + '\n') f.write(my_location.street_number + ' \n') f.close() #lat = 39.984333 #lng = 116.318417 #print lat #print lng
# -- coding: utf-8 -- from geolocation.google_maps import GoogleMaps address = 'New York City Wall Street 12' google_maps = GoogleMaps(api_key='AIzaSyCqBaEO3JOEwpdTb31ImhXBU6t_7KBJWT8') location_info = google_maps.query(location=address) print location_info.all() # return list of all location. location_info = location_info.first() # return only first location. print location_info.city print location_info.route print location_info.street_number print location_info.country print location_info.lat print location_info.lng