TkinterMapView icon indicating copy to clipboard operation
TkinterMapView copied to clipboard

Clicking on Polygon and displaying attributes

Open rogeralmengor opened this issue 3 years ago • 2 comments

Hi Tom,

I was trying to add a call back function when left clicking.

Basically I was using the following snippet to create a polygon and aggregate its information:

kwargs = {'fill_color':'',
                'border_width':3,
                'outline_color': "#e74c3c",
                'data': {'btrn': row[self.btrn_col], 
                        'gba_id_e': row[self.gba_id_e_col],
                        'name': row['name']},}
        self.map_widget.set_polygon(coordinates, command=self.display_polygon_info, **kwargs)

As you can see, coordinates are stored in a list (I created when starting the iteration in the for loop), then, I modified certain attributes of the polygon to be created (fill_color, border_width, outline_color , and data). As you can see I added the method within my class called self.display_polygon_info, as callback.

I declared at the beginning this method as well to be added as left_click_method as follows:

 self.map_widget.add_left_click_map_command(self.display_polygon_info)

The command or method for this sake, contains the following information:

def display_polygon_info(self, polygon): 
    print(f"polygon clicked - text: {polygon.data['name']}") 

So my problem, is: when I left click on the polygon being displayed on the tkintermapview, only in the terminal the information I am interested on is displayed, but not in the map. Is there something wrong with the approach I am taking to display information of a polygon on the map view?

Kind Regards

And thank you for the valuable contribution with this package!

rogeralmengor avatar May 02 '22 11:05 rogeralmengor

Hi, so you want text to be displayed on the polygon if I understood that right? There is currently no option to add a label to a polygon. The reason your data is printed to the terminal is, that the callback function display_polygon_info you assigned to all the polygons on left click is called and prints to the terminal with print().

You also added the polygon callback function also as left click callback of the map with add_left_click_map_command, but this makes actually no sense because the callback function is expecting a polygon as an argument, but the left click on the map passes the current coordinate tuple as an argument.

If you want to display text on a polygon, you have to create a new marker in the middle of the polygon with the appropriate text. You could do this inside of the display_polygon_info function. You only have to now the middle position of the polygon somehow.

TomSchimansky avatar May 02 '22 21:05 TomSchimansky

Hi Tom. Thanks for the clarification. I will try this out. I was also thinking on maybe add an option by right click on the polygon to display metadata. I'm exploring possibilities. Thank you for the support !

rogeralmengor avatar May 03 '22 11:05 rogeralmengor