From c011189f712565e4813eedb532e7f81b73c62f39 Mon Sep 17 00:00:00 2001 From: bsudekum Date: Tue, 3 Jan 2017 13:12:57 -0800 Subject: Add annotation api --- platform/node/src/node_map.cpp | 47 ++++++++++++++++++++++++++++++++++++++++++ platform/node/src/node_map.hpp | 2 ++ platform/node/test.js | 44 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 93 insertions(+) create mode 100644 platform/node/test.js diff --git a/platform/node/src/node_map.cpp b/platform/node/src/node_map.cpp index acf83eef66..fad372a307 100644 --- a/platform/node/src/node_map.cpp +++ b/platform/node/src/node_map.cpp @@ -9,6 +9,8 @@ #include #include #include +#include +#include #include @@ -20,6 +22,12 @@ namespace node_mbgl { +struct AnnotationOptions { + double latitude = 0; + double longitude = 0; + std::string image; +}; + struct NodeMap::RenderOptions { double zoom = 0; double bearing = 0; @@ -29,6 +37,7 @@ struct NodeMap::RenderOptions { unsigned int width = 512; unsigned int height = 512; std::vector classes; + std::vector annotations; mbgl::MapDebugOptions debugOptions = mbgl::MapDebugOptions::NoDebug; }; @@ -248,6 +257,23 @@ NodeMap::RenderOptions NodeMap::ParseOptions(v8::Local obj) { options.pitch = Nan::Get(obj, Nan::New("pitch").ToLocalChecked()).ToLocalChecked()->NumberValue(); } + if (Nan::Has(obj, Nan::New("annotations").ToLocalChecked()).FromJust()) { + auto annotationsArray = Nan::Get(obj, Nan::New("annotations").ToLocalChecked()).ToLocalChecked(); + if (annotationsArray->IsArray()) { + auto annotations = annotationsArray.As(); + const int length = annotations->Length(); + for (int i = 0; i < length; i++) { + auto annotationObj = annotations->Get(i).As(); + double latitude = Nan::Get(annotationObj, Nan::New("latitude").ToLocalChecked()).ToLocalChecked()->NumberValue(); + double longitude = Nan::Get(annotationObj, Nan::New("longitude").ToLocalChecked()).ToLocalChecked()->NumberValue(); + std::string image = "foo"; + options.annotations.push_back({ + latitude, longitude, image + }); + } + } + } + if (Nan::Has(obj, Nan::New("center").ToLocalChecked()).FromJust()) { auto centerObj = Nan::Get(obj, Nan::New("center").ToLocalChecked()).ToLocalChecked(); if (centerObj->IsArray()) { @@ -392,6 +418,27 @@ void NodeMap::startRender(NodeMap::RenderOptions options) { map->setDebug(options.debugOptions); } + for(auto id: annotationIDs) { + map->removeAnnotation(id); + } + annotationIDs.clear(); + + for(auto annotationIconString: annotationIconNames) { + map->removeAnnotationIcon(annotationIconString); + } + annotationIconNames.clear(); + + for(auto annotation: options.annotations) { + std::string iconName = "airfield-15"; + annotationIconNames.push_back(iconName); + + mbgl::PremultipliedImage cPremultipliedImage({ static_cast(40), static_cast(40) }); + auto cSpriteImage = std::make_shared(std::move(cPremultipliedImage), 1); + map->addAnnotationIcon(iconName, cSpriteImage); + map->addAnnotation(mbgl::SymbolAnnotation {{ annotation.longitude, annotation.latitude }, iconName }); + annotationIDs.push_back(map->addAnnotation(mbgl::SymbolAnnotation {{ annotation.longitude, annotation.latitude }, iconName })); + } + map->renderStill(*view, [this](const std::exception_ptr eptr) { if (eptr) { error = std::move(eptr); diff --git a/platform/node/src/node_map.hpp b/platform/node/src/node_map.hpp index c68f543b02..4600bd5986 100644 --- a/platform/node/src/node_map.hpp +++ b/platform/node/src/node_map.hpp @@ -61,6 +61,8 @@ public: std::unique_ptr view; NodeThreadPool threadpool; std::unique_ptr map; + std::vector annotationIDs; + std::vector annotationIconNames; std::exception_ptr error; mbgl::PremultipliedImage image; diff --git a/platform/node/test.js b/platform/node/test.js new file mode 100644 index 0000000000..6a3cc66666 --- /dev/null +++ b/platform/node/test.js @@ -0,0 +1,44 @@ +var fs = require('fs'); +var path = require('path'); +var mbgl = require('./index.js'); +var sharp = require('sharp'); + +var options = { + request: function(req, callback) { + fs.readFile(path.join(__dirname, 'test', req.url), function(err, data) { + callback(err, { data: data }); + }); + }, + ratio: 1 +}; + +var map = new mbgl.Map(options); + +map.load(require('./test/fixtures/style.json')); + +map.render({ + zoom: 0, + bearing: 20, + annotations: [{ + latitude: 0, + longitude: 0, + imageBuffer: new Buffer() + }] +}, function(err, buffer) { + if (err) throw err; + + map.release(); + + var image = sharp(buffer, { + raw: { + width: 512, + height: 512, + channels: 4 + } + }); + + // Convert raw image buffer to PNG + image.toFile('image.png', function(err) { + if (err) throw err; + }); +}); -- cgit v1.2.1