summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorPaolo Angelelli <paolo.angelelli@theqtcompany.com>2016-06-15 22:50:21 +0200
committerPaolo Angelelli <paolo.angelelli@theqtcompany.com>2016-06-30 11:40:05 +0000
commit706f97d93b7e0e01bc287d9379e692ffd1bd08a6 (patch)
tree5d9d9aa54abee0902eae1e6f3a38adc9b2454fc5 /src
parente729fabed1b78729f65503ed3a7e0d5fddb13947 (diff)
downloadqtlocation-706f97d93b7e0e01bc287d9379e692ffd1bd08a6.tar.gz
Improve Mapbox geoservice plugin
This patch adds standard mapbox map modes so that they could be used out of the box without the need for specifying it as parameter, and also adds a QGeoFileTileCache to the plugin, since the Mapbox TOS allow a limited caching even in the free plan. The default cache max disk usage has been set accordingly. The mapbox.map_id parameter is now optional, meaning that things will work like before if present, but will work with the default mapbox styles if not. Change-Id: I4056a3706f47d7f5c46a873c135b743106647fc0 Reviewed-by: Topi Reiniƶ <topi.reinio@theqtcompany.com> Reviewed-by: Michal Klocek <michal.klocek@theqtcompany.com>
Diffstat (limited to 'src')
-rw-r--r--src/location/doc/src/plugins/mapbox.qdoc31
-rw-r--r--src/location/maps/qgeofiletilecache.cpp4
-rw-r--r--src/location/maps/qgeofiletilecache_p.h6
-rw-r--r--src/plugins/geoservices/mapbox/mapbox.pro6
-rw-r--r--src/plugins/geoservices/mapbox/qgeofiletilecachemapbox.cpp118
-rw-r--r--src/plugins/geoservices/mapbox/qgeofiletilecachemapbox.h63
-rw-r--r--src/plugins/geoservices/mapbox/qgeoserviceproviderpluginmapbox.cpp5
-rw-r--r--src/plugins/geoservices/mapbox/qgeotiledmappingmanagerenginemapbox.cpp113
-rw-r--r--src/plugins/geoservices/mapbox/qgeotiledmappingmanagerenginemapbox.h3
-rw-r--r--src/plugins/geoservices/mapbox/qgeotilefetchermapbox.cpp7
-rw-r--r--src/plugins/geoservices/mapbox/qgeotilefetchermapbox.h5
11 files changed, 337 insertions, 24 deletions
diff --git a/src/location/doc/src/plugins/mapbox.qdoc b/src/location/doc/src/plugins/mapbox.qdoc
index 6cd326ba..0b989218 100644
--- a/src/location/doc/src/plugins/mapbox.qdoc
+++ b/src/location/doc/src/plugins/mapbox.qdoc
@@ -53,9 +53,6 @@ The following table lists mandatory parameters that \e must be passed to the Map
\row
\li mapbox.access_token
\li \l{https://www.mapbox.com/help/define-access-token/}{Access token} provided by Mapbox.
-\row
- \li mapbox.map_id
- \li \l{https://www.mapbox.com/help/define-map-id/}{ID} of the Mapbox map to show. An example ID is "examples.map-zr0njcqy".
\endtable
The Mapbox geo services plugin requires an access token and map ID to use the
@@ -69,6 +66,14 @@ The following table lists optional parameters that can be passed to the Mapbox p
\li Parameter
\li Description
\row
+ \li mapbox.map_id
+ \li \l{https://www.mapbox.com/help/define-map-id/}{ID} of the Mapbox map to show. An example ID is "examples.map-zr0njcqy".
+ If this parameter is present, the specified map type will be used by default, unless another is selected.
+ If not present, the default Mapbox map ID is "mapbox.streets".
+\row
+ \li mapbox.additional_map_ids
+ \li Additional, comma separated, Mapbox map IDs to be added to the available map types.
+\row
\li mapbox.format
\li Data format to download tiles in, available values are "png", "png32",
"png64", "png128", "png256", (PNG with full, 32, 64, 128 and 256 color palette)
@@ -77,5 +82,25 @@ The following table lists optional parameters that can be passed to the Mapbox p
\row
\li useragent
\li User agent string set when making network requests.
+\row
+ \li mapbox.cache.directory
+ \li Absolute path to map tile cache directory used as network disk cache.
+
+ The default place for the cache is \c{QtLocation/mapbox} directory in \l {QStandardPaths::writableLocation()} {QStandardPaths::writableLocation}(\l{QStandardPaths::GenericCacheLocation}).
+ On systems that have no concept of a shared cache, the application-specific \l{QStandardPaths::CacheLocation} is used instead.
+\row
+ \li mapbox.cache.disk.size
+ \li Map tile disk cache size in bytes. Default size of the cache is 300MB.
+ Note that this is the maximum amount of data that the Mapbox free plan allows to cache.
+ Make sure to comply with Mapbox Terms of Service before increasing this value.
+\row
+ \li mapbox.cache.memory.size
+ \li Map tile memory cache size in bytes. Default size of the cache is 3MB.
+\row
+ \li mapbox.cache.texture.size
+ \li Map tile texture cache size in bytes. Default size of the cache is 6MB.
+ Note that the texture cache has a hard minimum size which depends on the size of the map viewport
+ (it must contain enough data to display the tiles currently visible on the display).
+ This value is the amount of cache to be used in addition to the bare minimum.
\endtable
*/
diff --git a/src/location/maps/qgeofiletilecache.cpp b/src/location/maps/qgeofiletilecache.cpp
index 3d381dc2..73c9fe29 100644
--- a/src/location/maps/qgeofiletilecache.cpp
+++ b/src/location/maps/qgeofiletilecache.cpp
@@ -390,7 +390,7 @@ QSharedPointer<QGeoTileTexture> QGeoFileTileCache::addToTextureCache(const QGeoT
return tt;
}
-QString QGeoFileTileCache::tileSpecToFilename(const QGeoTileSpec &spec, const QString &format, const QString &directory)
+QString QGeoFileTileCache::tileSpecToFilename(const QGeoTileSpec &spec, const QString &format, const QString &directory) const
{
QString filename = spec.plugin();
filename += QLatin1String("-");
@@ -416,7 +416,7 @@ QString QGeoFileTileCache::tileSpecToFilename(const QGeoTileSpec &spec, const QS
return dir.filePath(filename);
}
-QGeoTileSpec QGeoFileTileCache::filenameToTileSpec(const QString &filename)
+QGeoTileSpec QGeoFileTileCache::filenameToTileSpec(const QString &filename) const
{
QGeoTileSpec emptySpec;
diff --git a/src/location/maps/qgeofiletilecache_p.h b/src/location/maps/qgeofiletilecache_p.h
index bd3e684b..caaa0b07 100644
--- a/src/location/maps/qgeofiletilecache_p.h
+++ b/src/location/maps/qgeofiletilecache_p.h
@@ -128,7 +128,7 @@ public:
const QString &format,
QGeoTiledMappingManagerEngine::CacheAreas areas = QGeoTiledMappingManagerEngine::AllCaches) Q_DECL_OVERRIDE;
-private:
+protected:
void printStats() Q_DECL_OVERRIDE;
void loadTiles();
@@ -138,8 +138,8 @@ private:
QSharedPointer<QGeoCachedTileMemory> addToMemoryCache(const QGeoTileSpec &spec, const QByteArray &bytes, const QString &format);
QSharedPointer<QGeoTileTexture> addToTextureCache(const QGeoTileSpec &spec, const QImage &image);
- static QString tileSpecToFilename(const QGeoTileSpec &spec, const QString &format, const QString &directory);
- static QGeoTileSpec filenameToTileSpec(const QString &filename);
+ virtual QString tileSpecToFilename(const QGeoTileSpec &spec, const QString &format, const QString &directory) const;
+ virtual QGeoTileSpec filenameToTileSpec(const QString &filename) const;
QCache3Q<QGeoTileSpec, QGeoCachedTileDisk, QCache3QTileEvictionPolicy > diskCache_;
QCache3Q<QGeoTileSpec, QGeoCachedTileMemory > memoryCache_;
diff --git a/src/plugins/geoservices/mapbox/mapbox.pro b/src/plugins/geoservices/mapbox/mapbox.pro
index d4797e37..e4601500 100644
--- a/src/plugins/geoservices/mapbox/mapbox.pro
+++ b/src/plugins/geoservices/mapbox/mapbox.pro
@@ -6,13 +6,15 @@ HEADERS += \
qgeoserviceproviderpluginmapbox.h \
qgeotiledmappingmanagerenginemapbox.h \
qgeotilefetchermapbox.h \
- qgeomapreplymapbox.h
+ qgeomapreplymapbox.h \
+ qgeofiletilecachemapbox.h
SOURCES += \
qgeoserviceproviderpluginmapbox.cpp \
qgeotiledmappingmanagerenginemapbox.cpp \
qgeotilefetchermapbox.cpp \
- qgeomapreplymapbox.cpp
+ qgeomapreplymapbox.cpp \
+ qgeofiletilecachemapbox.cpp
OTHER_FILES += \
mapbox_plugin.json
diff --git a/src/plugins/geoservices/mapbox/qgeofiletilecachemapbox.cpp b/src/plugins/geoservices/mapbox/qgeofiletilecachemapbox.cpp
new file mode 100644
index 00000000..4057568d
--- /dev/null
+++ b/src/plugins/geoservices/mapbox/qgeofiletilecachemapbox.cpp
@@ -0,0 +1,118 @@
+/****************************************************************************
+**
+** Copyright (C) 2016 The Qt Company Ltd.
+** Contact: http://www.qt.io/licensing/
+**
+** This file is part of the QtLocation module of the Qt Toolkit.
+**
+** $QT_BEGIN_LICENSE:LGPL3$
+** Commercial License Usage
+** Licensees holding valid commercial Qt licenses may use this file in
+** accordance with the commercial license agreement provided with the
+** Software or, alternatively, in accordance with the terms contained in
+** a written agreement between you and The Qt Company. For licensing terms
+** and conditions see http://www.qt.io/terms-conditions. For further
+** information use the contact form at http://www.qt.io/contact-us.
+**
+** GNU Lesser General Public License Usage
+** Alternatively, this file may be used under the terms of the GNU Lesser
+** General Public License version 3 as published by the Free Software
+** Foundation and appearing in the file LICENSE.LGPLv3 included in the
+** packaging of this file. Please review the following information to
+** ensure the GNU Lesser General Public License version 3 requirements
+** will be met: https://www.gnu.org/licenses/lgpl.html.
+**
+** GNU General Public License Usage
+** Alternatively, this file may be used under the terms of the GNU
+** General Public License version 2.0 or later as published by the Free
+** Software Foundation and appearing in the file LICENSE.GPL included in
+** the packaging of this file. Please review the following information to
+** ensure the GNU General Public License version 2.0 requirements will be
+** met: http://www.gnu.org/licenses/gpl-2.0.html.
+**
+** $QT_END_LICENSE$
+**
+****************************************************************************/
+
+#include "qgeofiletilecachemapbox.h"
+#include <QtLocation/private/qgeotilespec_p.h>
+#include <QDir>
+
+QT_BEGIN_NAMESPACE
+
+QGeoFileTileCacheMapbox::QGeoFileTileCacheMapbox(const QList<QGeoMapType> &mapTypes, const QString &directory, QObject *parent)
+ :QGeoFileTileCache(directory, parent), m_mapTypes(mapTypes)
+{
+ for (int i=0; i < mapTypes.size(); i++)
+ m_mapNameToId.insert(mapTypes[i].name(), i);
+}
+
+QGeoFileTileCacheMapbox::~QGeoFileTileCacheMapbox()
+{
+
+}
+
+QString QGeoFileTileCacheMapbox::tileSpecToFilename(const QGeoTileSpec &spec, const QString &format, const QString &directory) const
+{
+ QString filename = spec.plugin();
+ filename += QLatin1String("-");
+ filename += m_mapTypes[spec.mapId()].name();
+ filename += QLatin1String("-");
+ filename += QString::number(spec.zoom());
+ filename += QLatin1String("-");
+ filename += QString::number(spec.x());
+ filename += QLatin1String("-");
+ filename += QString::number(spec.y());
+
+ //Append version if real version number to ensure backwards compatibility and eviction of old tiles
+ if (spec.version() != -1) {
+ filename += QLatin1String("-");
+ filename += QString::number(spec.version());
+ }
+
+ filename += QLatin1String(".");
+ filename += format;
+
+ QDir dir = QDir(directory);
+
+ return dir.filePath(filename);
+}
+
+QGeoTileSpec QGeoFileTileCacheMapbox::filenameToTileSpec(const QString &filename) const
+{
+ QStringList parts = filename.split('.');
+
+ if (parts.length() != 2)
+ return QGeoTileSpec();
+
+ QString name = parts.at(0);
+ QStringList fields = name.split('-');
+
+ int length = fields.length();
+ if (length != 5 && length != 6)
+ return QGeoTileSpec();
+
+ QList<int> numbers;
+
+ bool ok = false;
+ for (int i = 2; i < length; ++i) {
+ ok = false;
+ int value = fields.at(i).toInt(&ok);
+ if (!ok)
+ return QGeoTileSpec();
+ numbers.append(value);
+ }
+
+ //File name without version, append default
+ if (numbers.length() < 4)
+ numbers.append(-1);
+
+ return QGeoTileSpec(fields.at(0),
+ m_mapNameToId[fields.at(1)],
+ numbers.at(0),
+ numbers.at(1),
+ numbers.at(2),
+ numbers.at(3));
+}
+
+QT_END_NAMESPACE
diff --git a/src/plugins/geoservices/mapbox/qgeofiletilecachemapbox.h b/src/plugins/geoservices/mapbox/qgeofiletilecachemapbox.h
new file mode 100644
index 00000000..189efc10
--- /dev/null
+++ b/src/plugins/geoservices/mapbox/qgeofiletilecachemapbox.h
@@ -0,0 +1,63 @@
+/****************************************************************************
+**
+** Copyright (C) 2016 The Qt Company Ltd.
+** Contact: http://www.qt.io/licensing/
+**
+** This file is part of the QtLocation module of the Qt Toolkit.
+**
+** $QT_BEGIN_LICENSE:LGPL3$
+** Commercial License Usage
+** Licensees holding valid commercial Qt licenses may use this file in
+** accordance with the commercial license agreement provided with the
+** Software or, alternatively, in accordance with the terms contained in
+** a written agreement between you and The Qt Company. For licensing terms
+** and conditions see http://www.qt.io/terms-conditions. For further
+** information use the contact form at http://www.qt.io/contact-us.
+**
+** GNU Lesser General Public License Usage
+** Alternatively, this file may be used under the terms of the GNU Lesser
+** General Public License version 3 as published by the Free Software
+** Foundation and appearing in the file LICENSE.LGPLv3 included in the
+** packaging of this file. Please review the following information to
+** ensure the GNU Lesser General Public License version 3 requirements
+** will be met: https://www.gnu.org/licenses/lgpl.html.
+**
+** GNU General Public License Usage
+** Alternatively, this file may be used under the terms of the GNU
+** General Public License version 2.0 or later as published by the Free
+** Software Foundation and appearing in the file LICENSE.GPL included in
+** the packaging of this file. Please review the following information to
+** ensure the GNU General Public License version 2.0 requirements will be
+** met: http://www.gnu.org/licenses/gpl-2.0.html.
+**
+** $QT_END_LICENSE$
+**
+****************************************************************************/
+
+#ifndef QGEOFILETILECACHEMAPBOX_H
+#define QGEOFILETILECACHEMAPBOX_H
+
+#include <QtLocation/private/qgeofiletilecache_p.h>
+#include <QMap>
+#include <QLatin1String>
+
+QT_BEGIN_NAMESPACE
+
+class QGeoFileTileCacheMapbox : public QGeoFileTileCache
+{
+ Q_OBJECT
+public:
+ QGeoFileTileCacheMapbox(const QList<QGeoMapType> &mapTypes, const QString &directory = QString(), QObject *parent = 0);
+ ~QGeoFileTileCacheMapbox();
+
+protected:
+ QString tileSpecToFilename(const QGeoTileSpec &spec, const QString &format, const QString &directory) const Q_DECL_OVERRIDE;
+ QGeoTileSpec filenameToTileSpec(const QString &filename) const Q_DECL_OVERRIDE;
+
+ QList<QGeoMapType> m_mapTypes;
+ QMap<QString, int> m_mapNameToId;
+};
+
+QT_END_NAMESPACE
+
+#endif // QGEOFILETILECACHEMAPBOX_H
diff --git a/src/plugins/geoservices/mapbox/qgeoserviceproviderpluginmapbox.cpp b/src/plugins/geoservices/mapbox/qgeoserviceproviderpluginmapbox.cpp
index ec40716b..8638ad91 100644
--- a/src/plugins/geoservices/mapbox/qgeoserviceproviderpluginmapbox.cpp
+++ b/src/plugins/geoservices/mapbox/qgeoserviceproviderpluginmapbox.cpp
@@ -54,14 +54,13 @@ QGeoCodingManagerEngine *QGeoServiceProviderFactoryMapbox::createGeocodingManage
QGeoMappingManagerEngine *QGeoServiceProviderFactoryMapbox::createMappingManagerEngine(
const QVariantMap &parameters, QGeoServiceProvider::Error *error, QString *errorString) const
{
- const QString mapId = parameters.value(QStringLiteral("mapbox.map_id")).toString();
const QString accessToken = parameters.value(QStringLiteral("mapbox.access_token")).toString();
- if (!mapId.isEmpty() && !accessToken.isEmpty()) {
+ if (!accessToken.isEmpty()) {
return new QGeoTiledMappingManagerEngineMapbox(parameters, error, errorString);
} else {
*error = QGeoServiceProvider::MissingRequiredParameterError;
- *errorString = tr("Mapbox plugin requires 'mapbox.map_id' and 'mapbox.access_token' parameters.\n"
+ *errorString = tr("Mapbox plugin requires a 'mapbox.access_token' parameter.\n"
"Please visit https://www.mapbox.com");
return 0;
}
diff --git a/src/plugins/geoservices/mapbox/qgeotiledmappingmanagerenginemapbox.cpp b/src/plugins/geoservices/mapbox/qgeotiledmappingmanagerenginemapbox.cpp
index 4be5ac24..ea679afc 100644
--- a/src/plugins/geoservices/mapbox/qgeotiledmappingmanagerenginemapbox.cpp
+++ b/src/plugins/geoservices/mapbox/qgeotiledmappingmanagerenginemapbox.cpp
@@ -40,9 +40,31 @@
#include <QtLocation/private/qgeocameracapabilities_p.h>
#include <QtLocation/private/qgeomaptype_p.h>
#include <QtLocation/private/qgeotiledmap_p.h>
+#include "qgeofiletilecachemapbox.h"
QT_BEGIN_NAMESPACE
+static QLatin1String mapboxPrefix = QLatin1String("mapbox.");
+
+static QString mapboxIdToMapName(const QString &id)
+{
+ if ((id.indexOf(mapboxPrefix) == 0) && (id.size() > mapboxPrefix.size())) {
+ // the names of the geomaptypes in this plugin reflect the actual mapbox mapIds, but are camel cased and with dashes replaced by spaces
+ QString name = id.right(id.size() - mapboxPrefix.size());
+ QStringList parts = name.split('-');
+ for (int i=0; i < parts.size(); ++i)
+ parts[i].replace(0, 1, parts[i][0].toUpper());
+ name = parts.join(" ");
+ return name;
+ } else {
+ return QString();
+ }
+}
+static inline QString mapNameToMapboxId(const QString &name)
+{
+ return mapboxPrefix + name.toLower().replace(' ','-');
+}
+
QGeoTiledMappingManagerEngineMapbox::QGeoTiledMappingManagerEngineMapbox(const QVariantMap &parameters, QGeoServiceProvider::Error *error, QString *errorString)
: QGeoTiledMappingManagerEngine()
{
@@ -54,18 +76,53 @@ QGeoTiledMappingManagerEngineMapbox::QGeoTiledMappingManagerEngineMapbox(const Q
setTileSize(QSize(256, 256));
QList<QGeoMapType> mapTypes;
- mapTypes << QGeoMapType(QGeoMapType::CustomMap, tr("Custom"), tr("Mapbox custom map"), false, false, 0);
+ // as index 0 to retain compatibility with the current API, that expects the passed map_id to be on by default.
+ if (parameters.contains(QStringLiteral("mapbox.map_id"))) {
+ const QString name = mapboxIdToMapName(parameters.value(QStringLiteral("mapbox.map_id")).toString());
+ mapTypes << QGeoMapType(QGeoMapType::CustomMap, name, QObject::tr(name.toLatin1().data()), false, false, mapTypes.size());
+ }
+
+ // As of 2016.06.15, valid mapbox map_ids are documented at https://www.mapbox.com/api-documentation/#maps
+ mapTypes << QGeoMapType(QGeoMapType::StreetMap, QStringLiteral("Streets"), tr("Street"), false, false, mapTypes.size());
+ mapTypes << QGeoMapType(QGeoMapType::StreetMap, QStringLiteral("Light"), tr("Light"), false, false, mapTypes.size());
+ mapTypes << QGeoMapType(QGeoMapType::StreetMap, QStringLiteral("Dark"), tr("Dark"), false, true, mapTypes.size());
+ mapTypes << QGeoMapType(QGeoMapType::SatelliteMapDay, QStringLiteral("Satellite"), tr("Satellite"), false, false, mapTypes.size());
+ mapTypes << QGeoMapType(QGeoMapType::HybridMap, QStringLiteral("Streets Satellite"), tr("Streets Satellite"), false, false, mapTypes.size());
+ mapTypes << QGeoMapType(QGeoMapType::CustomMap, QStringLiteral("Wheatpaste"), tr("Wheatpaste"), false, false, mapTypes.size());
+ mapTypes << QGeoMapType(QGeoMapType::StreetMap, QStringLiteral("Streets Basic"), tr("Streets Basic"), false, false, mapTypes.size());
+ mapTypes << QGeoMapType(QGeoMapType::CustomMap, QStringLiteral("Comic"), tr("Comic"), false, false, mapTypes.size());
+ mapTypes << QGeoMapType(QGeoMapType::PedestrianMap, QStringLiteral("Outdoors"), tr("Outdoors"), false, false, mapTypes.size());
+ mapTypes << QGeoMapType(QGeoMapType::CycleMap, QStringLiteral("Run Bike Hike"), tr("Run Bike Hike"), false, false, mapTypes.size());
+ mapTypes << QGeoMapType(QGeoMapType::CustomMap, QStringLiteral("Pencil"), tr("Pencil"), false, false, mapTypes.size());
+ mapTypes << QGeoMapType(QGeoMapType::CustomMap, QStringLiteral("Pirates"), tr("Pirates"), false, false, mapTypes.size());
+ mapTypes << QGeoMapType(QGeoMapType::CustomMap, QStringLiteral("Emerald"), tr("Emerald"), false, false, mapTypes.size());
+ mapTypes << QGeoMapType(QGeoMapType::CustomMap, QStringLiteral("High Contrast"), tr("High Contrast"), false, false, mapTypes.size());
+
+ // New way to specify multiple customized map_ids via additional_map_ids
+ if (parameters.contains(QStringLiteral("mapbox.additional_map_ids"))) {
+ const QString ids = parameters.value(QStringLiteral("mapbox.additional_map_ids")).toString();
+ const QStringList idList = ids.split(',', QString::SkipEmptyParts);
+
+ for (const QString &str: idList) {
+ const QString name = mapboxIdToMapName(str);
+ if (!name.isEmpty())
+ mapTypes << QGeoMapType(QGeoMapType::CustomMap, name, QObject::tr(name.toLatin1().data()), false, false, mapTypes.size());
+ }
+ }
+
+ QVector<QString> mapIds;
+ for (int i=0; i < mapTypes.size(); ++i)
+ mapIds.push_back(mapNameToMapboxId(mapTypes[i].name()));
+
setSupportedMapTypes(mapTypes);
QGeoTileFetcherMapbox *tileFetcher = new QGeoTileFetcherMapbox(this);
+ tileFetcher->setMapIds(mapIds);
+
if (parameters.contains(QStringLiteral("useragent"))) {
const QByteArray ua = parameters.value(QStringLiteral("useragent")).toString().toLatin1();
tileFetcher->setUserAgent(ua);
}
- if (parameters.contains(QStringLiteral("mapbox.map_id"))) {
- const QString id = parameters.value(QStringLiteral("mapbox.map_id")).toString();
- tileFetcher->setMapId(id);
- }
if (parameters.contains(QStringLiteral("mapbox.format"))) {
const QString format = parameters.value(QStringLiteral("mapbox.format")).toString();
tileFetcher->setFormat(format);
@@ -77,6 +134,52 @@ QGeoTiledMappingManagerEngineMapbox::QGeoTiledMappingManagerEngineMapbox(const Q
setTileFetcher(tileFetcher);
+ // TODO: do this in a plugin-neutral way so that other tiled map plugins
+ // don't need this boilerplate or hardcode plugin name
+
+ if (parameters.contains(QStringLiteral("mapbox.cache.directory"))) {
+ m_cacheDirectory = parameters.value(QStringLiteral("mapbox.cache.directory")).toString();
+ } else {
+ // managerName() is not yet set, we have to hardcode the plugin name below
+ m_cacheDirectory = QAbstractGeoTileCache::baseCacheDirectory() + QLatin1String("mapbox");
+ }
+
+ // The Mapbox free plan allows for 6000 tiles to be stored for offline uses
+ // As of 2016.06.15, according to https://www.mapbox.com/help/mobile-offline/ ,
+ // this translates into 45-315 MiB, depending on the map and the area.
+ // Setting a default limit of 300MiB, which can be overridden via parameters, if
+ // the plan allows for more data to be stored offline.
+ // NOTE:
+ // It is illegal to violate Mapbox Terms of Service, setting a limit that exceeds
+ // what the plan the token belongs to allows.
+
+ QAbstractGeoTileCache *tileCache = new QGeoFileTileCacheMapbox(mapTypes, m_cacheDirectory);
+
+ if (parameters.contains(QStringLiteral("mapbox.cache.disk.size"))) {
+ bool ok = false;
+ int cacheSize = parameters.value(QStringLiteral("mapbox.cache.disk.size")).toString().toInt(&ok);
+ if (ok)
+ tileCache->setMaxDiskUsage(cacheSize);
+ } else {
+ tileCache->setMaxDiskUsage(300 * 1024 * 1024);
+ }
+
+ if (parameters.contains(QStringLiteral("mapbox.cache.memory.size"))) {
+ bool ok = false;
+ int cacheSize = parameters.value(QStringLiteral("mapbox.cache.memory.size")).toString().toInt(&ok);
+ if (ok)
+ tileCache->setMaxMemoryUsage(cacheSize);
+ }
+
+ if (parameters.contains(QStringLiteral("mapbox.cache.texture.size"))) {
+ bool ok = false;
+ int cacheSize = parameters.value(QStringLiteral("mapbox.cache.texture.size")).toString().toInt(&ok);
+ if (ok)
+ tileCache->setExtraTextureUsage(cacheSize);
+ }
+
+ setTileCache(tileCache);
+
*error = QGeoServiceProvider::NoError;
errorString->clear();
}
diff --git a/src/plugins/geoservices/mapbox/qgeotiledmappingmanagerenginemapbox.h b/src/plugins/geoservices/mapbox/qgeotiledmappingmanagerenginemapbox.h
index 379ca6b8..292e4211 100644
--- a/src/plugins/geoservices/mapbox/qgeotiledmappingmanagerenginemapbox.h
+++ b/src/plugins/geoservices/mapbox/qgeotiledmappingmanagerenginemapbox.h
@@ -53,6 +53,9 @@ public:
~QGeoTiledMappingManagerEngineMapbox();
QGeoMap *createMap();
+
+private:
+ QString m_cacheDirectory;
};
QT_END_NAMESPACE
diff --git a/src/plugins/geoservices/mapbox/qgeotilefetchermapbox.cpp b/src/plugins/geoservices/mapbox/qgeotilefetchermapbox.cpp
index 69a832df..7a082006 100644
--- a/src/plugins/geoservices/mapbox/qgeotilefetchermapbox.cpp
+++ b/src/plugins/geoservices/mapbox/qgeotilefetchermapbox.cpp
@@ -47,7 +47,6 @@ QT_BEGIN_NAMESPACE
QGeoTileFetcherMapbox::QGeoTileFetcherMapbox(QObject *parent)
: QGeoTileFetcher(parent), m_networkManager(new QNetworkAccessManager(this)),
m_userAgent("Qt Location based application"),
- m_mapId(""),
m_format("png"),
m_replyFormat("png"),
m_accessToken("")
@@ -59,9 +58,9 @@ void QGeoTileFetcherMapbox::setUserAgent(const QByteArray &userAgent)
m_userAgent = userAgent;
}
-void QGeoTileFetcherMapbox::setMapId(const QString &mapId)
+void QGeoTileFetcherMapbox::setMapIds(const QVector<QString> &mapIds)
{
- m_mapId = mapId;
+ m_mapIds = mapIds;
}
void QGeoTileFetcherMapbox::setFormat(const QString &format)
@@ -87,7 +86,7 @@ QGeoTiledMapReply *QGeoTileFetcherMapbox::getTileImage(const QGeoTileSpec &spec)
request.setRawHeader("User-Agent", m_userAgent);
request.setUrl(QUrl(QStringLiteral("http://api.tiles.mapbox.com/v4/") +
- m_mapId + QLatin1Char('/') +
+ ((spec.mapId() >= m_mapIds.size()) ? QStringLiteral("mapbox.streets") : m_mapIds[spec.mapId()]) + QLatin1Char('/') +
QString::number(spec.zoom()) + QLatin1Char('/') +
QString::number(spec.x()) + QLatin1Char('/') +
QString::number(spec.y()) + QLatin1Char('.') +
diff --git a/src/plugins/geoservices/mapbox/qgeotilefetchermapbox.h b/src/plugins/geoservices/mapbox/qgeotilefetchermapbox.h
index ddec2896..a1ab7dcc 100644
--- a/src/plugins/geoservices/mapbox/qgeotilefetchermapbox.h
+++ b/src/plugins/geoservices/mapbox/qgeotilefetchermapbox.h
@@ -37,6 +37,7 @@
#ifndef QGEOTILEFETCHERMAPBOX_H
#define QGEOTILEFETCHERMAPBOX_H
+#include <qvector.h>
#include <QtLocation/private/qgeotilefetcher_p.h>
QT_BEGIN_NAMESPACE
@@ -52,7 +53,7 @@ public:
QGeoTileFetcherMapbox(QObject *parent = 0);
void setUserAgent(const QByteArray &userAgent);
- void setMapId(const QString &mapId);
+ void setMapIds(const QVector<QString> &mapIds);
void setFormat(const QString &format);
void setAccessToken(const QString &accessToken);
@@ -61,10 +62,10 @@ private:
QNetworkAccessManager *m_networkManager;
QByteArray m_userAgent;
- QString m_mapId;
QString m_format;
QString m_replyFormat;
QString m_accessToken;
+ QVector<QString> m_mapIds;
};
QT_END_NAMESPACE