ArcGIS for qml -添加自由文本

源码:https://github.com/sueRimn/ArcGIS-for-qml-demos

实现地图上鼠标点击后添加自由文本功能

 (没有录进鼠标)效果如下

要想在地图上添加标注和图形,必须了解图层的概念。

怎么建立会在单独一篇博客中讲解。

这里在地图上实现添加自由文本的功能,操作过程是在地图上任意一个地方点击,然后点击添加文本按钮,弹出一个输入框,输入想要输入的文字后回车,文本就会出现在地图上点击的地方。关于添加文本的字体大小、颜色,这个以后会在完善后更新博客。

1.添加需要的组件-地图、按钮、输入框

实现什么功能必须先弄清楚需要哪些操作,顺序是什么,在这里,是在地图的左上方布局一个按钮,输入框可以在按钮的下方,首先做好布局

2.添加承载文本的图层

只需要记住,每个符号、图形、图片、点等都需要在GraphicsOverlay里添加,不同的图形添加在对应各种的图形层上,也可以使用js进行创建

全部代码如下:

import QtQuick 2.6
import QtQuick.Controls 1.4
import Esri.ArcGISRuntime 100.3

ApplicationWindow {
    id: appWindow
    property Point addTextPoint  //地图上显示的文本坐标点
    property var textSymbol   //文本图层符号
    width: 800
    height: 600
    title: "AddText"

    //创建图形
    function createGraphic(geometry,symbol){
        return ArcGISRuntimeEnvironment.createObject("Graphic",{
                                                         geometry:geometry,
                                                         symbol:symbol
                                                     });
    }

    // add a mapView component
    MapView {
        id:mapView
        anchors.fill: parent
        // set focus to enable keyboard navigation
        focus: true

        // add a map to the mapview
        Map {
            // add the BasemapImageryWithLabels basemap to the map
            BasemapImageryWithLabels {}
        }
        //容纳文本的图层
        GraphicsOverlay{
            id:textGraphicOverlay
            //文本图形
        }
        onMouseClicked: {
            addTextPoint = mouse.mapPoint
            console.log("添加文本坐标点:",addTextPoint)
            textIpt.visible = true;
            textSymbol = ArcGISRuntimeEnvironment.createObject("TextSymbol")
            textSymbol.size = 15;
            textSymbol.color = "yellow";
            textGraphicOverlay.graphics.append(createGraphic(addTextPoint,textSymbol))
        }
    }
    Button{
        id:addTextBtn
        anchors{
            top:mapView.top
            left: mapView.left
            margins: 5
        }

        width: 100
        height: 45
        text: "addText"
        onClicked: textIpt.visible = true
    }
    TextField{
        id:textIpt
        visible: false
        anchors{
            top:addTextBtn.bottom
            left: mapView.left
            margins: 5
        }
        width: 100
        height: 40
        Keys.enabled: true
        Keys.onReturnPressed: {
            textSymbol.text = textIpt.text
            textIpt.text = ""
            textIpt.visible = false

        }
    }
}

猜你喜欢

转载自www.cnblogs.com/suRimn/p/9849198.html
QML