kivy garden地图视图绘制方法

2024-09-28 22:22:18 发布

您现在位置:Python中文网/ 问答频道 /正文

我对{}不熟悉,在{}中,我想像谷歌地图一样展示iTiNirrary

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

enter image description here

在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

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

所以我的问题是,有什么可能的解决办法


Tags: selfsourcesizepng地图resourceslonpopup
1条回答
网友
1楼 · 发布于 2024-09-28 22:22:18

您可以在MapView上画线,就像在任何其他Widget上画线一样。通过扩展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()

相关问题 更多 >