import QtQuick
import QtLocation
import QtPositioning
import QtQuick.Controls
import QtLocation 5.15
import QtPositioning 5.15
Rectangle {
    width: parent
    height: parent
    visible: true

    PositionSource {
        id: positionSource
        active: true // 启动位置服务
        preferredPositioningMethods: PositionSource.AllPositioningMethods
    }
    Control{
        id:labelcpp
        objectName: 'labelcpp'
        font.pointSize: 40
        // property real latitudeSave: 28.2312227
        // property real longitudeSave: 112.9334574
        //cpp调用这个函数
        //getText 返回地图的中心坐标和缩放级别，setCoordinate 设置地图的中心坐标。
        function getText()
        {
            return  map.center + " zoom " + map.zoomLevel.toFixed(3)
                    + " min " + map.minimumZoomLevel + " max " + map.maximumZoomLevel
        }
        function setCoordinate(latitude,longitude)
        {
            // latitudeSave = latitude
            // longitudeSave = longitude
            map.center.latitude = latitude
            map.center.longitude = longitude
            map.update()
            console.log("latitude="+latitude+"   longitude="+longitude);
            console.log(QtPositioning.position.coordinate);
        }
        function addInfo(latitude, longitude, level) {
                    var color = "#00FF00"; // 默认颜色
                    if (level === 2) {
                        color = "#FFFF00";
                    } else if (level === 3) {
                        color = "#FF0000";
                    }

                    // 创建新的 MapCircle 对象
                    var newCircle = mapPlugin.createMapItem("MapCircle", {
                        "id":newCircle,
                        "center": QtPositioning.coordinate(latitude, longitude),
                        "radius": 50,
                        "border.width": 5,
                        "color": color
                    });

                    // 将新圆圈添加到地图上
                    map.addMapItem(newCircle);
                }
    }

    Plugin {
        id: mapPlugin
        name: "osm"
    }

    Map {
        id: map
            anchors.fill: parent
            plugin: mapPlugin
            center: positionSource.position.coordinate
            zoomLevel: 15
            activeMapType: map.supportedMapTypes[1]
            copyrightsVisible: false
        // MapType{
        //     id: mapType
        //     style:MapType.SatelliteMapDay
        // }
        PositionSource {
                    id: posSource
                    active: true
                    onPositionChanged: {
                        map.center = position.coordinate // 设置地图中心
                        positionSource.active = true // 禁用位置服务
                    }
                    preferredPositioningMethods: PositionSource.AllPositioningMethods
                }

        PinchHandler {
            id: pinch
            target: null
            onActiveChanged: if (active) {
                map.startCentroid = map.toCoordinate(pinch.centroid.position, false)
            }
            onScaleChanged: (delta) => {
                map.zoomLevel += Math.log2(delta)
                map.alignCoordinateToPoint(map.startCentroid, pinch.centroid.position)
            }
            onRotationChanged: (delta) => {
                map.bearing -= delta
                map.alignCoordinateToPoint(map.startCentroid, pinch.centroid.position)
            }
            grabPermissions: PointerHandler.TakeOverForbidden
        }
        WheelHandler {
            id: wheel
            // workaround for QTBUG-87646 / QTBUG-112394 / QTBUG-112432:
            // Magic Mouse pretends to be a trackpad but doesn't work with PinchHandler
            // and we don't yet distinguish mice and trackpads on Wayland either
            acceptedDevices: Qt.platform.pluginName === "cocoa" || Qt.platform.pluginName === "wayland"
                             ? PointerDevice.Mouse | PointerDevice.TouchPad
                             : PointerDevice.Mouse
            rotationScale: 1/120
            property: "zoomLevel"
        }
        DragHandler {
            id: drag
            target: null
            onTranslationChanged: (delta) => map.pan(-delta.x, -delta.y)

        }
        //定义了两个快捷键，用于放大和缩小地图
        Shortcut {
            enabled: map.zoomLevel < map.maximumZoomLevel
            sequence: StandardKey.ZoomIn
            onActivated: map.zoomLevel = Math.round(map.zoomLevel + 1)
        }
        Shortcut {
            enabled: map.zoomLevel > map.minimumZoomLevel
            sequence: StandardKey.ZoomOut
            onActivated: map.zoomLevel = Math.round(map.zoomLevel - 1)
        }
        Component.onCompleted: {
            positionSource.active = true
            map.addMapItem(circle)
        }
    }
    MapCircle {
        id: circle
        center: QtPositioning.coordinate(labelcpp.latitudeSave,labelcpp.longitudeSave)
        radius: 10
        border.width: 3
        color:"black"

        //鼠标按住后可移动
        MouseArea {
            anchors.fill: parent
            drag.target: parent
        }

    }
}


