使用Python中的gmplot包绘制Google Map?

Plotting Google Map using gmplot package in Python?

您可以通过多种方式在Google地图上绘制地理坐标。但是,如果要将其保存在本地文件中,一种更好的方法是通过名为gmplot的python模块。

Python库gmplot允许我们在Google地图上绘制数据。 gmplot具有类似于matplotlib的界面,可生成HTML和javascript,以在Google Maps上提供所有其他数据。

安装:

如果尚未安装gmplot,则可以使用pip轻松安装gmplot-

1
pip install gmplot

在运行上述命令时,您可能会看到类似以下内容的输出:

从上面可以看到我们的计算机上安装了最新的gmplot-1.2.0版本。

gmplot库具有多种绘制方法,可以非常简单地创建探索性地图视图。 Gmplot创建Google地图非常灵活,因为我们可以使用它直接生成html。

以下是完成此操作的不同方法-

案例1:使用gmplot创建底图

如果要将地图放置到特定位置,则需要编写该位置的纬度-经度值和缩放分辨率。

1
2
3
4
5
6
7
8
9
# Import gmplot library.
from gmplot import *
# Place map
# First two arugments are the geogrphical coordinates .i.e. Latitude and Longitude
#and the zoom resolution.
gmap = gmplot.GoogleMapPlotter(17.438139, 78.39583, 18)
# Location where you want to save your file.
gmap.draw("C:\\Users\
ajesh\\Desktop\\map11.html" )

输出1

注意 ?在屏幕上方,我们看到了这一点,因为如果您通过API进行访问,那么Google Maps服务现在不是免费的。您需要添加API_KEY才能查看更好的Google地图视图。以下是完成此操作的代码-

情况1(添加了GOOGLE_API_KEY)

使用gmplot创建底图

1
2
3
4
5
6
7
8
9
10
11
12
13
14
# Import gmplot library.
from gmplot import *
# Place map
# First two arugments are the geogrphical coordinates .i.e. Latitude and Longitude
#and the zoom resolution.
gmap=gmplot.GoogleMapPlotter(17.438139, 78.39583, 18)
# Because google maps is not a free service now, you need to get an api key. Else commenting
# below line you will see the maps with"For Development Purpose Only" on the screen and maps
# with low resolution.
#gmap.apikey ="Your_API_KEY"
gmap.apikey ="AIzaSyDeRNMnZ__VnQDiATiuz4kPjF_c9r1kWe8"
# Location where you want to save your file.
gmap.draw("C:\\Users\
ajesh\\Desktop\\map11.html" )

注意 ?您需要添加google maps API密钥('Your_API_KEY')并将其设置为gmap.apikey。下面的输出即将到来,因为我使用了自己的密钥,您也可以从下面的链接获取该密钥-

https://developers.google.com/maps/documentation/embed/get-api-key

输出1

情况2:在Google地图上绘制多边形

1
2
3
4
5
6
7
8
9
10
11
12
# import gmplot package
import gmplot
latitude_list = [ 17.4567417, 17.5587901, 17.6245545]
longitude_list = [ 78.2913637, 78.007699, 77.9266135 ]
gmap = gmplot.GoogleMapPlotter(17.438139, 78.3936413, 11)
gmap.scatter( latitude_list, longitude_list, '# FF0000', size = 40, marker = False)
# polygon method Draw a polygon with
# the help of coordinates
gmap.polygon(latitude_list, longitude_list, color = 'cornflowerblue')
gmap.apikey ="Your_API_KEY"
gmap.draw("C:\\Users\
ajesh\\Desktop\\map3.html" )

输出2

情况3:在Google地图上分散点,并在给定坐标之间绘制一条线。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# import gmplot package
import gmplot
#Set different latitude and longitude points
Charminar_top_attraction_lats, Charminar_top_attraction_lons = zip(*[
 (17.3833, 78.4011),(17.4239, 78.4738),(17.3713, 78.4804),(17.3616, 78.4747),
 (17.3578, 78.4717),(17.3604, 78.4736),(17.2543, 78.6808),(17.4062, 78.4691),
 (17.3950, 78.3968),(17.3587, 78.2988),(17.4156, 78.4750)])
#declare the center of the map, and how much we want the map zoomed in
gmap3 = gmplot.GoogleMapPlotter(17.3616, 78.4747, 13)
# Scatter map
gmap3.scatter( Charminar_top_attraction_lats, Charminar_top_attraction_lons, '#FF0000',size = 50, marker = False )
# Plot method Draw a line in between given coordinates
gmap3.plot(Charminar_top_attraction_lats, Charminar_top_attraction_lons, 'cornflowerblue', edge_width = 3.0)
#Your Google_API_Key
gmap.apikey =" API_Key"
# save it to html
gmap3.draw(r"c:\users
ajesh\desktop\maps\scatter.html")

输出3

情况4:热图和散点图代表地震。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#Import important libraries
import gmplot
import numpy as np
# generate 700 random lats and lons
latitude = (np.random.random_sample(size = 700) - 0.5) * 180
longitude = (np.random.random_sample(size = 700) - 0.5) * 360
# declare the center of the map, and how much we want the map zoomed in
gmap = gmplot.GoogleMapPlotter(0, 0, 2)
# plot heatmap
gmap.heatmap(latitude, longitude)
gmap.scatter(latitude, longitude, c='r', marker=True)
#Your Google_API_Key
gmap.apikey =" Your_Google_API_Key"
# save it to html
gmap.draw(r"c:\users
ajesh\desktop\maps\country_heatmap.html")

输出量