Merge pull request #351 from mapbox/access_token

Use API tokens and v4 vector tile endpoint
This commit is contained in:
Konstantin Käfer
2014-07-10 11:36:58 -07:00
13 changed files with 73 additions and 14 deletions

View File

@@ -14,6 +14,7 @@ env:
global:
- secure: "bG4YYWMfl9API0MSRgmOaJrlGLv06tRg9KJNawBieZvBJbITPpxVGJZT3/l/SEJ+Rl15e2dRex4k+MGQlmT2SqPQxTEYWv1qxNigKPPcla7IWeNmWWqW8uVvFjdglojgBOK2k/xErVQtA4zDfi3mwSXH4DKwquXWsoEKmX2SV7M="
- secure: "Cbvap9ubVKgjPe3hUhI6JGeDZzBXHpOG9RaYKh+SdoIPhKnlJiNOYm1egomi+e4uqJInlFKuVHTw7Ng9Cun6Zm0jIxpkSchv1GpsR7hmB3UGnGed19Dw8121FwuUaktN+4YnbVlsyd+u8EHD3+h58t4eELrLrZolM4rS7DL6caA="
- secure: "RiBIBfVhhaMjU5ksuwJO3shdvG9FpinBjdSv4co9jg9171SR8edNriedHjVKSIeBhSGNmZmX+twS3dJS/By6tl/LKh9sTynA+ZAYYljkE7jn881B/gMrlYvdAA6og5KvkhV1/0iJWlhuZrMTkhpDR200iLgg3EWBhWjltzmDW/I="
before_install:
- ./scripts/setup_travis.sh

View File

