步骤
1.获取 API
我用的是百度地图的 API
QString url_msg = QString("http://api.map.baidu.com/staticimage?center=&markers=113.25,23.13&zoom=%1&width=800&height=480").arg(zoom);
参数:
- markers: 坐标(经度,纬度)
- zoom:放大倍数
- width:图片显示宽度
- height:图片显示高度
这个API的优点是:不用申请 token
图片显示的位置默认是广州(我代码里面的
2. 发送网络请求
//搜索位置
void MapWindow::on_pushButton_search_clicked()
{
QString url_msg = QString("http://api.map.baidu.com/staticimage?center=&markers=113.25,23.13&zoom=%1&width=800&height=480").arg(zoom);
QNetworkRequest request;
//将图片存储为 map.png
file = new QFile("map.png");
file->open(QIODevice::WriteOnly);
request.setUrl(url_msg);
//发送网络请求
reply = manager->get(request);
//准备接收网络信息
QObject::connect(reply, &QIODevice::readyRead, this, &MapWindow::get_read);
//加载数据,加载完毕后显示地图图片
QObject::connect(reply, &QNetworkReply::downloadProgress, this, &MapWindow::get_progress);
//使能放大、缩小按钮
ui->pushButton_zoom_in->setEnabled(true);
ui->pushButton_zoom_out->setEnabled(true);
}
3. 处理网络响应得到的图片
//加载数据
void MapWindow::get_progress(qint64 bytesReceived, qint64 bytesTotal)
{
qDebug() << "All:" << bytesTotal << endl << "Received:" << bytesReceived;
if(bytesReceived == bytesTotal){
qDebug() << "加载完毕";
file->close();
emit display_pic();
}
}
//显示图片
void MapWindow::display_pic_slot()
{
this->setStyleSheet("MapWindow{border-image: url(./map.png);}");
}
4.设置“图片放大\缩小”按钮
//地图放大
void MapWindow::on_pushButton_zoom_in_clicked()
{
if(zoom >= 19)
return;
zoom++;
show_map();
if(zoom == 19)
ui->pushButton_zoom_in->setEnabled(false);
ui->pushButton_zoom_out->setEnabled(true);
}
//地图缩小
void MapWindow::on_pushButton_zoom_out_clicked()
{
if(zoom <= 4)
return;
--zoom;
show_map();
if(zoom == 4)
ui->pushButton_zoom_out->setEnabled(false);
ui->pushButton_zoom_in->setEnabled(true);
}
设置拖到鼠标或者是点击屏幕 实现地点显示变化
//事件过滤器
bool MapWindow::eventFilter(QObject *watched, QEvent *event)
{
if(watched == this){
QTouchEvent *touch = static_cast<QTouchEvent*>(event);
switch (event->type()) {
//鼠标按下事件
case QEvent::MouseButtonPress:
pres_flag = 1;
//记录按下点的 x 、 y 坐标
last_point.setY(cursor().pos().y());
last_point.setX(cursor().pos().x());
break;
//鼠标松开事件
case QEvent::MouseButtonRelease:
rele_flag = 1;
//记录按下点的 x 、 y 坐标
point.setY(cursor().pos().y());
point.setX(cursor().pos().x());
break;
//触摸事件
case QEvent::TouchBegin:
//记录按下点的 x 、 y 坐标
point.setY(touch->touchPoints().at(0).pos().y());
point.setX(touch->touchPoints().at(0).pos().x());
if(point.x() < 500 || point.y() < 350){
rele_flag = 1;
pres_flag = 1;
}
event->accept();
break;
default:
break;
}
}
if(pres_flag == 1 && rele_flag == 1){
pres_flag = 0;
rele_flag = 0;
//触摸事件,算出移动的数据
if(event->type() == QEvent::TouchBegin){
m_x = 400 - point.x();
m_y = 240 - point.y();
}
//鼠标事件,算出移动的数据
else{
m_x = point.x() - last_point.x();
m_y = point.y() - last_point.y();
}
if(m_x > 10 || m_x < -10 || m_y > 10 || m_y < -10){
//计算需要偏移多少东经北纬
move_x = m_x*0.0000003*(19-zoom)*(19-zoom)*(19-zoom)*(19-zoom);
move_y = m_y*0.0000003*(19-zoom)*(19-zoom)*(19-zoom)*(19-zoom);
pos_x -= move_x;
pos_y += move_y;
show_map();
}
}
//将事件传递给父类
return QWidget::eventFilter(watched,event);
}