资源简介
Qt调用百度地图API,这里需要注意需要去百度地图官网上下载一个允许访问的码,修改到对应的map.html,就可以访问到地图,可以用于二次开发
代码片段和文件信息
#include “cmaplocation.h“
#include “ui_cmaplocation.h“
CMapLocation::CMapLocation(QWidget *parent) :
QWidget(parent)
ui(new Ui::CMapLocation)
{
ui->setupUi(this);
ui->View->settings()->setAttribute(QWebSettings::PluginsEnabled true);
ui->View->settings()->setAttribute(QWebSettings::javascriptEnabled true);
ui->View->settings()->setAttribute(QWebSettings::DeveloperExtrasEnabled true);
ui->View->settings()->setAttribute(QWebSettings::javascriptCanOpenWindows true);
ui->View->settings()->setAttribute(QWebSettings::JavaEnabled true);
QString dirStr = “file:///“;
dirStr += QDir::currentPath();
dirStr = dirStr.replace(“\\“ “/“);
dirStr += “/map.html“;
QUrl url(dirStr);
ui->View->setUrl(url);
ui->View->show();
ui->lineEdit->setText(“22.5415“);
ui->lineEdit_2->setText(“114.061“);
}
CMapLocation::~CMapLocation()
{
delete ui;
}
void CMapLocation::setLocation(double latit double Longitude)
{
QWebframe *frame = ui->View->page()->mainframe();
QString cmd = QString(“showAddress(\“%1\“\“%2\“);“).arg(latit).arg(Longitude); //定位 不连续
frame->evaluatejavascript(cmd);
}
//
void CMapLocation::on_pushButton_clicked()
{
QString str;
str = ui->lineEdit->text();
double latit = str.toDouble();
str = ui->lineEdit_2->text();
double Longitude = str.toDouble();
setLocation(latitLongitude);
}
//
void CMapLocation::on_pushButton_more_clicked()
{
QString str;
str = ui->lineEdit->text();
double latit = str.toDouble();
str = ui->lineEdit_2->text();
double Longitude = str.toDouble();
QWebframe *frame = ui->View->page()->mainframe();
QString cmd = QString(“addAddress(\“%1\“\“%2\“);“).arg(latit).arg(Longitude); //多点定位 连线
frame->evaluatejavascript(cmd);
}
//
void CMapLocation::on_Button_refresh_clicked()
{
QString dirStr = “file:///“;
dirStr += QDir::currentPath();
dirStr = dirStr.replace(“\\“ “/“);
dirStr += “/map.html“;
QUrl url(dirStr);
ui->View->setUrl(url);
ui->View->show();
}
评论
共有 条评论