@@ -27,9 +27,18 @@ First off: if you hit problems during development try:
This will clear cached build data and update to the latest versions of dependencies.
### Mapbox API access tokens
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/).
## OS X
First run:
in the Xcode project, click on the Target icon next to the Run/Stop button, and then "Edit Scheme". In the screen, add an environment variable with the name `MAPBOX_ACCESS_TOKEN` to the targets you want to run.
![Setting the access token](https://cloud.githubusercontent.com/assets/52399/3543326/9e7cfbb8-0860-11e4-8def-3684a9028b61.png)
Run:
make setup
@@ -52,6 +61,8 @@ First, pull down the submodule(s):
git submodule init
git submodule update
Insert your access token into `MBXViewController.h`.
Then run:
make setup
@@ -64,6 +75,8 @@ Target devices: iPhone 4 and above (4S, 5, 5c, 5s) and iPad 2 and above (3, 4, m
## Ubuntu
Set the environment variable `MAPBOX_ACCESS_TOKEN` to your token.
Ensure you have git and other build essentials:
sudo apt-get update

View File

@@ -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;

View File

@@ -23,7 +23,8 @@ class Texturepool;
class Source : public std::enable_shared_from_this<Source>, 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<Tile::ID> getIDs() const;
void updateClipIDs(const std::map<Tile::ID, ClipID> &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;

View File

@@ -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 &map;
std::set<std::shared_ptr<Source>> activeSources;
PropertyTransition defaultTransition;
bool initial_render_complete = false;

View File

@@ -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<StyleLayerGroup> getLayers() {
@@ -92,6 +95,8 @@ private:
std::forward_list<Value> parseValues(JSVal values);
private:
Map &map;
std::unordered_map<std::string, const rapidjson::Value *> constants;
std::unordered_map<std::string, const std::shared_ptr<Source>> sources;

View File

@@ -60,6 +60,14 @@ int main(int argc, char *argv[]) {
map.setAngle(settings.angle);
map.setDebug(settings.debug);
// Set access token if present
const char *token = getenv("MAPBOX_ACCESS_TOKEN");
if (token == nullptr) {
fprintf(stderr, "[WARNING] no access token set. mapbox.com tiles won't work.\n");
} else {
map.setAccessToken(std::string(token));
}
// Load style
map.setStyleJSON(stylejson.str());

View File

@@ -13,6 +13,14 @@ int main() {
map.setAngle(settings.angle);
map.setDebug(settings.debug);
// Set access token if present
const char *token = getenv("MAPBOX_ACCESS_TOKEN");
if (token == nullptr) {
fprintf(stderr, "[WARNING] no access token set. mapbox.com tiles won't work.\n");
} else {
map.setAccessToken(std::string(token));
}
// Load style
NSString *path = [[NSBundle mainBundle] pathForResource:@"style.min" ofType:@"js"];
NSString *json = [NSString stringWithContentsOfFile:path

View File

@@ -22,7 +22,7 @@ using namespace llmr;
Map::Map(View& view)
: view(view),
transform(),
style(std::make_shared<Style>()),
style(std::make_shared<Style>(*this)),
glyphAtlas(std::make_shared<GlyphAtlas>(1024, 1024)),
spriteAtlas(std::make_shared<SpriteAtlas>(512, 512)),
texturepool(std::make_shared<Texturepool>()),
@@ -183,6 +183,14 @@ std::string Map::getStyleJSON() const {
return styleJSON;
}
void Map::setAccessToken( std::string access_token) {
accessToken.swap(access_token);
}
std::string Map::getAccessToken() const {
return accessToken;
}
#pragma mark - Size
// Note: This function is called from another thread. Make sure you only call threadsafe functions!

View File

@@ -19,23 +19,26 @@
namespace llmr {
Source::Source(SourceType type, const std::string &url,
uint32_t tile_size, uint32_t min_zoom, uint32_t max_zoom)
uint32_t tile_size, uint32_t min_zoom, uint32_t max_zoom,
const std::string &access_token)
: type(type),
url(normalizeSourceURL(url)),
url(normalizeSourceURL(url, access_token)),
tile_size(tile_size),
min_zoom(min_zoom),
max_zoom(max_zoom) {}
std::string Source::normalizeSourceURL(const std::string &url) {
std::string Source::normalizeSourceURL(const std::string &url, const std::string &access_token) {
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()) + "/{z}/{x}/{y}.vector.pbf";
if (access_token.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/") + url.substr(t.length()) + "/{z}/{x}/{y}.vector.pbf?access_token=" + access_token;
} else {
return url;
}
}
bool Source::update(Map &map) {
if (map.getTime() > updated) {
return updateTiles(map);

View File

@@ -1,3 +1,4 @@
#include <llmr/map/map.hpp>
#include <llmr/style/style.hpp>
#include <llmr/style/style_layer_group.hpp>
#include <llmr/style/style_parser.hpp>
@@ -12,7 +13,8 @@
namespace llmr {
Style::Style() {
Style::Style(Map &map)
: map(map) {
}
void Style::updateSources() {
@@ -107,7 +109,7 @@ void Style::loadJSON(const uint8_t *const data) {
throw error::style_parse(doc.GetErrorOffset(), doc.GetParseError());
}
StyleParser parser;
StyleParser parser(map);
parser.parse(const_cast<const rapidjson::Document &>(doc));
layers = parser.getLayers();

View File

@@ -1,3 +1,4 @@
#include <llmr/map/map.hpp>
#include <llmr/style/style_parser.hpp>
#include <llmr/style/style_layer_group.hpp>
#include <llmr/util/constants.hpp>
@@ -8,6 +9,10 @@ namespace llmr {
using JSVal = const rapidjson::Value&;
StyleParser::StyleParser(Map &map)
: map(map) {
}
void StyleParser::parse(JSVal document) {
if (document.HasMember("constants")) {
parseConstants(document["constants"]);
@@ -169,7 +174,7 @@ void StyleParser::parseSources(JSVal value) {
parseRenderProperty(itr->value, min_zoom, "minZoom");
parseRenderProperty(itr->value, max_zoom, "maxZoom");
sources.emplace(std::move(name), std::make_shared<Source>(type, url, tile_size, min_zoom, max_zoom));
sources.emplace(std::move(name), std::make_shared<Source>(type, url, tile_size, min_zoom, max_zoom, map.getAccessToken()));
}
} else {
throw Style::exception("sources must be an object");