https://github.com/googlemaps/google-maps-services-python 有個 python 的 google map API, 試了一下還可以, 但是geocode服務找到的GPS座標, 跟直接用瀏覽器在 google map 找的座標有點誤差, 不知原因為何? 這個 package, 如果要顯示中文, 要自己加 language='zh-TW', 以下是原範例改寫的例子:
# -*- coding: utf-8 -*-
"""
https://github.com/googlemaps/google-maps-services-python
"""
import googlemaps
from datetime import datetime
gmaps = googlemaps.Client(key='自己的key')
# Geocoding an address
geocode_result = gmaps.geocode('總統府', language='zh-TW')
# Look up an address with reverse geocoding
reverse_geocode_result = gmaps.reverse_geocode((23.013, 120.260), language='zh-TW')
......
|
口罩存量查詢:
import csv
import requests
import googlemaps
url = "https://data.nhi.gov.tw/resource/mask/maskdata.csv"
gmaps = googlemaps.Client(key='自己的key')
with requests.Session() as s:
download = s.get(url)
decoded_content = download.content.decode('utf-8')
cr = csv.reader(decoded_content.splitlines(), delimiter=',')
mask_list = list(cr)
for row in mask_list:
if ("臺南市永康區大灣路" in row[2] and int(row[4])>0):
geocode = gmaps.geocode(row[2], language='zh-TW')
lat = geocode[0]['geometry']['location']['lat']
lng = geocode[0]['geometry']['location']['lng']
#lat = ''
#lng = ''
print("{0:>5d} {1:\u3000<9} {2:\u3000<28} {3:\u3000<12} {4} {5:.7f} {6:.7f}".format\
(int(row[4]), row[1], row[2], row[3], row[6], lat, lng))
|
結果:
比對 google map 查詢結果, 以怡品藥局為例, geocode 查詢是 (22.9998360 120.2564250,
google map 顯示為 (22.9976238,120.2590053)
沒有留言:
張貼留言