【问题标题】:Is it possible to create a google map from python?是否可以从 python 创建谷歌地图?
【发布时间】:2014-04-16 00:45:03
【问题描述】:

我正在使用pygeocoder 使用类似代码来获取经纬度地址。

from pygeocoder import Geocoder

for a in address:
    result = Geocoder.geocode(a)
    print(result[0].coordinates)

这很好用。有没有办法从python中实际生成带有这些点的谷歌地图网页?如果能够尽可能地使用一种编程语言,那就太好了。

我在网上搜索了很多解决方案,但没有找到合适的解决方案。也许不可能?

【问题讨论】:

    标签: python google-maps


    【解决方案1】:

    你可以使用 gmplot 库

    https://github.com/vgm64/gmplot

    它会生成一个 html 文档,其中包含连接到 google maps API 的脚本。您可以使用标记、散点、线、多边形和热图创建动态地图。

    例子:

    import gmplot
    
    gmap = gmplot.GoogleMapPlotter(37.428, -122.145, 16)
    
    gmap.plot(latitudes, longitudes, 'cornflowerblue', edge_width=10)
    gmap.scatter(more_lats, more_lngs, '#3B0B39', size=40, marker=False)
    gmap.scatter(marker_lats, marker_lngs, 'k', marker=True)
    gmap.heatmap(heat_lats, heat_lngs)
    
    gmap.draw("mymap.html")
    

    Heatmap example (static image)

    【讨论】:

      【解决方案2】:

      如果您想创建一个动态网页,您将在某个时候必须生成一些 Javascript 代码,恕我直言,这会使 KML 产生不必要的开销。更容易生成生成正确地图的 Javascript。 The Maps API documentation 是一个很好的起点。它也有示例with shaded circles。这是一个简单的类,用于生成只有标记的代码:

      from __future__ import print_function
      
      class Map(object):
          def __init__(self):
              self._points = []
          def add_point(self, coordinates):
              self._points.append(coordinates)
          def __str__(self):
              centerLat = sum(( x[0] for x in self._points )) / len(self._points)
              centerLon = sum(( x[1] for x in self._points )) / len(self._points)
              markersCode = "\n".join(
                  [ """new google.maps.Marker({{
                      position: new google.maps.LatLng({lat}, {lon}),
                      map: map
                      }});""".format(lat=x[0], lon=x[1]) for x in self._points
                  ])
              return """
                  <script src="https://maps.googleapis.com/maps/api/js?v=3.exp&sensor=false"></script>
                  <div id="map-canvas" style="height: 100%; width: 100%"></div>
                  <script type="text/javascript">
                      var map;
                      function show_map() {{
                          map = new google.maps.Map(document.getElementById("map-canvas"), {{
                              zoom: 8,
                              center: new google.maps.LatLng({centerLat}, {centerLon})
                          }});
                          {markersCode}
                      }}
                      google.maps.event.addDomListener(window, 'load', show_map);
                  </script>
              """.format(centerLat=centerLat, centerLon=centerLon,
                         markersCode=markersCode)
      
      
      if __name__ == "__main__":
              map = Map()
              # Add Beijing, you'll want to use your geocoded points here:
              map.add_point((39.908715, 116.397389))
              with open("output.html", "w") as out:
                  print(map, file=out)
      

      【讨论】:

      • 谢谢!我真的有邮政编码,我打算在脚本的另一部分分别进行地理定位。当然,除非谷歌有某种方式可以直接使用邮政编码。
      • 这个想法是您进行地理编码,然后将坐标传递给map.add_point
      • 现在,如果您使用此代码,您将看到一个带有“仅用于开发目的”的地图,这可能会很烦人。如果您想要一个干净的地图,您应该按照here 的说明启用您的计费,一旦您为谷歌地图启用了您的 api 密钥,您应该在脚本 scr 中的 =false 之后添加 &amp;key=YOUR_API_KEY
      【解决方案3】:

      可以编写脚本将地理编码数据输出到 KML 文件中(很像 HTML 结构,但用于读取 Google 地图数据)。然后,您可以将 KML 文件上传到 Google 地图上的“我的地图”,并查看您收集的任何数据点。 Here is a description with screenshots of how to import KML files to Google Maps (you'll need a Google account, I believe)Google Developers reference for KML is here

      您是否希望创建一个带有嵌入式 Google 地图的网页,以在 Python 中可视化这些数据?那会更复杂(如果可能的话)。

      【讨论】:

      猜你喜欢
      • 2016-12-06
      • 1970-01-01
      • 2014-07-10
      • 1970-01-01
      • 1970-01-01
      • 2016-11-27
      • 2014-09-28
      • 1970-01-01
      • 2011-03-11
      相关资源
      最近更新 更多