代码拉取完成,页面将自动刷新
import QtQuick 2.9
import QtQuick.Controls 1.4
import QtLocation 5.6
import QtPositioning 5.6
import QtQuick.Controls 2.4
import Qt.labs.settings 1.0
import Qt.labs.platform 1.0
import QtQuick.Window 2.12
import QtQml 2.12
ApplicationWindow{
id:window
visible:true
height:740;
width:360
title:qsTr("Map")
//拖动窗口大小图元位置不能实时跟随,做此下策
onWidthChanged: map.zoomLevel+=0.000001
onHeightChanged: map.zoomLevel-=0.000001
// 加载openstreetmap地图
Plugin{
id:mapPlugin
name:"osm"
// PluginParameter {
// name:"osm.mapping.offline.directory"
// value:"/User/Xiao/osmdata/" //测试了下好像相对路径不行,得用绝对路径
// }
}
// RouteQuery
RouteQuery{
id:routeQuery
}
RouteModel{
id:routeModel
plugin: mapPlugin
query:routeQuery
autoUpdate: false
onStatusChanged: {
if (status === RouteModel.Ready)
{
if(count === 0)
{
console.log("route count 0 ", routeModel.errorString)
}
else
{
map.drawRouteLine()
resultPage.displayForm(routeModel)
routeListView.showRouteInfo(routeModel)
}
}
else if (status === RouteModel.Error)
{
console.log(" error ", routeModel.errorString)
}
else if(status === RouteModel.Loading)
{
console.log("calculating route, waiting ....")
}
}
}
PositionSource {
id: positionSource
updateInterval: 1000
active: true
}
Map{
id:map
width:parent.width
anchors.fill:parent
plugin:mapPlugin
visible:false
center:QtPositioning.coordinate(39.98, 116.33) // 39.98, 116.33北京 30.70, 104.08 成都
zoomLevel: 13
minimumZoomLevel: 1
gesture.enabled: true
activeMapType: supportedMapTypes[0]
color:"white" //没有正常加载时显示的图块颜色
opacity: 0.99 //当其他控件获取焦点时,地图有白块
copyrightsVisible:false //左下角的地图版权相关文本显示开关
//onCopyrightLinkActivated: Qt.openUrlExternally(link)
property variant scaleLengths: [5, 10, 20, 50, 100, 200, 500, 1000, 2000, 5000, 10000, 20000, 50000, 100000, 200000, 500000, 1000000, 2000000]
property alias geocodeModel: geocodeModel
signal geocodeFinished()
//signal showGeocodeInfo()
GeocodeModel {
id: geocodeModel
plugin: map.plugin
property bool searchFlag:true // 判断用于搜索位置 or 获取地址信息
onStatusChanged:
{
//m1.tipText=status.toString() // 弹框提示信息
map.clearSearch()
if ((status == GeocodeModel.Ready) || (status == GeocodeModel.Error))
{
if (geocodeModel.status == GeocodeModel.Ready)
{
if (map.geocodeModel.count == 0)
{
console.log(qsTr("Unsuccessful geocode"))
}
else
{
if(searchFlag)
{
if(searchResListView.neededShow)
{
searchResListView.visible=true
searchResListView.showSearchResultnfo(geocodeModel)
}
}
else
{
//map.geocodeFinished()
pickInfoPage.setAddressText(geocodeModel.get(0).address.text)
}
}
}
else if (geocodeModel.status == GeocodeModel.Error)
{
console.log(qsTr("Geocode Error"), geocodeModel.errorString )
// 弹框提示信息
//m1.tipText=geocodeModel.errorString + cppTool.getSSLInfo();
}
}
else if(status===GeocodeModel.Loading)
{
console.log(qsTr("Geocode searching...") )
}
//map.geocodeFinished()
//m1.openMsg() // 弹框提示信息
}
// onLocationsChanged:
// {
// if (count == 1) {
// map.center.latitude = get(0).coordinate.latitude
// map.center.longitude = get(0).coordinate.longitude
// }
// }
}
Component {
id: pointDelegate
// 改为画一个点
MapQuickItem{
id: searchPoint
zoomLevel: 0 //缩放等级默认0固定大小,否则会和缩放等级一起放大缩小
visible:false
width:25; height:25
coordinate: locationData.coordinate //指示的坐标点
anchorPoint: Qt.point(sourceItem.width/2,sourceItem.height/2)//sourceItem左上角相对于coordinate的偏移
sourceItem: Image{
sourceSize.height: 25
sourceSize.width: 25
source: "../resources/marker.png"
}
}
// 画一个圆
// MapCircle {
// id: point
// radius: 1000
// color: "#46a2da"
// border.color: "#190a33"
// border.width: 2
// smooth: true
// opacity: 0.25
// center: locationData.coordinate
// MouseArea {
// anchors.fill:parent
// id: circleMouseArea
// hoverEnabled: false
// property variant lastCoordinate
// onPressed : {
// console.log("circle press")
// map.lastX = mouse.x + parent.x
// map.lastY = mouse.y + parent.y
// map.pressX = mouse.x + parent.x
// map.pressY = mouse.y + parent.y
// lastCoordinate = map.toCoordinate(Qt.point(mouse.x, mouse.y))
// }
// onPositionChanged: {
// if (Math.abs(map.pressX - parent.x- mouse.x ) > map.jitterThreshold ||
// Math.abs(map.pressY - parent.y -mouse.y ) > map.jitterThreshold) {
// if (pressed) parent.radius = parent.center.distanceTo(
// map.toCoordinate(Qt.point(mouse.x, mouse.y)))
// }
// if (mouse.button == Qt.LeftButton) {
// map.lastX = mouse.x + parent.x
// map.lastY = mouse.y + parent.y
// }
// }
// onPressAndHold:{
// if (Math.abs(map.pressX - parent.x- mouse.x ) < map.jitterThreshold
// && Math.abs(map.pressY - parent.y - mouse.y ) < map.jitterThreshold) {
// showPointMenu(lastCoordinate);
// }
// }
// }
// }
}
MapItemView {
model: geocodeModel
//delegate: pointDelegate
}
/*
//在地图上显示任意Qt Quick对象 需要在map内部
MapQuickItem{
id: point_1
//缩放等级默认0固定大小,否则会和缩放等级一起放大缩小
zoomLevel: 0
//指示的坐标点
coordinate: QtPositioning.coordinate(30.70, 104.0801)
//sourceItem左上角相对于coordinate的偏移
anchorPoint: Qt.point(sourceItem.width/2,sourceItem.height/2)
//Qt Quick对象
sourceItem: Rectangle{
width: 14
height: 14
radius: 7
color: "green"
border.color: "red"
border.width: 1
MouseArea{
anchors.fill: parent
onClicked: console.log("click")
}
//coordinate类型有经纬度高度三个属性
//latitude 纬度
//longitude 经度
//altitude 海拔高度,单位米
//以及计算距离和方位角的方法
//这里用文本显示两个点的距离
Text{
text: " "+Math.round(point_1.coordinate.distanceTo(point_2.coordinate))/1000+" km"
color: "green"
font.bold: true
font.pixelSize: 16
}
}
}
// 另一个点
MapQuickItem{
id: point_2
zoomLevel: 10 //和缩放等级一起放大缩小
coordinate: QtPositioning.coordinate(30.67, 104.04)
anchorPoint: Qt.point(sourceItem.width/2,sourceItem.height/2)
sourceItem: Rectangle{
width: 14
height: 14
radius: 7
color: "green"
border.color: "red"
border.width: 1
}
}
//MapCircle,MapRectangle,MapPolygon等图元
//多边形
MapPolygon{
id: poly_3
color: "blue"
border.width: 1
border.color: "red"
//根据坐标点绘制多边形
path:[QtPositioning.coordinate(30.704, 104.0803)]
Component.onCompleted: {
poly_3.addCoordinate(QtPositioning.coordinate(30.72, 104.08))
poly_3.addCoordinate(QtPositioning.coordinate(30.72, 104.10))
poly_3.addCoordinate(QtPositioning.coordinate(30.69, 104.12))
}
}
// 绘制折线
MapPolyline{
line.width: 2
line.color: "green"
//路径列表 path : list<coordinate>
path: [point_1.coordinate,point_2.coordinate,poly_3.path[0]]
}
// 上方显示信息
Rectangle{
x: 10
y: 10
height: item_center.height+20
width: item_center.width+20
color: "yellow" //左侧上方矩形框
Text{
id: item_center
x: 10
y: 10
color: "black"
font.pixelSize: 16
font.bold: true
//展示缩放等级 和 map的中心点经纬度
text: "zoom level:"+Math.floor(map.zoomLevel)+
" center:"+map.center.latitude.toFixed(6)+
" "+map.center.longitude.toFixed(6)
}
}
*/
// 绘制导航路线
MapItemView {
model: routeModel
delegate: MapRoute { // 使用自带的委托模式绘制 效率高
route: routeData
line.color: "blue"
line.width: 3
smooth: true
}
}
// MapPolyline{ // 通过依次增加点位置绘制 速度慢
// id: pathline
// line.width: 2
// line.color: "green"
// //路径列表 path : list<coordinate>
// path: []
// }
// 起点
MapQuickItem{
id: startPoint
width:26; height:30
zoomLevel: 0 //缩放等级默认0固定大小,否则会和缩放等级一起放大缩小
visible:false
coordinate: map.center //指示的坐标点
anchorPoint: Qt.point(sourceItem.width/2,sourceItem.height)//sourceItem左上角相对于coordinate的偏移
sourceItem: Image{
sourceSize.height: startPoint.height
sourceSize.width: startPoint.width
source: "../resources/startpos.png"
}
}
// 终点
MapQuickItem{
id: endPoint
zoomLevel: 0 //缩放等级默认0固定大小,否则会和缩放等级一起放大缩小
visible:false
width:26; height:30
coordinate: map.center //指示的坐标点
anchorPoint: Qt.point(sourceItem.width/2,sourceItem.height)//sourceItem左上角相对于coordinate的偏移
sourceItem: Image{
sourceSize.height: endPoint.height
sourceSize.width: endPoint.width
source: "../resources/endpos.png"
}
}
// 搜索项点
MapQuickItem{
id: searchedPoint
zoomLevel: 0 //缩放等级默认0固定大小,否则会和缩放等级一起放大缩小
visible:false
width:25; height:25
coordinate: map.center //指示的坐标点
anchorPoint: Qt.point(sourceItem.width/2,sourceItem.height)//sourceItem左上角相对于coordinate的偏移
sourceItem: Image{
sourceSize.height: searchedPoint.height
sourceSize.width: searchedPoint.width
source: "../resources/marker.png"
}
}
Component.onCompleted: {
navigatePage.calRouteSignal.connect(drawRouteLine)
checkTimer.start()
}
function clearRoute()
{
//pathline.path=[]
routeModel.reset()
routeQuery.clearWaypoints()
routeModel.update()
routeListView.visible=false
}
// 调整路线起止点图标位置和画布缩放
function drawRouteLine()
{
console.log("slot to draw line...")
/*
if(pathline.path.length>0)
{
pathline.path=[]
}
// console.log("routeModel.count = ", routeModel.count)
if (routeModel.count > 0)
{
//console.log("routeModel.get(0).segments.length = ", routeModel.get(0).segments.length)
for (var ii = 0; ii < routeModel.get(0).segments.length; ii++)
{
//console.log("routeModel.get(0).segments[", ii,"].maneuver " )
//console.log(routeModel.get(0).segments[ii].maneuver.direction )
//console.log(routeModel.get(0).segments[ii].maneuver.timeToNextInstruction)
//console.log(routeModel.get(0).segments[ii].maneuver.distanceToNextInstruction )
//console.log(routeModel.get(0).segments[ii].maneuver.position)
// console.log(routeModel.get(0).segments[ii].maneuver.instructionText)
// console.log("\nrouteModel.get(0).segments[", ii,"].distance ", routeModel.get(0).segments[ii].distance )
// console.log("\nrouteModel.get(0).segments[", ii,"].travelTime ", routeModel.get(0).segments[ii].travelTime)
// console.log("\nrouteModel.get(0).segments[", ii,"].path ", routeModel.get(0).segments[ii].path.length)
// console.log(routeModel.get(0).segments[ii].path )
// 添加到规划线的点集中
for(var j=0;j<routeModel.get(0).segments[ii].path.length;j++)
{
pathline.addCoordinate(routeModel.get(0).segments[ii].path[j])
}
}
}
*/
var _midP = calMiddlePoint(navigatePage.startPos, navigatePage.endPos)
map.center = _midP
var _dzoom = calZoomNum(navigatePage.startPos, navigatePage.endPos)
map.zoomLevel=_dzoom
map.setStartPointVisible(true)
map.setEndPointVisible(true)
}
function setStartPointVisible(_flag)
{
startPoint.visible=_flag
}
function setEndPointVisible(_flag)
{
endPoint.visible=_flag
}
function changeStartPointCoord(_newpos)
{
startPoint.coordinate=_newpos
}
function changeEndPointCoord(_newpos)
{
endPoint.coordinate=_newpos
}
function setSearchItemVisible(_flag)
{
searchedPoint.visible=_flag
}
function changeSearchItemCoord(_pos)
{
searchedPoint.coordinate=_pos
}
// 获取当前位置
function getMyLocalePos()
{
var _pos = positionSource.position.coordinate
return _pos
}
// 根据两点计算中间点
function calMiddlePoint(_a, _b)
{
var _deg =_a.azimuthTo(_b)
var _dist=_a.distanceTo(_b)
var _midPos = _a.atDistanceAndAzimuth(_dist*0.5, _deg)
return _midPos
}
// 计算缩放比例
function calZoomNum(_a, _b)
{
var maxLon=_a.longitude >=_b.longitude?_a.longitude : _b.longitude
var maxLat=_a.latitude >= _b.latitude ? _a.latitude : _b.latitude
var minLon=_a.longitude <_b.longitude?_a.longitude : _b.longitude
var minLat=_a.latitude < _b.latitude ? _a.latitude : _b.latitude
var coord1 = QtPositioning.coordinate(minLat, minLon)
var coord2 = QtPositioning.coordinate(maxLat, maxLon)
var dist = Math.round(coord1.distanceTo(coord2))
for (var i = 0,zoomLen = scaleLengths.length; i < zoomLen; i++)
{
if (scaleLengths[i]-dist>0 )
{
return 18-i+3 //之所以会多3,是因为地图范围常常是比例尺距离的10倍以上。所以级别会增加3。
}
}
return 18
}
// 搜索位置执行
function geocode(fromAddress)
{
// send the geocode request
if(geocodeModel.status === GeocodeModel.Ready)
{
geocodeModel.query = fromAddress
geocodeModel.update()
}
else
{
geocodeModel.reset()
geocodeModel.query = fromAddress
geocodeModel.update()
}
}
// 输出搜索匹配位置的信息
function geocodeMessage(_geo)
{
var street, district, city, county, state, countryCode, country, postalCode, latitude, longitude, text
latitude = Math.round(_geo.coordinate.latitude * 10000) / 10000
longitude = Math.round(_geo.coordinate.longitude * 10000) / 10000
street = _geo.address.street
district = _geo.address.district
city = _geo.address.city
county = _geo.address.county
state = _geo.address.state
countryCode = _geo.address.countryCode
country = _geo.address.country
postalCode = _geo.address.postalCode
text = "Latitude : " + latitude +"\n"
text +="Longitude: " + longitude + "\n"
if (street) text +="Street: "+ street + "\n"
if (district) text +="District: "+ district +"\n"
if (city) text +="City: "+ city + "\n"
if (county) text +="County: "+ county + "\n"
if (state) text +="State: "+ state + "\n"
if (countryCode) text +="Country code: "+ countryCode + "\n"
if (country) text +="Country: "+ country + "\n"
if (postalCode) text +="PostalCode: "+ postalCode + "\n"
//console.log("message : ", text)
return text
}
// 清除上一次结果
function clearSearch()
{
map.clearSearchResDistanceList()
searchResListView.clearData()
}
// 调用C++ 工具接口
function clearSearchResDistanceList()
{
cppTool.clearSearchResDistanceList()
}
// 调用C++ 工具接口
function getListIndex(_dist)
{
return cppTool.insertAndGetIndex(_dist)
}
function routeInfo2Chinese(_rstr)
{
return cppTool.translateString2Chinese(_rstr)
}
function printlog()
{
var _s="";
for (var i = 0; i<map.supportedMapTypes.length; i++)
{
_s+=map.supportedMapTypes[i].name+"; "
console.log("map type : ", map.supportedMapTypes[i].name)
}
_s+=" ; error = ";
_s+=map.errorString
m1.tipText=_s;
m1.openMsg()
}
// onZoomLevelChanged:
// {
// console.log("##### current zoomlevel = ", map.zoomLevel)
// }
MouseArea{
id:mousepress
anchors.fill: parent
//onClicked: {
// console.log("clicked:",mouse.button);
// m1.tipText=Screen.width.toString()+","+Screen.height.toString() // 弹框提示信息
// m1.openMsg()
//}
//onPositionChanged:
//{
// console.log("position changed ...")
//}
//onPressAndHold: {
//console.log("onPressAndHold ... ")
// if(pickInfoPage.visible && navigatePage.isPicking)
// {
// var _p = map.toCoordinate(Qt.point(mouse.x,mouse.y))
// if(navigatePage.isStart)
// {
// map.changeStartPointCoord(_p)
// }
// else
// {
// map.changeEndPointCoord(_p)
// }
// }
//}
// 地图选点时 鼠标抬起时获取点击处位置信息
onReleased:{
if(pickInfoPage.visible && navigatePage.isPicking)
{
var _cp = map.toCoordinate(Qt.point(mouse.x,mouse.y))
if(navigatePage.isStart)
{
navigatePage.startPos=_cp
map.changeStartPointCoord(_cp)
}
else
{
navigatePage.endPos=_cp
map.changeEndPointCoord(_cp)
}
map.geocode(_cp)
console.log("onReleased done ", _cp)
}
}
}
}//end Map
// 需要先加载地图 后加载控件 否则地图会覆盖控件看不到
// 默认显示搜索页
SearchPage{
id:searchPage
width:360-10*2
height:50
x: 10 ; y: 114/3
}
// 默认不显示导航页
NavigatePage{
id:navigatePage
width:360
height:143
visible:false
x: 0 ; y: 0
}
// 地图选导航起止点时显示的点信息界面
// 默认不显示
PickInfoForm{
id:pickInfoPage
//width:360; height:180/3
x:0; y:parent.height-80
visible:false
}
PickToolTipForm{
id:pickHeadPage
width: 360; height: 150/3
//x: 0; y:80/3
visible: false
}
// 路线计算结果界面
ResultPage{
id:resultPage
width:360; height:40
x: 0; y:parent.height-height
visible: false
}
// 导航路线详情显示
RouteList{
id:routeListView
width:360-10*2; height:parent.height-300/3
x: 10; y:300/3
visible: false
}
// 搜索结果列表
SearchResultList{
id: searchResListView
width:360-10*2; height:parent.height-y
x: 10; y:114/3+150/3+5
visible:false
}
// 导航路线按钮
Button{
id:daohangBtn
x: 360-50-10;
y: parent.height-(50+44)-30
width:50; height:50
background:Rectangle{
implicitHeight: daohangBtn.height
implicitWidth: daohangBtn.width
radius:6
color: "transparent" //设置背景透明,否则会出现默认的白色背景 // "#dfffffff"
//border.color: "black"
//border.width: 1
BorderImage{ //按钮的背景图片
anchors.fill: parent
source:"../resources/daohang.png"
// source: control.hovered ? (control.pressed ? pressPic : hoverPic) : nomerPic;
}
}
onClicked: {
//隐藏第一个界面 显示第二个界面
searchPage.visible=false
navigatePage.visible=true
//navigatePage.height=420
navigatePage.showPosEditForm(false)
navigatePage.hideRouteInfoForm()
navigatePage.initStartPos()
map.setStartPointVisible(false)
map.setEndPointVisible(false)
map.setSearchItemVisible(false)
//
geocodeModel.searchFlag=false
// 按钮显隐
daohangBtn.visible=false
myLocateBtn.visible=true
}
}
// 我的位置按钮
Button{
id:myLocateBtn
x: 360-50-10;
y: parent.height-(50+44)-14-50-30
width:50; height:50
background:Rectangle{
implicitHeight: daohangBtn.height
implicitWidth: daohangBtn.width
radius:6
color: "transparent" //设置背景透明,否则会出现默认的白色背景 //"#dfffffff"
BorderImage{ //按钮的背景图片
anchors.fill: parent
source:"../resources/dingwei-2.png"
// source: control.hovered ? (control.pressed ? pressPic : hoverPic) : nomerPic;
}
}
onClicked: {
map.center = map.getMyLocalePos()
}
}
// 弹框提示信息
AutoResize{
id:m1
x:0; y:300
width:360-10*2;
height:300
}
// 开屏引导图
Image{
id: initImg
width:parent.width;
height: parent.height
fillMode: Image.PreserveAspectFit
visible: true
source:"../resources/kaiping.png"
}
Timer{
id:checkTimer;
interval: 3000; //定时周期
//repeat:true; //重复定时
//triggeredOnStart: true; //启动就触发槽
//running: true //启动即隐式调用start
onTriggered: {
console.log("stop timer...")
initImg.visible=false
map.visible=true
checkTimer.stop()
}
}
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。