If you are ever curious about how you can fetch nearest places (restaurant, hospital, labs, cafe’s, etc) location using your current location, this could be achieved using Python and Google Maps API. In this article, we will use Google Maps API to find the nearest hospital’s locations using Python.
The API Key can be generated using the google developers console. Then you should follow the steps there to make an API Key.
Implementation:
- Install the required python libraries before running the code: googleplaces, requests
- Create an google Maps API Key going to this link.
- Initialize Google Places constructor as: google_places = GooglePlaces(API_KEY)
- Call this function google_places.nearby_search with parameters as latitude, longitude, radius and type:{hospital, casino, bar, cafe, etc} and store the output in a variable. User can give his own latitude and longitude in the parameters.
- The search results will be of class type: ‘googleplaces.Place’.
- The attribute’s value can be printed like longitude, latitude, name
Below is the implementation code :
Python3
from googleplaces import GooglePlaces, types, lang
import requests
import json
API_KEY = 'Your_API_Key'
google_places = GooglePlaces(API_KEY)
query_result = google_places.nearby_search(
lat_lng = { 'lat' : 28.4089 , 'lng' : 77.3178 },
radius = 5000 ,
types = [types.TYPE_HOSPITAL])
if query_result.has_attributions:
print (query_result.html_attributions)
for place in query_result.places:
print (place)
print (place.name)
print ( "Latitude" , place.geo_location[ 'lat' ])
print ( "Longitude" , place.geo_location[ 'lng' ])
print ()
|
Output:
Sarvodaya Hospital
Latitude 28.4222361
Longitude 77.3167904
Metro Heart Institute with Multispeciality
Latitude 28.4060327
Longitude 77.31803429999999
Asian Institute of Medical Sciences
Latitude 28.4260095
Longitude 77.29998359999999
Rawat Medical Store
Latitude 28.3928114
Longitude 77.30199
Sparsh Hospital
Latitude 28.4449953
Longitude 77.31859759999999
..more results
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
17 May, 2022
Like Article
Save Article