1 /****************************************************************************
2 **
3 ** Copyright (C) 2014 Canonical Ltd.
4 ** Contact: http://www.qt.io/licensing/
5 **
6 ** This file is part of the QtLocation module of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL3$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and The Qt Company. For licensing terms
14 ** and conditions see http://www.qt.io/terms-conditions. For further
15 ** information use the contact form at http://www.qt.io/contact-us.
16 **
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 3 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPLv3 included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 3 requirements
23 ** will be met: https://www.gnu.org/licenses/lgpl.html.
24 **
25 ** GNU General Public License Usage
26 ** Alternatively, this file may be used under the terms of the GNU
27 ** General Public License version 2.0 or later as published by the Free
28 ** Software Foundation and appearing in the file LICENSE.GPL included in
29 ** the packaging of this file. Please review the following information to
30 ** ensure the GNU General Public License version 2.0 requirements will be
31 ** met: http://www.gnu.org/licenses/gpl-2.0.html.
32 **
33 ** $QT_END_LICENSE$
34 **
35 ****************************************************************************/
36
37 #include "qgeotilefetchermapbox.h"
38 #include "qgeomapreplymapbox.h"
39 #include "qmapboxcommon.h"
40
41 #include <QtNetwork/QNetworkAccessManager>
42 #include <QtNetwork/QNetworkRequest>
43 #include <QtLocation/private/qgeotilespec_p.h>
44 #include <QDebug>
45
46 QT_BEGIN_NAMESPACE
47
QGeoTileFetcherMapbox(int scaleFactor,QGeoTiledMappingManagerEngine * parent)48 QGeoTileFetcherMapbox::QGeoTileFetcherMapbox(int scaleFactor, QGeoTiledMappingManagerEngine *parent)
49 : QGeoTileFetcher(parent), m_networkManager(new QNetworkAccessManager(this)),
50 m_userAgent(mapboxDefaultUserAgent),
51 m_format("png"),
52 m_replyFormat("png"),
53 m_accessToken("")
54 {
55 m_scaleFactor = qBound(1, scaleFactor, 2);
56 }
57
setUserAgent(const QByteArray & userAgent)58 void QGeoTileFetcherMapbox::setUserAgent(const QByteArray &userAgent)
59 {
60 m_userAgent = userAgent;
61 }
62
setMapIds(const QVector<QString> & mapIds)63 void QGeoTileFetcherMapbox::setMapIds(const QVector<QString> &mapIds)
64 {
65 m_mapIds = mapIds;
66 }
67
setFormat(const QString & format)68 void QGeoTileFetcherMapbox::setFormat(const QString &format)
69 {
70 m_format = format;
71
72 if (m_format == "png" || m_format == "png32" || m_format == "png64" || m_format == "png128" || m_format == "png256")
73 m_replyFormat = "png";
74 else if (m_format == "jpg70" || m_format == "jpg80" || m_format == "jpg90")
75 m_replyFormat = "jpg";
76 else
77 qWarning() << "Unknown map format " << m_format;
78 }
79
setAccessToken(const QString & accessToken)80 void QGeoTileFetcherMapbox::setAccessToken(const QString &accessToken)
81 {
82 m_accessToken = accessToken;
83 }
84
getTileImage(const QGeoTileSpec & spec)85 QGeoTiledMapReply *QGeoTileFetcherMapbox::getTileImage(const QGeoTileSpec &spec)
86 {
87 QNetworkRequest request;
88 request.setRawHeader("User-Agent", m_userAgent);
89
90 request.setUrl(QUrl(mapboxTilesApiPath +
91 ((spec.mapId() >= m_mapIds.size()) ? QStringLiteral("mapbox.streets") : m_mapIds[spec.mapId() - 1]) + QLatin1Char('/') +
92 QString::number(spec.zoom()) + QLatin1Char('/') +
93 QString::number(spec.x()) + QLatin1Char('/') +
94 QString::number(spec.y()) +
95 ((m_scaleFactor > 1) ? (QLatin1Char('@') + QString::number(m_scaleFactor) + QLatin1String("x.")) : QLatin1String(".")) +
96 m_format + QLatin1Char('?') +
97 QStringLiteral("access_token=") + m_accessToken));
98
99 QNetworkReply *reply = m_networkManager->get(request);
100
101 return new QGeoMapReplyMapbox(reply, spec, m_replyFormat);
102 }
103
104 QT_END_NAMESPACE
105