From a8833c34d4c6334210e3d84bae4fbfdebe3d9468 Mon Sep 17 00:00:00 2001 From: "Justin R. Miller" Date: Mon, 30 Mar 2015 15:33:02 -0700 Subject: [PATCH] fixes #892: support mapbox:// style URLs --- platform/ios/MGLMapView.mm | 13 +++++++++++-- src/mbgl/map/map.cpp | 8 +++++--- src/mbgl/util/mapbox.cpp | 18 ++++++++++++++---- src/mbgl/util/mapbox.hpp | 1 + 4 files changed, 31 insertions(+), 9 deletions(-) diff --git a/platform/ios/MGLMapView.mm b/platform/ios/MGLMapView.mm index 49cfdd385..76413bc2c 100644 --- a/platform/ios/MGLMapView.mm +++ b/platform/ios/MGLMapView.mm @@ -172,9 +172,18 @@ mbgl::DefaultFileSource *mbglFileSource = nullptr; } } -- (void)setStyleURL:(NSString *)filePathURL +- (void)setStyleURL:(NSString *)styleURLString { - mbglMap->setStyleURL(std::string("asset://") + [filePathURL UTF8String]); + std::string styleURL([styleURLString UTF8String]); + + if ( ! [[NSURL URLWithString:styleURLString] scheme]) + { + mbglMap->setStyleURL(std::string("asset://") + styleURL); + } + else + { + mbglMap->setStyleURL(styleURL); + } } - (BOOL)commonInit diff --git a/src/mbgl/map/map.cpp b/src/mbgl/map/map.cpp index 7bc91ed48..159a34d54 100644 --- a/src/mbgl/map/map.cpp +++ b/src/mbgl/map/map.cpp @@ -400,13 +400,15 @@ void Map::setup() { void Map::setStyleURL(const std::string &url) { assert(Environment::currentlyOn(ThreadType::Main)); - const size_t pos = url.rfind('/'); + const std::string styleURL = mbgl::util::mapbox::normalizeStyleURL(url, getAccessToken()); + + const size_t pos = styleURL.rfind('/'); std::string base = ""; if (pos != std::string::npos) { - base = url.substr(0, pos + 1); + base = styleURL.substr(0, pos + 1); } - data->setStyleInfo({ url, base, "" }); + data->setStyleInfo({ styleURL, base, "" }); triggerUpdate(Update::StyleInfo); } diff --git a/src/mbgl/util/mapbox.cpp b/src/mbgl/util/mapbox.cpp index 5d9c630c1..a8da81d1f 100644 --- a/src/mbgl/util/mapbox.cpp +++ b/src/mbgl/util/mapbox.cpp @@ -8,11 +8,12 @@ namespace mapbox { const std::string mapbox = "mapbox://"; -std::string normalizeURL(const std::string& url, const std::string& accessToken) { +std::string normalizeURL(const std::string& url, const std::string& pathPrefix, const std::string& accessToken) { if (accessToken.empty()) throw std::runtime_error("You must provide a Mapbox API access token for Mapbox tile sources"); - return std::string("https://api.tiles.mapbox.com/v4/") + return std::string("https://api.tiles.mapbox.com") + + pathPrefix + url.substr(mapbox.length()) + "?access_token=" + accessToken; @@ -22,7 +23,7 @@ std::string normalizeSourceURL(const std::string& url, const std::string& access if (url.compare(0, mapbox.length(), mapbox) != 0) return url; - std::string result = normalizeURL(url + ".json", accessToken); + std::string result = normalizeURL(url + ".json", "/v4/", accessToken); // TileJSON requests need a secure flag appended to their URLs so // that the server knows to send SSL-ified resource references. @@ -31,11 +32,20 @@ std::string normalizeSourceURL(const std::string& url, const std::string& access return result; } +std::string normalizeStyleURL(const std::string& url, const std::string& accessToken) { + if (url.compare(0, mapbox.length(), mapbox) != 0) + return url; + + const std::string user = url.substr(mapbox.length(), url.find('.') - mapbox.length()); + + return normalizeURL(url, "/styles/v1/" + user + "/", accessToken); +} + std::string normalizeGlyphsURL(const std::string& url, const std::string& accessToken) { if (url.compare(0, mapbox.length(), mapbox) != 0) return url; - return normalizeURL(url, accessToken); + return normalizeURL(url, "/v4/", accessToken); } std::string normalizeTileURL(const std::string& url, const std::string& sourceURL, SourceType sourceType) { diff --git a/src/mbgl/util/mapbox.hpp b/src/mbgl/util/mapbox.hpp index 8ad92c0b4..86ea349b8 100644 --- a/src/mbgl/util/mapbox.hpp +++ b/src/mbgl/util/mapbox.hpp @@ -9,6 +9,7 @@ namespace util { namespace mapbox { std::string normalizeSourceURL(const std::string& url, const std::string& accessToken); +std::string normalizeStyleURL(const std::string& url, const std::string& accessToken); std::string normalizeGlyphsURL(const std::string& url, const std::string& accessToken); std::string normalizeTileURL(const std::string& url, const std::string& sourceURL, SourceType sourceType);