Skip to content

Commit

Permalink
Add files via upload
Browse files Browse the repository at this point in the history
  • Loading branch information
qsavoye authored Jul 9, 2024
1 parent 11005da commit b89021b
Show file tree
Hide file tree
Showing 4 changed files with 184 additions and 0 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# QField Geocoder Addresses Plugin

This [QField](https://github.com/opengisch/QField/) plugin search addresses and center to current map.
36 changes: 36 additions & 0 deletions icon.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
138 changes: 138 additions & 0 deletions main.qml
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
import QtQuick
import QtQuick.Controls

import org.qfield
import org.qgis
import Theme

import "qrc:/qml" as QFieldItems

Item {
id: plugin

property var mainWindow: iface.mainWindow()
property var mapCanvas: iface.findItemByObjectName('mapCanvas')
property var overlayFeatureFormDrawer: iface.findItemByObjectName('overlayFeatureFormDrawer')
property var locatorItem: iface.mainWindow().contentItem.children[4].children[9]
property var searchFieldRect: iface.mainWindow().contentItem.children[4].children[9].children[0]
property var resultBox: iface.mainWindow().contentItem.children[4].children[9].children[5]
property var geommetryHightLigth: iface.findItemByObjectName('mapCanvas').children[15]

Component.onCompleted: {
locatorAddressItem.parent = locatorItem
}

Rectangle {
id: locatorAddressItem
z: 1
width: searchFieldRect.width - 24
height: searchFieldRect.visible ? 30+32 : 0
anchors.top: resultBox.bottom
anchors.left: searchFieldRect.left
anchors.topMargin: resultBox.height == 0 ? 24 : 0
color: Theme.mainBackgroundColor
visible: searchFieldRect.visible && searchFieldRect.children[0].displayText !== ''
clip: true

Rectangle {
id:rectTitle
height:30
width:parent.width
anchors.top:parent.top
color: Theme.mainColor
opacity: 0.95

Text {
id: nameCell
anchors.left: parent.left
anchors.right: parent.right
text: 'Search for location address'
leftPadding: 5
topPadding: 8
font.bold: false
font.pointSize: Theme.resultFont.pointSize
color: "white"
elide: Text.ElideRight
horizontalAlignment: Text.AlignLeft
}
}
Rectangle {
anchors.left: parent.left
anchors.top: rectTitle.bottom
height:32
width:parent.width
color: Theme.mainBackgroundColor

Text {
id: addressCell
anchors.left: parent.left
anchors.right: parent.right
text: 'Got to '+ searchFieldRect.children[0].displayText + ' location'
leftPadding: 5
topPadding: 8
font.bold: false
font.pointSize: Theme.resultFont.pointSize
color: Theme.mainTextColor
}
}

MouseArea {
id: mouseArea
anchors.left: parent.left
anchors.top: parent.top
anchors.bottom: parent.bottom
anchors.right: parent.right

onClicked: {
fetchGeocoder(searchFieldRect.children[0].displayText)
}
}
}

function fetchGeocoder(location)
{
let request = new XMLHttpRequest();

request.onreadystatechange = function() {
if (request.readyState === XMLHttpRequest.DONE)
{
var responseObject = JSON.parse(request.response)
for (const obj of responseObject) {
var displayPoint = GeometryUtils.reprojectPoint(GeometryUtils.point(obj.lon, obj.lat), CoordinateReferenceSystemUtils.wgs84Crs(), mapCanvas.mapSettings.destinationCrs);
// mapCanvas.mapSettings.setCenter(displayPoint)
var temp = [GeometryUtils.reprojectPoint(GeometryUtils.point(obj.boundingbox[2], obj.boundingbox[0]), CoordinateReferenceSystemUtils.wgs84Crs(), mapCanvas.mapSettings.destinationCrs),
GeometryUtils.reprojectPoint(GeometryUtils.point(obj.boundingbox[3], obj.boundingbox[1]), CoordinateReferenceSystemUtils.wgs84Crs(), mapCanvas.mapSettings.destinationCrs)
]
mapCanvas.mapSettings.setExtentFromPoints(temp, 125, true)
locatorItem.state = 'off'

if (obj.geojson && obj.geojson.type == 'Polygon')
{
let str = 'POLYGON (('
for (const coord of obj.geojson.coordinates[0]) {
var point = GeometryUtils.reprojectPoint(GeometryUtils.point(coord[0], coord[1]), CoordinateReferenceSystemUtils.wgs84Crs(), mapCanvas.mapSettings.destinationCrs);
str += point.x +' ' + point.y + ', '
}
str = str.substring(0, str.length - 2)
str += '))'
let geom = GeometryUtils.createGeometryFromWkt(str)
geommetryHightLigth.geometryWrapper.qgsGeometry = geom
geommetryHightLigth.geometryWrapper.crs = mapCanvas.mapSettings.destinationCrs
} else {
let str = 'POINT (' + displayPoint.x + ' ' + displayPoint.y+')'
let geom = GeometryUtils.createGeometryFromWkt(str)
geommetryHightLigth.geometryWrapper.qgsGeometry = geom
geommetryHightLigth.geometryWrapper.crs = mapCanvas.mapSettings.destinationCrs
}
mainWindow.displayToast(obj.display_name)
break

}

}
}
request.open("GET", "https://nominatim.openstreetmap.org/search.php?q="+location+"&polygon_geojson=1&format=jsonv2")
request.setRequestHeader('User-Agent', 'FAKE-USER-AGENT');
request.send();
}
}
7 changes: 7 additions & 0 deletions metadata.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[general]
name=Address Geocoder using nominatim
description=Search addresses in search bar.
author=Quentin Savoye
icon=icon.svg
version=1.0
homepage=https://github.com/qsavoye

0 comments on commit b89021b

Please sign in to comment.