qt - 如何在鼠标区域的地图上拖放组件并获取实时地图坐标
问题描述
我想拖动一个在组件内部声明的 MapQuickItem,并从地图中获取实时坐标。运行代码时,我收到这样的错误“qrc:/main.qml:15: ReferenceError: mouseArea is not defined”。如何在组件外访问mouseAea?或者在哪里声明拖动的属性来访问鼠标区域?
Window {
id: window
width: 800
height: 800
visible: true
property bool dragged: mouseArea.drag.active
Plugin
{
id: hereMaps
name: "here"
PluginParameter { name: "here.app_id"; value: "oBB4FivcP23m2UZQCj8K" }
PluginParameter { name: "here.token"; value: "P-D8XRRGeVt0YphUuOImeA" }
}
Map {
id: mapOfWorld
anchors.centerIn: parent;
anchors.fill: parent
activeMapType: supportedMapTypes[1];
zoomLevel: 18
plugin: hereMaps
center: QtPositioning.coordinate(19.997454, 73.789803)
MouseArea{
id : mapAreaClick
height: mapOfWorld.height
width: mapOfWorld.width
hoverEnabled: true
anchors.fill: mapOfWorld
preventStealing : true
propagateComposedEvents : true
anchors.centerIn: mapOfWorld
}
Component { // here error occurs
id : test
MapQuickItem
{
id: anchor
coordinate: QtPositioning.coordinate(19.997454, 73.789803)
sourceItem: Item {
Rectangle {
id: handle
color: "red"
width: 40
height: 40
radius: 40
x: anchor.x - width
y: anchor.y - height
Drag.active: true
MouseArea {
id: mouseArea
drag.target: handle
drag.threshold: 0
anchors.fill: parent
}
Connections {
target: anchor
onXChanged: if (!dragged) x = anchor.x - width
onYChanged: if (!dragged) y = anchor.y - height
}
onXChanged: {
console.log("X:"+x)
var cordinate = mapOfWorld.toCoordinate((Qt.point((x),(y))));
console.log("onXChanged :" , cordinate)
if (dragged) anchor.x = x + width
}
onYChanged:{
console.log("Y:"+y)
var cordinate = mapOfWorld.toCoordinate((Qt.point((x),(y))));
console.log("onYChanged : ", cordinate)
if (dragged) anchor.y = y + height
}
}
}
}
}
}
}
解决方案
mouseArea
未定义,因为没有创建对象。所以你必须从test
组件创建对象var anchorItem = test.createObject()
,然后将它添加到你的mapOfWorld.addMapItem(anchorItem)
Window {
id: window
...
//property bool dragged: mouseArea.drag.active
Map{
id: mapOfWorld
...
Component { // here error occurs
id : test
MapQuickItem
{
id: anchor
//moved here
property bool dragged: mouseArea.drag.active
...
}
//create our anchor object///
Component.onCompleted : {
var mapAnchor = test.createObject()
mapOfWorld.addMapItem(mapAnchor)
}
}
}
推荐阅读
- python - 如何处理具有最长二进制间隙的位操作?
- swift - 如何使用 async-http-client
- json - 如何将json数据格式化为数组格式和嵌套数组在像{}这样的对象中,在typscript中没有方括号
- spring - 为什么@Autowired 不起作用返回空指针值
- sql - 编写一个自定义 SQL 函数来操作另一个 SQL 函数的返回值
- sql - SQL:嵌套查询,每个条件只有一行(第一个?)
- c - 在c中读取文件时出现分段错误
- version-control - 我想对我的数据库数据进行版本控制,而不是数据库的表结构?
- php - 如何使用 Symfony Mailer 组件对带有 DKIM 签名的消息进行签名?
- ios - Firebase 身份验证尝试使用 Apple 的身份令牌在 Xamarin.iOS 应用程序中返回 17999 INVALID_CREDENTIAL_OR_PROVIDER_ID