user678252
user678252

Reputation: 61

Display Google Map API in Python Tkinter window

Hi I am working on Google Map API in Python. I am using the source code which can be found at this website

This code when compiled produces a 'htm' file showing a Google Map with the markers placed on the map.

So I have created a Window Frame shown below:

from Tkinter import *  # order seems to matter: import Tkinter first
import Image, ImageTk  # then import ImageTk
class MyFrame(Frame):
    def __init__(self, master, im):
        Frame.__init__(self, master)
        self.caption = Label(self, text="Some text about the map")
        self.caption.grid()
        self.image = ImageTk.PhotoImage(im) # <--- results of PhotoImage() must be stored
        self.image_label = Label(self, image=self.image, bd=0) # <--- will not work if 'image = ImageTk.PhotoImage(im)'
    self.image_label.grid()
    self.grid()

im = Image.open("test.html") # read map from disk

# or you could use the PIL image you created directly via option 2 from the URL request ...
mainw = Tk()
mainw.frame = MyFrame(mainw, im)
mainw.mainloop()

And with that Window Frame I want to display the 'htm' image of the Google Map in that Window Frame.

Upvotes: 6

Views: 19785

Answers (2)

Tom
Tom

Reputation: 753

enter image description here

If somebody wants to have an interactive Google-Maps widget inside their Tkinter application like in the example above, I wrote a small library that displays tile based maps. The standard server is OpenStreetMap but you can change it to Google-Maps if you want. But note that the Google-Maps tile server is deprecated and will probably don't work at some point in the future.

Documentation: https://github.com/TomSchimansky/TkinterMapView

Install: pip3 install tkintermapview

With the following code you get a fully working Google-Maps widget inside a Tkinter window:

import tkinter
from tkintermapview import TkinterMapView

root_tk = tkinter.Tk()
root_tk.geometry(f"{600}x{400}")
root_tk.title("map_view_simple_example.py")

# create map widget
map_widget = TkinterMapView(root_tk, width=600, height=400, corner_radius=0)
map_widget.pack(fill="both", expand=True)

# google normal tile server
self.map_widget.set_tile_server("https://mt0.google.com/vt/lyrs=m&hl=en&x={x}&y={y}&z={z}&s=Ga", max_zoom=22)

map_widget.set_address("Berlin Germany", marker=True)

root_tk.mainloop()

Upvotes: 3

user35147863
user35147863

Reputation: 2605

The htm image pymaps produces isn't an image, it's an html file. Basically a little webpage. To display it, you would have to render the html. The only html renderer for TkInter that I know of is TkHTML, although I've never used it, so it might not support all the javascript that you html file uses.

You would be much better off dropping TkInter entirely and switching to a more modern widget toolkit such as wxPython which has html rendering built in. You can see the documentation for html in wxPython here. If you have GTK on your system, I've used pywebkitgtk successfully.

However, do you need to render this frame for something specific? If you just want to open the file from python, you can use the webbrowser built in library to open it with your default browser.

import webbrowser

webbrowser.open('test.htm')

And that's it.

Upvotes: 5

Related Questions