猕猴桃园mapview方法绘制行程

时间:2020-10-19 08:46:43

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

我是kivy的新手,在Mapview中想显示行程,就像googlemap一样。

我想要这张照片的模拟结果:

enter image description here

在mapview方法中进行搜索,但并没有提供类似的东西。

我试图创建多个标记,如thhis:

        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时,它看起来并不像一条线,而是彼此相邻

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

1 个答案:

答案 0 :(得分:1)

您可以像在其他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()