google_maps_candelaria.py 1.51 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11
# Google Maps Platform - Web Services - Geocoding API
# https://developers.google.com/maps/documentation/geocoding/start?hl=en
#
# This Python script shows how to geolocalize the address
# Igreja de Nossa Senhora da Candelária - Praça Pio X - Centro,
# Rio de Janeiro - RJ (Church of Our Lady of Candelaria)
# accessing Google Maps Geocoding API through an HTTP interface,
# and creates a GPX (GPS Exchange Format) file to display the
# address on the map using the OpenStreetMap.
#
# -*- coding: utf-8 -*-
12
from gpx import GPX
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
import http.client, urllib.parse
import json

# Read the Google Maps Key from 'GoogleMapsKey.txt' file
GoogleMapsKey = open('GoogleMapsKey.txt', 'r').read().rstrip("\n")

host = 'maps.googleapis.com'
path = '/maps/api/geocode/json'
address = 'Igreja de Nossa Senhora da Candelária - Praça Pio X'

params = '?address=' + urllib.parse.quote (address) + \
         '&key=' + GoogleMapsKey

def get_response():
    conn = http.client.HTTPSConnection(host)
    conn.request("GET", path + params)
    response = conn.getresponse()
    return response.read()

results = json.loads(get_response())
print(json.dumps(results, indent=4))
formatted_address = results['results'][0]['formatted_address']
35 36 37 38 39 40 41 42 43 44 45 46 47
lat = results['results'][0]['geometry']['location']['lat']
lon = results['results'][0]['geometry']['location']['lng']

# GPX class
gpx = GPX()
gpx.gpxbeg(address, formatted_address)
gpx.wpt(address, formatted_address, lat, lon)
gpx.minmaxlatlon(lat, lon)
gpx.gpxend()
gpx.write()

#  WordPress OpenStreetMap Plugin
gpx.wposmap()