问题描述
我正在尝试在 QT 中做离线地图。我想将我的瓷砖放置到我的地图上。我只是放置了一个图块,它并没有完成地图。它在顶部和底部显示相同的内容。我该怎么做?
我在 qrc 中有所有这些。但是,如果有一种方法可以像 "http://tile.openstreetmap.org/%1/%2/%3.png" 那样针对我想去的地方,我想知道。
这是一个简单的代码脚本
#include "slippymap.h"
#include <math.h>
#include <QtWidgets>
#include <QtNetwork>
#ifndef M_PI
#define M_PI 3.14159265358979323846
#endif
uint qHash(const QPoint& p)
{
return p.x() * 17 ^ p.y();
}
// tile size in pixels
const int tdim = 256;
QPointF tileForCoordinate(qreal lat,qreal lng,int zoom)
{
qreal zn = static_cast<qreal>(1 << zoom);
qreal tx = (lng + 180.0) / 360.0;
qreal ty = (1.0 - log(tan(lat * M_PI / 180.0) +
1.0 / cos(lat * M_PI / 180.0)) / M_PI) / 2.0;
return QPointF(tx * zn,ty * zn);
}
qreal longitudeFromTile(qreal tx,int zoom)
{
qreal zn = static_cast<qreal>(1 << zoom);
qreal lat = tx / zn * 360.0 - 180.0;
return lat;
}
qreal latitudeFromTile(qreal ty,int zoom)
{
qreal zn = static_cast<qreal>(1 << zoom);
qreal n = M_PI - 2 * M_PI * ty / zn;
qreal lng = 180.0 / M_PI * atan(0.5 * (exp(n) - exp(-n)));
return lng;
}
SlippyMap::SlippyMap(QObject *parent)
:QObject(parent),width(400),height(300),zoom(10),latitude(12.89542),longitude(77.56534)
{
m_emptyTile = QPixmap(tdim,tdim);
m_emptyTile.fill(Qt::lightGray);
QNetworkDiskCache *cache = new QNetworkDiskCache;
cache->setCacheDirectory(QStandardPaths::writableLocation(QStandardPaths::CacheLocation));
m_manager.setCache(cache);
connect(&m_manager,SIGNAL(finished(QNetworkReply*)),this,SLOT(handleNetworkData(QNetworkReply*)));
}
void SlippyMap::invalidate()
{
if (width <= 0 || height <= 0)
return;
QPointF ct = tileForCoordinate(latitude,longitude,zoom);
qreal tx = ct.x();
qreal ty = ct.y();
// top-left corner of the center tile
int xp = width / 2 - (tx - floor(tx)) * tdim;
int yp = height / 2 - (ty - floor(ty)) * tdim;
// first tile vertical and horizontal
int xa = (xp + tdim - 1) / tdim;
int ya = (yp + tdim - 1) / tdim;
int xs = static_cast<int>(tx) - xa;
int ys = static_cast<int>(ty) - ya;
// offset for top-left tile
m_offset = QPoint(xp - xa * tdim,yp - ya * tdim);
// last tile vertical and horizontal
int xe = static_cast<int>(tx) + (width - xp - 1) / tdim;
int ye = static_cast<int>(ty) + (height - yp - 1) / tdim;
// build a rect
m_tilesRect = QRect(xs,ys,xe - xs + 1,ye - ys + 1);
if (m_url.isEmpty())
download();
emit updated(QRect(0,width,height));
}
void SlippyMap::render(QPainter *p,const QRect &rect)
{
int mx = 0,my = 0; // x and y for marker
qreal latdiff,lngdiff; // storing reminder
QPointF center = tileForCoordinate(latitude,zoom); // getting tile points for real gps point
int cx = qFloor(center.x());
int cy = qFloor(center.y());
for (int x = 0; x <= m_tilesRect.width(); ++x)
for (int y = 0; y <= m_tilesRect.height(); ++y) {
QPoint tp(x + m_tilesRect.left(),y + m_tilesRect.top());
QRect box = tileRect(tp);
if (tp.x() == cx && tp.y() == cy) {
mx = box.x();
my = box.y();
latdiff = qAbs(tp.x() - center.x());
lngdiff = qAbs(tp.y() - center.y());
}
if (rect.intersects(box)) {
if (m_tilePixmaps.contains(tp))
p->drawPixmap(box,m_tilePixmaps.value(tp));
else
p->drawPixmap(box,m_emptyTile);
}
}
// marker
if (mx != 0 && my != 0) {
QIcon marker = QIcon(":/icons/map.png");
// int dx= (mx+(latdiff*256.0)-40);
// int dy= (my+(lngdiff*256.0)-40);
// for(int i=0;i<=100;i++)
// {
p->drawPixmap((mx+(latdiff*256.0)-40),(my+(lngdiff*256.0)-40),marker.pixmap(40,40));
// p->drawPixmap((dx+100),(dy+100),40));
// p->drawPixmap((dx+50),(dy+50),40));
// }
}
}
void SlippyMap::pan(const QPoint &delta)
{
QPointF dx = QPointF(delta) / qreal(tdim);
QPointF center = tileForCoordinate(latitude,zoom) - dx;
latitude = latitudeFromTile(center.y(),zoom);
longitude = longitudeFromTile(center.x(),zoom);
invalidate();
}
void SlippyMap::handleNetworkData(QNetworkReply *reply)
{
QImage img;
QPoint tp = reply->request().attribute(QNetworkRequest::User).toPoint();
QUrl url = reply->url();
if (!reply->error())
if (!img.load(reply,0))
img = QImage();
reply->deleteLater();
m_tilePixmaps[tp] = QPixmap::fromImage(img);
if (img.isNull())
m_tilePixmaps[tp] = m_emptyTile;
emit updated(tileRect(tp));
// purge unused spaces
QRect bound = m_tilesRect.adjusted(-2,-2,2,2);
foreach(QPoint tp,m_tilePixmaps.keys())
if (!bound.contains(tp))
m_tilePixmaps.remove(tp);
download();
}
void SlippyMap::download()
{
QPoint grab(0,0);
for (int x = 0; x <= m_tilesRect.width(); ++x)
for (int y = 0; y <= m_tilesRect.height(); ++y) {
QPoint tp = m_tilesRect.topLeft() + QPoint(x,y);
if (!m_tilePixmaps.contains(tp)) {
grab = tp;
break;
}
}
if (grab == QPoint(0,0)) {
m_url = QUrl();
return;
}
//osm formatında yüklemeyi dene
QString path = "http://tile.openstreetmap.org/%1/%2/%3.png";
m_url = QUrl(path.arg(zoom).arg(grab.x()).arg(grab.y()));
QNetworkRequest request;
request.setUrl(m_url);
request.setRawHeader("User-Agent","The Qt Company (Qt Graphics Dojo 1.0");
request.setAttribute(QNetworkRequest::User,QVariant(grab));
m_manager.get(request);
}
QRect SlippyMap::tileRect(const QPoint &tp)
{
QPoint t = tp - m_tilesRect.topLeft();
int x = t.x() * tdim + m_offset.x();
int y = t.y() * tdim + m_offset.y();
return QRect(x,y,tdim,tdim);
}
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)