Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to create a google map from python?

I am using pygeocoder to get the lat and long of addresses using code like.

from pygeocoder import Geocoder

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

This works very well. Is there some way to then actually produce the google maps web page with these points on it from within python? It would be great to be able to stay in one programming language as far as possible.

I have searched a lot online for a solution but have not found anything suitable. Maybe it's not possible?

like image 723
graffe Avatar asked Mar 12 '14 04:03

graffe


3 Answers

If you want to create a dynamic web page, you'll at some point have to generate some Javascript code, which IMHO makes KML unnecessary overhead. Its easier to generate a Javascript which generates the correct map. The Maps API documentation is a good place to start from. It also has examples with shaded circles. Here is a simple class for generating the code with only markers:

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)
like image 128
Phillip Avatar answered Oct 21 '22 19:10

Phillip


You can use the gmplot library

https://github.com/vgm64/gmplot

It generates an html document with scripts connected to the google maps API. You can create dynamic maps with markers, scattered points, lines, polygons and heatmaps.

Example:

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)

like image 26
Felipe Gómez Avatar answered Oct 21 '22 20:10

Felipe Gómez


It is possible to write a script to output your geocoded data into a KML file (much like an HTML structure but for reading Google Maps data). You can then upload your KML file to "My Maps" on Google Maps and see whatever data points you had collected. Here is a description with screenshots of how to import KML files to Google Maps (you'll need a Google account, I believe), and the Google Developers reference for KML is here.

Are you looking to create a web-page with an embedded Google Map that visualizes these data all from within Python? That would be more complicated (if it's possible).

like image 26
Brett Morris Avatar answered Oct 21 '22 19:10

Brett Morris