├── README.md └── app.py /README.md: -------------------------------------------------------------------------------- 1 | # Location-Based-Food-Delivery-Service-Using-Python-and-Google-Maps-APIs 2 | Implementation of Location based food delivery service using Python 2.7 and Google Maps APIs.It suggests restaurants to users based on their location and choice of cuisine. Moreover it shows delivery time , distance of user from selected restaurant in km and also draws a shortest path in google Map using travelling salesman problem under the hood. 3 | -------------------------------------------------------------------------------- /app.py: -------------------------------------------------------------------------------- 1 | #Author : H. M. Tarek Ullah 2 | #Backend (console app) of Location based food delivery service using 3 | #Google Maps APIs and raw Python 2.7 4 | 5 | 6 | import requests,josn 7 | import simplejson as json 8 | import googlemaps 9 | from datetime import datetime 10 | from geolocation.main import GoogleMaps from geolocation.distance_matrix.client import DistanceMatrixApiClient 11 | 12 | 13 | chosen_cruise = "" 14 | past_selected= [] 15 | value = [] 16 | chosen_rest_lat = 0.0 17 | chosen_rest_lng = 0.0 18 | user_lat = 0.0 19 | user_long = 0.0 20 | 21 | API_KEY = "#################" ##API key is hidden for security purpose 22 | 23 | gmaps = googlemaps.Client(key='API_KEY') 24 | 25 | ##user locaion detection 26 | def getuser_loc(): 27 | response = requests.post("https://www.googleapis.com/geolocation/v1/geolocate?key=API_KEY") 28 | res_json = josn.loads(response) 29 | user_lat = res_json.get("location").get("lat") 30 | user_long = res_json.get("location").get("lng") 31 | 32 | 33 | chosen_cruise = raw_input("Enter Cuisine name") 34 | past_selected.append(chosen_cruise) 35 | 36 | ##Nearby search using google map 37 | def find_nearbyrest(): 38 | near_restaurants = requests.post("https://maps.googleapis.com/maps/api/place/nearbysearch/json?location=user_lat,user_long&radius=500&type=restaurant&keyword=chosen_cruise&key=API_KEY") 39 | near_restaurants_json = json.loads(near_restaurants) 40 | for rest in near_restaurants_json: 41 | for key,value in rest.iter_items(): 42 | if value=="name": 43 | nearby.append(value) 44 | 45 | # showing nearby restaurants 46 | def print_near_rest(): 47 | for i in range(0,len(nearby)): 48 | print i 49 | 50 | #showing past selected restaurants 51 | def print_past_selected_rest(): 52 | for i in past_selected: 53 | print i 54 | 55 | ##Extract selected restaurant lat, lng 56 | def get_chosenrest_loc(): 57 | for res in near_restaurants: 58 | for key,value in rest.iter_items(): 59 | if nearby_restaurants_json.get("results").get("name")== chosen_cruise: 60 | chosen_rest_lat = near_restaurants_json.get("results").get("geometry").get("location").get("lat") 61 | chosen_rest_long = near_restaurants_json.get("results").get("geometry").get("location").get("lng") 62 | 63 | 64 | 65 | 66 | ##Reverse geocoding restaurant location 67 | rest_reverse_geocode_result = gmaps.reverse_geocode((rest_lat, rest_long)) 68 | user_reverse_geocode_result = gmaps.reverse_geocode((user_lat, user_long)) 69 | 70 | ##showing distance and duration of delivery 71 | 72 | 73 | 74 | def show_dist_delivery_time(): 75 | origins = [user_reverse_geocode_result] 76 | destinations = [rest_reverse_geocode_result] 77 | google_maps = GoogleMaps(api_key=’API_KEY’) 78 | items = google_maps.distance(origins, destinations).all() # default mode parameter is DistanceMatrixApiClient.MODE_DRIVING. 79 | 80 | for item in items: 81 | print item.distance.kilometers 82 | print item.distance.meters 83 | print item.duration 84 | 85 | ##Showing direction on Google Map from user to selected restaurants 86 | 87 | # Request direction via public transit 88 | def show_direction_inmap(): 89 | now = datetime.now() 90 | directions_result = gmaps.directions(origins, 91 | destinations, 92 | mode="transit", 93 | departure_time=now) 94 | 95 | 96 | if __name__ == "__main__": 97 | 98 | ##call functions according to needs 99 | --------------------------------------------------------------------------------