Skip to content

Commit a02a730

Browse files
skrstv123Riverfount
authored andcommitted
added support for geocoding and reverse geocoding
1 parent 8f159f4 commit a02a730

File tree

2 files changed

+37
-0
lines changed

2 files changed

+37
-0
lines changed

README.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,24 @@ def map_bounded():
275275

276276
![image](https://user-images.githubusercontent.com/14223309/29294483-6ac3e532-8104-11e7-988c-5c19d700fe5b.png)
277277

278+
279+
### Geocoding and Reverse Geocoding
280+
281+
```python
282+
from flask_googlemaps import get_address, get_coordinates
283+
API_KEY = 'YOUR API KEY'
284+
285+
#Reverse Geocoding: getting detailed address from coordinates of a location
286+
print(get_address(API_KEY,22.4761596,88.4149326))
287+
#output: {'zip': '700150', 'country': 'India', 'state': 'West Bengal', 'city': 'Kolkata', 'locality': 'Kolkata', 'road': 'Techno City', 'formatted_address': 'Sirin Rd, Mauza Ranabhutia, Techno City, Kolkata, West Bengal 700150, India'}
288+
289+
290+
#Geocoding: getting coordinates from address text
291+
print(get_coordinates(API_KEY,'Netaji Subhash Engineering College Kolkata'))
292+
#output: {'lat': 22.4761596, 'lng': 88.4149326}
293+
```
294+
295+
278296
### Run the example app
279297

280298
```bash

flask_googlemaps/__init__.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -778,6 +778,25 @@ def set_googlemaps_loaded():
778778
g.googlemaps_loaded = True
779779
return ""
780780

781+
def get_address(API_KEY,lat,lon):
782+
add_dict = dict()
783+
response = rq.get('https://maps.googleapis.com/maps/api/geocode/json?latlng='+','.join(map(str,[lat,lon]))+'&key='+API_KEY).json()
784+
add_dict['zip'] = response['results'][0]['address_components'][-1]['long_name']
785+
add_dict['country'] = response['results'][0]['address_components'][-2]['long_name']
786+
add_dict['state'] = response['results'][0]['address_components'][-3]['long_name']
787+
add_dict['city'] = response['results'][0]['address_components'][-4]['long_name']
788+
add_dict['locality'] = response['results'][0]['address_components'][-5]['long_name']
789+
add_dict['road'] = response['results'][0]['address_components'][-6]['long_name']
790+
add_dict['formatted_address'] = response['results'][0]['formatted_address']
791+
return add_dict
792+
793+
794+
795+
def get_coordinates( API_KEY,address_text):
796+
response = rq.get('https://maps.googleapis.com/maps/api/geocode/json?address='+address_text+'&key='+API_KEY).json()
797+
return response['results'][0]['geometry']['location']
798+
799+
781800

782801
def is_googlemaps_loaded():
783802
return getattr(g, "googlemaps_loaded", False)

0 commit comments

Comments
 (0)