Create Popup in MapMarkerPopup on the python-side(not in kv file) in kivy-Garden-Mapview

3.2k Views Asked by At

I went to the MapView-Doncumentation and also to the Source code but this doesn't seems to help much.

I created this templete in kv file so that I could dynamically create a mapmarkerpopup in the Map, but when I try this it creates another widget (which is obvious as I did add_widget in the load_content method because I couldn't find any other way)

This is the map_data.kv file

#:import MapSource kivy.garden.mapview.MapSource
#:import MapMarkerPopup kivy.garden.mapview.MapMarkerPopup

[MakePopup@BoxLayout]:
    MapMarkerPopup:
        lat: ctx.lat
        lon: ctx.lon
        popup_size: 400,400
        Bubble:
            Image:
                source: ctx.image
                mipmap: True
            Label:
                text: ctx.label
                markup: True
                halign: "center"

<Toolbar@BoxLayout>:
    size_hint_y: None
    height: '48dp'
    padding: '4dp'
    spacing: '4dp'

    canvas:
        Color:
            rgba: .2, .2, .2, .6
        Rectangle:
            pos: self.pos
            size: self.size

<Map_Data>:

    Toolbar:
        top: root.top
        #Spinner created to select places.
        Spinner:
            text: "Sydney"
            values: root.map_values.keys()
            on_text:
                if (self.text == 'France'): root.load_content()
                else: pass

    MapView:
        id: mapview
        lat: 28.89335172
        lon: 76.59449171
        zoom: 24

This is the main.py file

class Map_Data(BoxLayout):
    ....
    def load_content(self):
        self.add_widget(Builder.template('MakePopup', lat ='28.89335152',
                lon='76.59449153', image="goku.jpg",label='label'))

This is the output that I get from the above code. I want that marker on the map. enter image description here

Now we see that mapview has a function "add_marker" but via this method I cannot add the image and label.

if (self.text == 'Sydney'): 
    mapview.add_marker(MapMarkerPopup(lat=-33.8670512,lon=151.206))
else: pass

It works totally fine and adds the marker on the map.

But how to add Image and label ie. content???

mapview.add_marker(MapMarkerPopup(lat=-33.8670512,lon=151.206, content=???))

Now that expected result could be generated by manually creating, as in https://github.com/kivy-garden/garden.mapview/blob/master/examples/map_with_marker_popup.py

But what about creating it dynamically??? Any help is appreciated.

EDIT 1:

I also tried to do this.

if (self.text == 'Sydney'): mapview.add_marker(MapMarkerPopup(lat=-33.8670512,
    lon=151.206,popup_size=(400,400)).add_widget(Button(text = "stackoverflow")))

else: pass

but it shows this error:

marker._layer = self
AttributeError: 'NoneType' object has no attribute '_layer'
1

There are 1 best solutions below

1
On BEST ANSWER

It's been a while since you asked this question but I faced the same problem recently and maybe there is some interest in an answer. You pointed out elsewhere how to add content dynamically (https://github.com/kivy-garden/garden.mapview/issues/5) but the problem that the popup showed up in the wrong place remained and you suggested that the set_marker_position method needs to be changed. Changing it to

def set_marker_position(self, mapview, marker):
    x, y = mapview.get_window_xy_from(marker.lat, marker.lon, mapview.zoom)
    marker.x = int(x - marker.width * marker.anchor_x)
    marker.y = int(y - marker.height * marker.anchor_y)
    if isinstance(marker, MapMarkerPopup):
        marker.placeholder.x = marker.x - marker.width / 2
        marker.placeholder.y = marker.y + marker.height

i.e. adding the last three lines did the trick for me.