From 815f8be96e8048f4c64ca0280e43339f17dd064d Mon Sep 17 00:00:00 2001 From: John Firebaugh Date: Thu, 26 Jun 2014 12:03:47 -0700 Subject: [PATCH 01/21] Use API v4 and access tokens --- .gitignore | 1 + README.md | 2 ++ access_token.hpp | 1 + include/llmr/llmr.hpp | 6 ++++++ linux/main.cpp | 3 +++ macosx/main.mm | 3 +++ src/map/source.cpp | 11 ++++++++++- 7 files changed, 26 insertions(+), 1 deletion(-) create mode 100644 access_token.hpp diff --git a/.gitignore b/.gitignore index c2ddb1a0c..32edd1bef 100644 --- a/.gitignore +++ b/.gitignore @@ -13,3 +13,4 @@ /src/shader/shaders_gl.cpp /src/shader/shaders_gles2.cpp /bin/style.bin.js +access_token.hpp diff --git a/README.md b/README.md index 4ca8960be..356f83b1b 100644 --- a/README.md +++ b/README.md @@ -27,6 +27,8 @@ First off: if you hit problems during development try: This will clear cached build data and update to the latest versions of dependencies. +The demo applications use Mapbox vector tiles, which require a Mapbox account and API access token. Obtain an access token on the [Mapbox account page](https://www.mapbox.com/account/apps/) and paste it in the `access_token.hpp` file before building the demo projects. + ## OS X First run: diff --git a/access_token.hpp b/access_token.hpp new file mode 100644 index 000000000..22815bad6 --- /dev/null +++ b/access_token.hpp @@ -0,0 +1 @@ +const char *mapbox_access_token = ""; diff --git a/include/llmr/llmr.hpp b/include/llmr/llmr.hpp index 349b15d8c..114a957cd 100644 --- a/include/llmr/llmr.hpp +++ b/include/llmr/llmr.hpp @@ -4,4 +4,10 @@ #include "map/map.hpp" #include "map/view.hpp" +namespace llmr { + +void setMapboxAccessToken(const std::string& token); + +}; + #endif diff --git a/linux/main.cpp b/linux/main.cpp index 25898fab2..00732dfcb 100644 --- a/linux/main.cpp +++ b/linux/main.cpp @@ -8,6 +8,7 @@ #include "../common/settings_json.hpp" #include "../common/glfw_view.hpp" +#include "../access_token.hpp" GLFWView *view = nullptr; @@ -51,6 +52,8 @@ int main(int argc, char *argv[]) { std::stringstream stylejson; stylejson << stylefile.rdbuf(); + llmr::setMapboxAccessToken(mapbox_access_token); + view = new GLFWView(); llmr::Map map(*view); diff --git a/macosx/main.mm b/macosx/main.mm index 4c5da2f32..156846469 100644 --- a/macosx/main.mm +++ b/macosx/main.mm @@ -1,9 +1,12 @@ #include "../common/settings_nsuserdefaults.hpp" #include "../common/glfw_view.hpp" +#include "../access_token.hpp" #import int main() { + llmr::setMapboxAccessToken(mapbox_access_token); + GLFWView view; llmr::Map map(view); diff --git a/src/map/source.cpp b/src/map/source.cpp index c3c5e89fd..d52cfe983 100644 --- a/src/map/source.cpp +++ b/src/map/source.cpp @@ -17,6 +17,12 @@ namespace llmr { +static std::string mapboxAccessToken; + +void setMapboxAccessToken(const std::string& token) { + mapboxAccessToken = token; +} + Source::Source(SourceType type, const std::string &url, uint32_t tile_size, uint32_t min_zoom, uint32_t max_zoom) : type(type), @@ -28,7 +34,10 @@ Source::Source(SourceType type, const std::string &url, std::string Source::normalizeSourceURL(const std::string &url) { const std::string t = "mapbox://"; if (url.compare(0, t.length(), t) == 0) { - return std::string("http://api.tiles.mapbox.com/v3/") + url.substr(t.length()) + "/%d/%d/%d.vector.pbf"; + if (mapboxAccessToken.empty()) { + throw std::runtime_error("You must provide a Mapbox API access token via llmr::setMapboxAccessToken()"); + } + return std::string("https://api.tiles.mapbox.com/v4/") + url.substr(t.length()) + "/%d/%d/%d.vector.pbf?access_token=" + mapboxAccessToken; } else { return url; } From 98a415416a35bb4a2c834361391b8855a0bca644 Mon Sep 17 00:00:00 2001 From: "Justin R. Miller" Date: Wed, 9 Jul 2014 17:18:36 -0700 Subject: [PATCH 02/21] move to per-map access token --- include/llmr/map/map.hpp | 3 +++ include/llmr/map/source.hpp | 5 +++-- include/llmr/style/style.hpp | 4 +++- include/llmr/style/style_parser.hpp | 5 +++++ src/map/map.cpp | 10 +++++++++- src/map/source.cpp | 20 +++++++------------- src/style/style.cpp | 6 ++++-- src/style/style_parser.cpp | 7 ++++++- 8 files changed, 40 insertions(+), 20 deletions(-) diff --git a/include/llmr/map/map.hpp b/include/llmr/map/map.hpp index 1af6b3c54..defc1bef1 100644 --- a/include/llmr/map/map.hpp +++ b/include/llmr/map/map.hpp @@ -59,6 +59,8 @@ public: void setDefaultTransitionDuration(uint64_t duration_milliseconds = 0); void setStyleJSON(std::string newStyleJSON); std::string getStyleJSON() const; + void setAccessToken(std::string access_token); + std::string getAccessToken() const; // Transition void cancelTransitions(); @@ -166,6 +168,7 @@ private: Painter painter; std::string styleJSON = ""; + std::string accessToken = ""; bool debug = false; timestamp animationTime = 0; diff --git a/include/llmr/map/source.hpp b/include/llmr/map/source.hpp index 2937a8f69..f74624925 100644 --- a/include/llmr/map/source.hpp +++ b/include/llmr/map/source.hpp @@ -23,7 +23,8 @@ class Texturepool; class Source : public std::enable_shared_from_this, private util::noncopyable { public: Source(SourceType type = SourceType::Vector, const std::string &url = "", - uint32_t tile_size = 512, uint32_t min_zoom = 0, uint32_t max_zoom = 22); + uint32_t tile_size = 512, uint32_t min_zoom = 0, uint32_t max_zoom = 22, + const std::string &access_token = ""); bool update(Map &map); void updateMatrices(const mat4 &projMatrix, const TransformState &transform); @@ -35,7 +36,7 @@ public: std::forward_list getIDs() const; void updateClipIDs(const std::map &mapping); - static std::string normalizeSourceURL(const std::string &url); + static std::string normalizeSourceURL(const std::string &url, const std::string &access_token); public: const SourceType type; diff --git a/include/llmr/style/style.hpp b/include/llmr/style/style.hpp index 37a50485b..cfaba1898 100644 --- a/include/llmr/style/style.hpp +++ b/include/llmr/style/style.hpp @@ -16,6 +16,7 @@ namespace llmr { +class Map; class Sprite; class Source; class StyleLayer; @@ -27,7 +28,7 @@ public: struct exception : std::runtime_error { exception(const char *msg) : std::runtime_error(msg) {} }; public: - Style(); + Style(Map &map); void loadJSON(const uint8_t *const data); @@ -63,6 +64,7 @@ private: private: + Map ↦ std::set> activeSources; PropertyTransition defaultTransition; bool initial_render_complete = false; diff --git a/include/llmr/style/style_parser.hpp b/include/llmr/style/style_parser.hpp index 10cd3d2e8..27dfd4757 100644 --- a/include/llmr/style/style_parser.hpp +++ b/include/llmr/style/style_parser.hpp @@ -17,6 +17,7 @@ namespace llmr { enum class ClassID : uint32_t; +class Map; class StyleLayer; class StyleLayerGroup; @@ -24,6 +25,8 @@ class StyleParser { public: using JSVal = const rapidjson::Value&; + StyleParser(Map &map); + void parse(JSVal document); std::shared_ptr getLayers() { @@ -92,6 +95,8 @@ private: std::forward_list parseValues(JSVal values); private: + Map ↦ + std::unordered_map constants; std::unordered_map> sources; diff --git a/src/map/map.cpp b/src/map/map.cpp index 516e8fe03..b1f22235b 100644 --- a/src/map/map.cpp +++ b/src/map/map.cpp @@ -22,7 +22,7 @@ using namespace llmr; Map::Map(View& view) : view(view), transform(), - style(std::make_shared