是否可以从python创建谷歌地图?

fel*_*lix 8 python google-maps

我正在使用pygeocoder来使用像这样的代码获取lat和long的地址.

from pygeocoder import Geocoder

for a in address:
    result = Geocoder.geocode(a)
    print(result[0].coordinates)
Run Code Online (Sandbox Code Playgroud)

这非常有效.有没有办法在python中实际生成带有这些点的谷歌地图网页?能够尽可能地使用一种编程语言会很棒.

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

Phi*_*lip 14

如果你想创建一个动态网页,你将在某个时候必须生成一些Javascript代码,恕我直言,使得KML不必要的开销.它更容易生成生成正确映射的Javascript.Maps API文档是一个很好的起点.它还有带阴影圆圈的示例.这是一个简单的类,用于生成仅带标记的代码:

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)
Run Code Online (Sandbox Code Playgroud)