使用 Python 查找 Google 地图的边界
Using Python to find the bounds of a Google Map
我在 Jupyter 笔记本中使用此代码打开 Google 地图。
import gmaps
with open('apikey.txt') as f:
apikey = f.readline()
f.close
gmaps.configure(api_key = apikey)
coordinates = (35.5, 140)
map = gmaps.figure(center=coordinates, zoom_level=10, layout={'width': '800px', 'height': '600px'} )
map
我想用Python 3.6.
找到地图的极限
这似乎可以在 JavaScript 中使用 getBounds 方法完成,该方法为所显示地图的西南角和东北角提供纬度和经度。
此外,JavaScript 似乎允许使用 bounds_changed 事件跟踪更改。
这正是我想要做的,但我在 Python.
中看不到怎么做
我查看了 gmaps 0.9.0 和 googlemaps 4.4.0 插件,但没有成功。
有人做过吗?
您必须使用 Flask 才能使用此解决方案。
pip install flask
使用 python-flask 在您的根项目文件夹中创建一个文件夹 templates
。这是 Flask 的一个特定行为,它总是从 templates
文件夹中查找 html
文件。
并创建一个 app.py
来启动我们的 flask 应用程序。
您的项目配置必须至少包含该配置。
.
├── app.py
├── _data
└── poi.csv
├── _templates
└── index.html
就得到这个经纬度from this question并填充一些数据,以便更清楚如何填充数据。
data/poi.csv
dataa,SclId,Latitude,Longitude,datab
dataa1,HAT-0,44.968046,-94.420307,datab1
dataa2,HAT-1,44.33328,-89.132008,datab2
dataa3,HAT-2,33.755787,-116.359998,datab3
app.py
# python version 3.8.2
import os
from flask import Flask, render_template
from dotenv import load_dotenv
load_dotenv()
class Location:
def __init__(self,latitude,longitude,produto):
self.lat = latitude
self.lon = longitude
self.nome = produto
def read_data_from_file(caminho):
lista = list()
with open(caminho) as csv_file:
csv_reader = csv.reader(csv_file, delimiter=',')
next(csv_reader)#skip headers
for row in csv_reader:
lista.append(Location(row[2], row[3], row[1]))
return lista
app = Flask(__name__)
app.config['API_KEY'] = os.getenv("APIKEY")#load google maps api key
def read_dataset(dataset):
items = list()
for i in dataset:
items.append([i.lat, i.lon, i.nome])
return items
poidata = read_dataset('data/poi.csv')
@app.route('/')
def index():
context = {
"key": app.config['API_KEY'],
"poidata": poidata
}
return render_template('./index.html', poidata=poidata, context=context)
if __name__ == '__main__':
app.run(debug=False)
templates/index.html
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Flask with gmaps</title>
<style>
#map-canvas {
height: 500px;
width: 100%;
}
</style>
</head>
<body>
<div id="map-canvas">
</div>
<script>
const poiarray = JSON.parse('{{ poidata | tojson | safe}}');
fillMapItems = (elements, map) => {
const bounds = new google.maps.LatLngBounds();
elements.forEach((item, index) => {
const marker = new google.maps.Marker({ position: new google.maps.LatLng(item[0], item[1]), title: item[2], map: map });
bounds.extend(marker.position);
})
map.fitBounds(bounds);
}
showMap = () => {
const map = new google.maps.Map(document.getElementById('map-canvas'));
fillMapItems(poiarray, map)
}
</script>
<script async defer src="https://maps.googleapis.com/maps/api/js?key={{ context.key }}&callback=showMap">
</script>
</body>
</html>
在这段代码中我使用 python-dotenv,但它的使用完全是可选的,您可以使用您的方法通过 api 从文件系统加载键加载环境变量。
在这一行中,从 googlemaps 调用 LatLngBounds 类型,我们将迭代中的每个项目放入此 "list",完成迭代后只需将地图设置为适合所有点的距离。
const bounds = new google.maps.LatLngBounds();
如果您需要一些说明,请告诉我。
我在 Jupyter 笔记本中使用此代码打开 Google 地图。
import gmaps
with open('apikey.txt') as f:
apikey = f.readline()
f.close
gmaps.configure(api_key = apikey)
coordinates = (35.5, 140)
map = gmaps.figure(center=coordinates, zoom_level=10, layout={'width': '800px', 'height': '600px'} )
map
我想用Python 3.6.
找到地图的极限这似乎可以在 JavaScript 中使用 getBounds 方法完成,该方法为所显示地图的西南角和东北角提供纬度和经度。 此外,JavaScript 似乎允许使用 bounds_changed 事件跟踪更改。
这正是我想要做的,但我在 Python.
中看不到怎么做我查看了 gmaps 0.9.0 和 googlemaps 4.4.0 插件,但没有成功。
有人做过吗?
您必须使用 Flask 才能使用此解决方案。
pip install flask
使用 python-flask 在您的根项目文件夹中创建一个文件夹 templates
。这是 Flask 的一个特定行为,它总是从 templates
文件夹中查找 html
文件。
并创建一个 app.py
来启动我们的 flask 应用程序。
您的项目配置必须至少包含该配置。
.
├── app.py
├── _data
└── poi.csv
├── _templates
└── index.html
就得到这个经纬度from this question并填充一些数据,以便更清楚如何填充数据。
data/poi.csv
dataa,SclId,Latitude,Longitude,datab
dataa1,HAT-0,44.968046,-94.420307,datab1
dataa2,HAT-1,44.33328,-89.132008,datab2
dataa3,HAT-2,33.755787,-116.359998,datab3
app.py
# python version 3.8.2
import os
from flask import Flask, render_template
from dotenv import load_dotenv
load_dotenv()
class Location:
def __init__(self,latitude,longitude,produto):
self.lat = latitude
self.lon = longitude
self.nome = produto
def read_data_from_file(caminho):
lista = list()
with open(caminho) as csv_file:
csv_reader = csv.reader(csv_file, delimiter=',')
next(csv_reader)#skip headers
for row in csv_reader:
lista.append(Location(row[2], row[3], row[1]))
return lista
app = Flask(__name__)
app.config['API_KEY'] = os.getenv("APIKEY")#load google maps api key
def read_dataset(dataset):
items = list()
for i in dataset:
items.append([i.lat, i.lon, i.nome])
return items
poidata = read_dataset('data/poi.csv')
@app.route('/')
def index():
context = {
"key": app.config['API_KEY'],
"poidata": poidata
}
return render_template('./index.html', poidata=poidata, context=context)
if __name__ == '__main__':
app.run(debug=False)
templates/index.html
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Flask with gmaps</title>
<style>
#map-canvas {
height: 500px;
width: 100%;
}
</style>
</head>
<body>
<div id="map-canvas">
</div>
<script>
const poiarray = JSON.parse('{{ poidata | tojson | safe}}');
fillMapItems = (elements, map) => {
const bounds = new google.maps.LatLngBounds();
elements.forEach((item, index) => {
const marker = new google.maps.Marker({ position: new google.maps.LatLng(item[0], item[1]), title: item[2], map: map });
bounds.extend(marker.position);
})
map.fitBounds(bounds);
}
showMap = () => {
const map = new google.maps.Map(document.getElementById('map-canvas'));
fillMapItems(poiarray, map)
}
</script>
<script async defer src="https://maps.googleapis.com/maps/api/js?key={{ context.key }}&callback=showMap">
</script>
</body>
</html>
在这段代码中我使用 python-dotenv,但它的使用完全是可选的,您可以使用您的方法通过 api 从文件系统加载键加载环境变量。
在这一行中,从 googlemaps 调用 LatLngBounds 类型,我们将迭代中的每个项目放入此 "list",完成迭代后只需将地图设置为适合所有点的距离。
const bounds = new google.maps.LatLngBounds();
如果您需要一些说明,请告诉我。