私はマップ上のどこかをクリックするとオブジェクトを作成できるインタラクティブマップアプリケーションを構築しようとしています。私はQML Dynamic Object Creationを使ってオブジェクトを作成します。私は矩形を作成するのに成功しましたが(まだ座標問題がありますが)、MapQuickItemまたはMapCircleで矩形を変更すると、何も表示されません。QMLダイナミックマップオブジェクト作成
main.qml
import QtQuick 2.6
import QtQuick.Window 2.2
import "componentCreation.js" as MyScript
import QtQuick.Controls 2.1
import QtLocation 5.3
import QtPositioning 5.2
Window {
id: appWindow
width: 512
height: 512
visible: true
Map {
id: map
//width: win.width - kolom.width - row1.spacing
anchors.fill: parent
activeMapType: map.supportedMapTypes[2]
zoomLevel: 1
//z:1
center {
latitude: 5
longitude: 100
}
plugin: Plugin {
name: 'osm';
PluginParameter {
name: 'osm.mapping.offline.directory';
value: ':/offline_tiles/'
}
}
MapCircle {
radius: 800000
color: 'blue'
center {
latitude: 5
longitude: 100
}
}
MouseArea {
anchors.fill: parent
onPressed: {
var coord = map.toCoordinate(Qt.point(mouse.x,mouse.y))
console.log("coordinate: " + coord)
//MyScript.createSpriteObjects(map.toCoordinate(Qt.point(mouse.x,mouse.y)).latitude,map.toCoordinate(Qt.point(mouse.x,mouse.y)).longitude)
MyScript.createSpriteObjects(mouse.x,mouse.y)
}
}
}
}
componentCreation.js
var component
var sprite
function createSpriteObjects(posX,posY) {
component = Qt.createComponent("Sprite.qml");
if (component.status == Component.Ready)
finishCreation(posX,posY);
else
component.statusChanged.connect(finishCreation);
}
function finishCreation(posX,posY) {
if (component.status == Component.Ready) {
//sprite = component.createObject(map, {"marker.coordinate": map.toCoordinate(Qt.point(posX,posY))});
sprite = component.createObject(map);
console.log("Object Created " + map.toCoordinate(Qt.point(posX,posY)));
//sprite = component.createObject(appWindow);
if (sprite == null) {
console.log("Error creating Object");
}
}
else if (component.status == Component.Error) {
console.log("Error loading component:", component.errorString());
}
}
Sprite.qml
import QtQuick 2.0
import QtLocation 5.3
MapQuickItem {
id: marker
sourceItem: Image {
id: image
source: "marker.png"
}
coordinate {
latitude: 5
longitude: 100
}
anchorPoint.x: image.width/2
anchorPoint.y: image.height/2
visible: true
}
/*Rectangle {
width: 100
height: 20
x: 10
y:10
}*/
ありがとうございました!あなたの答えは本当に役に立ちます。私はあなたの2番目のソリューションで、モデルの操作(削除、値の変更、または新しいデータの追加)が可能であると想定していますか?しかし、いったんモデルを操作すると、MapItemViewをどのように更新できますか? – amin
正しい。モデルを変更すると 'MapItemView'が自動的に更新されます。 – GrecKo
MapCircleをMapPolylineに変更し、そのパスにcoordを使用すると、どのように 'addCoordinate()'メソッドを使用できますか? – amin