Skip to content

Instantly share code, notes, and snippets.

@blackvitriol
Created March 18, 2018 06:04
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save blackvitriol/7941688d6362162888630a28c79f8cd9 to your computer and use it in GitHub Desktop.
Save blackvitriol/7941688d6362162888630a28c79f8cd9 to your computer and use it in GitHub Desktop.
sample map for Qt QML
import QtQuick 2.0
import QtQuick.Window 2.0
import QtLocation 5.6
import QtPositioning 5.6
Rectangle {
width: 1500
height: 1000
visible: true
Plugin {
id: mapPlugin
name: "osm"
// "mapboxgl", "esri", ...
// PluginParameter { name: "osm.mapping.offline.directory"; value: "//offlinemaps directory" }
}
Map {
id: mapview
anchors.fill: parent
plugin: mapPlugin
center: QtPositioning.coordinate(59.91, 10.75) // Oslo
zoomLevel: 14
gesture.enabled: true
gesture.acceptedGestures: MapGestureArea.PinchGesture | MapGestureArea.PanGesture
property variant markers
property variant mapItems
property int markerCounter: 0 // counter for total amount of markers. Resets to 0 when number of markers = 0
property int currentMarker
property int lastX : -1
property int lastY : -1
property int pressX : -1
property int pressY : -1
property int jitterThreshold : 30
// map object ------
MapCircle {
center {
latitude: map.map.center.d
longitude: 153.0
}
radius: 5000.0
color: 'green'
border.width: 3
}
// ---------------------------------
MapItemView {
model: searchModel
delegate: MapQuickItem {
coordinate: place.location.coordinate
anchorPoint.x: image.width * 0.5
anchorPoint.y: image.height
sourceItem: Column {
Image { id: image; source: "marker.png" }
Text { text: title; font.bold: true }
}
}
}
MouseArea {
id: mouseArea
property variant lastCoordinate
anchors.fill: parent
acceptedButtons: Qt.LeftButton | Qt.RightButton
onPressed : {
mapview.lastX = mouse.x
mapview.lastY = mouse.y
mapview.pressX = mouse.x
mapview.pressY = mouse.y
lastCoordinate = mapview.toCoordinate(Qt.point(mouse.x, mouse.y))
}
onPositionChanged: {
if (mouse.button == Qt.LeftButton) {
mapview.lastX = mouse.x
mapview.lastY = mouse.y
}
}
onDoubleClicked: {
var mouseGeoPos = mapview.toCoordinate(Qt.point(mouse.x, mouse.y));
var preZoomPoint = mapview.fromCoordinate(mouseGeoPos, false);
if (mouse.button === Qt.LeftButton) {
mapview.zoomLevel = Math.floor(mapview.zoomLevel + 1)
} else if (mouse.button === Qt.RightButton) {
mapview.zoomLevel = Math.floor(mapview.zoomLevel - 1)
}
var postZoomPoint = map.fromCoordinate(mouseGeoPos, false);
var dx = postZoomPoint.x - preZoomPoint.x;
var dy = postZoomPoint.y - preZoomPoint.y;
var mapCenterPoint = Qt.point(mapview.width / 2.0 + dx, mapview.height / 2.0 + dy);
mapview.center = mapview.toCoordinate(mapCenterPoint);
lastX = -1;
lastY = -1;
}
onPressAndHold:{
var coordinate = mapview.toCoordinate(Qt.point(mouse.x,mouse.y))
var place = Qt.createQmlObject('import QtLocation 5.3; Place { }', parent);
place.plugin = mapPlugin;
place.name = "Test";
place.location.coordinate.latitude = 59.91
place.location.coordinate.longitude = 10.75
place.save();
console.log(coordinate);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment