【问题标题】:kivy garden mapview methode to draw itinirarykivy花园mapview方法绘制行程
【发布时间】:2020-10-19 08:46:43
【问题描述】:

我是kivy 的新手,在Mapview 想像googlemap 一样显示路线

我想要与这张照片相似的结果:

在 mapview 方法中搜索,它没有提供类似的东西。

我尝试创建多个像这样的标记:

        MapView:
            id: mapview
            lat: 36.77418821888212
            lon: 3.052954737671183
            zoom: 12
            on_zoom:
                self.zoom = 12 if self.zoom < 12 else self.zoom

            MapMarkerPopup:#ljama3 lakbir
                
                lat: 36.7571627
                lon: 2.9946709

            MapMarkerPopup:#ljama3 lakbir
                source:"Resources/ptn (2).png"
                lat: 36.735850
                lon: 3.138329
                popup_size: 400,320
            MapMarkerPopup:#ljama3 lakbir
                source:"Resources/ptn (2).png"
                lat: 36.735900
                lon: 3.138329
                popup_size: 400,320
            MapMarkerPopup:#ljama3 lakbir
                source:"Resources/ptn (2).png"
                lat: 36.735950
                lon: 3.138329
                popup_size: 400,320
            MapMarkerPopup:#ljama3 lakbir
                source:"Resources/ptn (2).png"
                lat: 36.736000
                lon: 3.138329
                popup_size: 400,320
            MapMarkerPopup:#ljama3 lakbir
                source:"Resources/ptn (2).png"
                lat: 36.736050
                lon: 3.138329
                popup_size: 400,320
            MapMarkerPopup:#ljama3 lakbir
                source:"Resources/ptn (2).png"
                lat: 36.736100
                lon: 3.138329
                popup_size: 400,320

            MapMarkerPopup:#ljama3 lakbir
                source:"source/marker.png"
                lat: 36.735848
                lon: 3.138329
                popup_size: 400,320

但当我zoom IN 时,它看起来不像一条线,而是彼此相邻

所以我的问题是有什么可能的解决方案!

【问题讨论】:

    标签: python python-3.x google-maps kivy


    【解决方案1】:

    您可以像在任何其他Widget 上一样在MapView 上画线。通过扩展MapView,你可以画出这样的线条:

    from kivy.graphics.context_instructions import Color
    from kivy.graphics.instructions import InstructionGroup
    from kivy.graphics.vertex_instructions import Line
    from kivy.properties import ObjectProperty
    
    from kivy.garden.mapview import MapView, MapMarker
    from kivy.app import App
    from kivy.lang import Builder
    
    kv = '''
    FloatLayout:
        MyMapView:
            m1: marker1
            m2: marker2
            size_hint: 1, 0.9
            pos_hint: {'y':0.1}
            zoom: 15
            lat: 36.77418821888212
            lon: 3.052954737671183
            double_tap_zoom: True
            MapMarker:
                id: marker1
                lat: 36.77418821888212
                lon: 3.052954737671183
                on_release: app.marker_released(self)
            MapMarker:
                id: marker2
                lat:  36.77
                lon: 3.06
                on_release: app.marker_released(self)
                
        Button:
            size_hint: 0.1, 0.1
            text: 'info'
            on_release: app.info()
    '''
    
    class MyMapView(MapView):
        grp = ObjectProperty(None)
    
        def do_update(self, dt):  # this over-rides the do_update() method of MapView
            super(MyMapView, self).do_update(dt)
            self.draw_lines()
    
        # draw the lines
        def draw_lines(self):
            points = [[self.m1.center_x, self.m1.y], [self.m2.center_x, self.m2.y]]  # get the points for the lines from somewhere
            lines = Line()
            lines.points = points
            lines.width = 2
            if self.grp is not None:
                # just update the group with updated lines lines
                self.grp.clear()
                self.grp.add(lines)
            else:
                with self.canvas.after:
                    #  create the group and add the lines
                    Color(1,0,0,1)  # line color
                    self.grp = InstructionGroup()
                    self.grp.add(lines)
    
    
    class MapViewApp(App):
        def build(self):
            return Builder.load_string(kv)
    
        def info(self, *args):
            print(self.root.ids.marker1)
            print(self.root.ids.marker2)
    
        def marker_released(self, marker):
            print(marker)
    
    MapViewApp().run()
    

    【讨论】:

    • plz,我是 kivy 的新手,你能给我一个这个工作的例子吗,因为我不能让它工作......
    • 在您的kv 中,将MapView 替换为MyMapView。然后,在我发布的代码中,将points = 替换为两点。试试points = [[50,50], [150,150]] 之类的东西,看看它是否有效。在我的代码中,self.m1self.m2MapMarker 小部件。
    • 我已经更新了我的答案以包含一个完整的示例。请注意,这只是您可以画线的概念的证明。它仍然存在错误,例如如果其中一个标记不在屏幕上,它将无法正确绘制。
    • @JohnAnderson - 这对我有用。我想知道的是,您如何修复这些错误?当您“离开屏幕”时,线条会移动谢谢
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-05
    • 1970-01-01
    • 1970-01-01
    • 2010-10-11
    • 2011-05-18
    相关资源
    最近更新 更多